Practice · 2 of 2
Credentials done right
CHALLENGE
Difficulty: advanced+25 XP
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).
Press Submit to check your solution.
Back to lesson: Practice: Security drills