Cryptographic Hashing with MessageDigest
Hash passwords with SHA-256, add salt to prevent rainbow table attacks, and compare digests securely.
What Is Cryptographic Hashing?
A cryptographic hash function maps data of any size to a fixed-size digest. It is one-way (cannot be reversed), deterministic, and collision-resistant. Used for password storage, data integrity, and digital signatures.
MessageDigest API
MessageDigest.getInstance("SHA-256") returns a SHA-256 digester. Call digest(bytes) to compute the hash and get a byte array.
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] hash = md.digest("password123".getBytes(StandardCharsets.UTF_8));
String hex = HexFormat.of().formatHex(hash);
System.out.println(hex); // 64-char hex stringAll lessons in this course
- Cryptographic Hashing with MessageDigest
- AES Symmetric Encryption
- HMAC-SHA256 for Message Integrity
- JWT: Structure, Creation, and Verification