我遇到MessageDigest在不同计算机上返回不同哈希值的问题.
一台计算机在Windows Vista上运行32位Java,另一台在Mac OS上运行64位Java.我不确定是不是因为MessageDigest是依赖于机器的,或者我需要在某处明确指定字符编码,或者可能是其他东西.这是代码:
public static boolean authenticate(String salt, String encryptedPassword,
char[] plainTextPassword ) throws NoSuchAlgorithmException {
// do I need to explcitly specify character encoding here? -->
String saltPlusPlainTextPassword = salt + new String(plainTextPassword);
MessageDigest sha = MessageDigest.getInstance("SHA-512");
// is this machine dependent? -->
sha.update(saltPlusPlainTextPassword.getBytes());
byte[] hashedByteArray = sha.digest();
// or... perhaps theres a translation problem here? -->
String hashed = new String(hashedByteArray);
return hashed.equals(encryptedPassword);
}
Run Code Online (Sandbox Code Playgroud)
这些代码应该在这两台不同的机器上执 如果它与我编写它的方式是机器相关的,那么还有另一种方法来散列这些更便携的密码吗?谢谢!
这是我用来生成盐的代码:
public static String getSalt() {
int size = 16; …Run Code Online (Sandbox Code Playgroud)