简体   繁体   中英

Hash value of same string is different in Android and Javascript

The hash values of the same string is different in Javascript and Android for SHA-256:

Javascript Code:

var hashedPassword = CryptoJS.SHA256(userPassword);
var passwordText = hashedPassword.toString(CryptoJS.enc.Base64);

Android Code:

MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
byte[] hashedPassword = messageDigest.digest(userPassword.getBytes());
String hashedPasswordText = Base64.encodeToString(hashedPassword, Base64.DEFAULT).trim();

Output for String "beast":

In Javascript :

1bf417bd87162d5d77a5ce3259e81bdee9f8a0ef56bc0f12eadcd5805a94b708

In Android :

G/QXvYcWLV13pc4yWegb3un4oO9WvA8S6tzVgFqUtwg=

According to this answer CryptoJS.enc.Base64 requires another component. And if it does not find it it defaults to HEX.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM