我想將一些在Java中的代碼轉換爲C#。C#SHA-256與Java SHA-256。不同的結果?
Java代碼:
private static final byte[] SALT = "NJui8*&N823bVvy03^4N".getBytes();
public static final String getSHA256Hash(String secret)
{
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.update(secret.getBytes());
byte[] hash = digest.digest(SALT);
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < hash.length; i++) {
hexString.append(Integer.toHexString(0xFF & hash[i]));
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
throw new RuntimeException("SHA-256 realization algorithm not found in JDK!");
}
當我試圖使用the SimpleHash class我得到不同hashs
UPDATE:
例如:
爪哇:字節[]散列= digest.digest(SALT); 生成(前6個字節):
[0] = 9
[1] = -95
[2] = -68
[3] = 64
[4] = -11
[5] = 53
....
C#代碼(類SimpleHash): 串散列值= Convert.ToBase64String(hashWithSaltBytes); hashWithSaltBytes具有(前6個字節):
[0] 175 byte
[1] 209 byte
[2] 120 byte
[3] 74 byte
[4] 74 byte
[5] 227 byte
爲什麼法名爲'getMd5Hash'如果你使用'「SHA-256」'? – dtb
我不知道。可能是這種方法在過去生成MD5。我們只是將項目從java轉換爲C# –