I have a below method in java which using the SHA256 algo to encrypt the string value 1234
public static String getHashedCode() {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
String lastFourDigs = "1234";
byte[] encodedHash = digest.digest(lastFourDigs.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encodedHash);
} catch (Exception e) {
System.out.println("Exception: " + e);
}
return null;
}
which will print hascode of 1234
as A6xnQhbz4Vx2HuGl4lXwZ5U2I8iziLRFnhP5eNfIRvQ
Now I want to achieve same thing in postman and tried below snippet
var sha256Hash = CryptoJS.SHA256('1234').toString();
var base64Hash = CryptoJS.enc.Utf8.parse('sha256Hash');
var base64 = CryptoJS.enc.Base64.stringify(base64Hash);
pm.environment.set("encrypted", base64);
And its generating a different hashcode c2hhMjU2SGFzaA==
which is not same as what java created.
Am i doing anything wrong here? Any help highly appreciated.!