I've been trying to encrypt some user passwords on a project but I can't seem to get it working properly. I've decided to use the SHA-256 algorithm and when I introduce a password to MySQL using the Sha2(Example,256) It adds two zeros to the crypted password. In Java I used this to hash the text on the program but can't get the equal result.
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();
}
The output I get is:
d17bf0da90f56b8fc627bac6523ffd284aa0d82c87e1a428274de048f49d78
d17bf0da90f56b8fc627bac6523ffd284aa0d82c87e1a428274de048f49d78
d17bf0da90f56b8fc627bac6523ffd284aa0d82c870e1a0428274de048f49d78
false
BUILD SUCCESSFUL (total time: 0 seconds)
Any ideas?