@Test public void testVerify() { assertTrue(PasswordHelper.verifyPassword("abcd", "abcd")); assertTrue(PasswordHelper.verifyPassword("abcd", "{SSHA}WPvqVeSt0Mr2llICYmAX9+pjtPH271eznDHvrw==")); assertTrue(PasswordHelper.verifyPassword("abcd", "{SMD5}/wZ7JQUARlCBq4JFHI57AfbvV7OcMe+v")); assertFalse(PasswordHelper.verifyPassword("1234", "abcd")); assertFalse(PasswordHelper.verifyPassword("1234", "{SSHA}WPvqVeSt0Mr2llICYmAX9+pjtPH271eznDHvrw==")); assertFalse(PasswordHelper.verifyPassword("1234", "{SMD5}/wZ7JQUARlCBq4JFHI57AfbvV7OcMe+v")); assertFalse(PasswordHelper.verifyPassword(" abcd", "abcd")); assertFalse(PasswordHelper.verifyPassword("abcd", "{SSHA}WPvqVeS")); assertFalse(PasswordHelper.verifyPassword("abcd", "{SSHA}/wZ7JQUAR")); assertFalse(PasswordHelper.verifyPassword("abcd", "{SSHA}WXYZ")); assertFalse(PasswordHelper.verifyPassword("abcd", "{SSHA}WPvqVeSt0Mr2llICYmAX9+pjtPH271eznDHvrwfghijkl")); assertFalse(PasswordHelper.verifyPassword("abcd", "{SSHA}/wZ7JQUARlC*")); }
public static boolean verifyPassword(String password, String hashedPassword) { String digestalg; int len; if (hashedPassword.startsWith(HSSHA)) { digestalg = SHA1; len = 20; } else if (hashedPassword.startsWith(HSMD5)) { digestalg = MD5; len = 16; } else { return hashedPassword.equals(password); } String digest = hashedPassword.substring(6); byte[] bytes = Base64.decode(digest); if (bytes == null) { return false; } if (bytes.length < len + 2) { return false; } byte[] hash = new byte[len]; byte[] salt = new byte[bytes.length - len]; System.arraycopy(bytes, 0, hash, 0, hash.length); System.arraycopy(bytes, hash.length, salt, 0, salt.length); return MessageDigest.isEqual(hash, digestWithSalt(password, salt, digestalg)); }
PasswordHelper { public static boolean verifyPassword(String password, String hashedPassword) { String digestalg; int len; if (hashedPassword.startsWith(HSSHA)) { digestalg = SHA1; len = 20; } else if (hashedPassword.startsWith(HSMD5)) { digestalg = MD5; len = 16; } else { return hashedPassword.equals(password); } String digest = hashedPassword.substring(6); byte[] bytes = Base64.decode(digest); if (bytes == null) { return false; } if (bytes.length < len + 2) { return false; } byte[] hash = new byte[len]; byte[] salt = new byte[bytes.length - len]; System.arraycopy(bytes, 0, hash, 0, hash.length); System.arraycopy(bytes, hash.length, salt, 0, salt.length); return MessageDigest.isEqual(hash, digestWithSalt(password, salt, digestalg)); } }
PasswordHelper { public static boolean verifyPassword(String password, String hashedPassword) { String digestalg; int len; if (hashedPassword.startsWith(HSSHA)) { digestalg = SHA1; len = 20; } else if (hashedPassword.startsWith(HSMD5)) { digestalg = MD5; len = 16; } else { return hashedPassword.equals(password); } String digest = hashedPassword.substring(6); byte[] bytes = Base64.decode(digest); if (bytes == null) { return false; } if (bytes.length < len + 2) { return false; } byte[] hash = new byte[len]; byte[] salt = new byte[bytes.length - len]; System.arraycopy(bytes, 0, hash, 0, hash.length); System.arraycopy(bytes, hash.length, salt, 0, salt.length); return MessageDigest.isEqual(hash, digestWithSalt(password, salt, digestalg)); } private  PasswordHelper(); }
PasswordHelper { public static boolean verifyPassword(String password, String hashedPassword) { String digestalg; int len; if (hashedPassword.startsWith(HSSHA)) { digestalg = SHA1; len = 20; } else if (hashedPassword.startsWith(HSMD5)) { digestalg = MD5; len = 16; } else { return hashedPassword.equals(password); } String digest = hashedPassword.substring(6); byte[] bytes = Base64.decode(digest); if (bytes == null) { return false; } if (bytes.length < len + 2) { return false; } byte[] hash = new byte[len]; byte[] salt = new byte[bytes.length - len]; System.arraycopy(bytes, 0, hash, 0, hash.length); System.arraycopy(bytes, hash.length, salt, 0, salt.length); return MessageDigest.isEqual(hash, digestWithSalt(password, salt, digestalg)); } private  PasswordHelper(); static boolean isHashed(String password); static String hashPassword(String password, String algorithm); static boolean verifyPassword(String password, String hashedPassword); static byte[] digestWithSalt(String password, byte[] salt,
            String algorithm); }
PasswordHelper { public static boolean verifyPassword(String password, String hashedPassword) { String digestalg; int len; if (hashedPassword.startsWith(HSSHA)) { digestalg = SHA1; len = 20; } else if (hashedPassword.startsWith(HSMD5)) { digestalg = MD5; len = 16; } else { return hashedPassword.equals(password); } String digest = hashedPassword.substring(6); byte[] bytes = Base64.decode(digest); if (bytes == null) { return false; } if (bytes.length < len + 2) { return false; } byte[] hash = new byte[len]; byte[] salt = new byte[bytes.length - len]; System.arraycopy(bytes, 0, hash, 0, hash.length); System.arraycopy(bytes, hash.length, salt, 0, salt.length); return MessageDigest.isEqual(hash, digestWithSalt(password, salt, digestalg)); } private  PasswordHelper(); static boolean isHashed(String password); static String hashPassword(String password, String algorithm); static boolean verifyPassword(String password, String hashedPassword); static byte[] digestWithSalt(String password, byte[] salt,
            String algorithm); static final String SSHA; static final String SMD5; }