異なるコンピューターで異なるハッシュ値を返す MessageDigest に問題があります。
1 台のコンピュータは Windows Vista で 32 ビット Java を実行しており、もう 1 台は 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);
}
このコードは、これら 2 つの異なるマシンで異なる方法で実行する必要がありますか? 私が書いたようにマシンに依存している場合、これらのパスワードをより移植性の高い方法でハッシュする別の方法はありますか? ありがとう!
編集:::::
これは、ソルトを生成するために使用しているコードです。
public static String getSalt() {
int size = 16;
byte[] bytes = new byte[size];
new Random().nextBytes(bytes);
return org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString(bytes);
}
解決:::
受け入れられた解決策のおかげで、コードを修正できました。
public static boolean authenticate_(String salt, String encryptedPassword,
char[] plainTextPassword ) throws NoSuchAlgorithmException, UnsupportedEncodingException {
// This was ok
String saltPlusPlainTextPassword = salt + new String(plainTextPassword);
MessageDigest sha = MessageDigest.getInstance("SHA-512");
// must specify "UTF-8" encoding
sha.update(saltPlusPlainTextPassword.getBytes("UTF-8"));
byte[] hashedByteArray = sha.digest();
// Use Base64 encoding here -->
String hashed = org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString(hashedByteArray);
return hashed.equals(encryptedPassword);
}