Skip to main content

Practice ยท 2 of 2

Credentials done right

1. static byte[] salt() โ€” 16 random bytes (SecureRandom). 2. static byte[] hash(byte[] salt, String password) โ€” SHA-256 over salt bytes followed by UTF-8 password bytes (concatenate the two arrays; MessageDigest from java.security). 3. static boolean verify(byte[] salt, String password, byte[] expectedHash) โ€” recompute hash(salt, password) and compare with MessageDigest.isEqual (constant time). 4. static boolean samePasswordDifferentSalt(byte[] s1, byte[] s2, String pw) โ€” true iff hash(s1,pw) differs from hash(s2,pw) โ€” demonstrating that two users with the same password produce different stored bytes. 5. static boolean timingSafe(byte[] a, byte[] b) โ€” MessageDigest.isEqual(a, b).

Difficulty: advanced

Back to lesson: Practice: Security drills