我一直在尝试加密项目中的一些用户密码,但我似乎无法让它正常工作。我决定使用 SHA-256 算法,当我使用 Sha2(Example,256) 向 MySQL 引入密码时,它会在加密密码中添加两个零。在 Java 中,我使用它对程序中的文本进行哈希处理,但无法获得相同的结果。
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest("ContrasenhaPassword".getBytes("UTF-8"));
StringBuilder hexString = new StringBuilder();
for (int i: hash) {
hexString.append(Integer.toHexString(0XFF & i));
}
String Hashed = new String(hexString);
System.out.println(hexString);
System.out.println(Hashed);
// Below, MySQL Output for SHA2('ContrasenhaPassword',256)
System.out.println("d17bf0da90f56b8fc627bac6523ffd284aa0d82c870e1a0428274de048f49d78");
System.out.println(Hashed.equals(hexString));
} catch (Exception e) {
e.printStackTrace();
}
我得到的输出是:
d17bf0da90f56b8fc627bac6523ffd284aa0d82c87e1a428274de048f49d78
d17bf0da90f56b8fc627bac6523ffd284aa0d82c87e1a428274de048f49d78
d17bf0da90f56b8fc627bac6523ffd284aa0d82c870e1a0428274de048f49d78
false
BUILD SUCCESSFUL (total time: 0 seconds)
有任何想法吗?