【发布时间】:2018-08-03 22:05:46
【问题描述】:
我想将 Java 中的一些代码转换为 C#。
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 时,我得到了不同的哈希
更新:
例如:
Java: byte[] hash = digest.digest(SALT); 生成(前 6 个字节):
[0] = 9
[1] = -95
[2] = -68
[3] = 64
[4] = -11
[5] = 53
....
C# 代码(SimpleHash 类): 字符串 hashValue = Convert.ToBase64String(hashWithSaltBytes); hashWithSaltBytes 有(前 6 个字节):
[0] 175 byte
[1] 209 byte
[2] 120 byte
[3] 74 byte
[4] 74 byte
[5] 227 byte
【问题讨论】:
-
如果使用
"SHA-256",为什么方法命名为getMd5Hash? -
我不太清楚。可能是这个方法在过去生成了MD5。我们只是将项目从 java 转换为 C#
-
您如何解决此问题。你能在这里更新修复,我也面临同样的问题
标签: c# java .net cryptography sha256