0
我們需要使用SHA-256散列字符串。SHA-256散列返回不同結果
這是我的代碼有:
public String getSHA256HashedString(String clearString) {
String _LOC = "[SB_UtilityBean: getSHA256HashedString]";
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(clearString.getBytes());
String _encrypted = bytesToHex(md.digest());
System.out.println(_LOC + "1.0 " + " Result 1: " + _encrypted);
System.out.println(_LOC + "1.0 " + " Result 2: " + bytesToHex(md.digest()));
return _encrypted;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
private static String bytesToHex(byte[] bytes) {
String _LOC = "[SB_UtilityBean: bytesToHex]";
StringBuffer result = new StringBuffer();
for (byte byt : bytes) result.append(Integer.toString((byt & 0xff) + 0x100, 16).substring(1));
return result.toString();
}
如果bytesToHex(md.digest())
被分配給一個變量_encrypted
,做的_encrypted
打印線,我得到的結果1:
9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08
如果我打印的行數爲bytesToHex(md.digest())
,我會得到Result 2:
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
@AndrewMedico我明白了。那麼結果1是否是正確的哈希? – bittersour
@bittersour是的。第二個散列('e3b ... 855')是一個空緩衝區的SHA256總和。 – duskwuff