I am new to java security so it may sound stupid to you guys. I am using triple-des algorithm for encryption decryption in that i am using hash value as keys. I am using sha-512 for hashing i have heard that two same strings hash will be same but i am getting different output for same string. I am affixing the code of sha-512. Let me know what the problem is if possible.
public class SHA256Algo {
public static String createHash(String text) throws UnsupportedEncodingException, NoSuchAlgorithmException
{
String encryptedText = "" ;
MessageDigest md = MessageDigest.getInstance("SHA-512");
md.update(text.getBytes("UTF-16")); // Change this to "UTF-16" if needed
byte[] digest = md.digest();
String str = digest.toString() ;
return str ;
}
public static void main(String[] args) {
try {
System.out.println(createHash("tarun")) ;
System.out.println(createHash("tarun")) ;
} catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
output :
[B@32d2bb53
[B@29086036
Hashing.sha512().hashString(text, Charsets.UTF_16).toString()
, which would accomplish exactly what you want. – Louis Wasserman