SHA512 hashes differ on android, php and javascript

和自甴很熟 提交于 2019-11-29 15:52:29

问题


I am using the SHA512 hash to transfer some encrypted data between my app and it's backend. However, I'm having a odd situation and have no idea what might be causing it.

So, I've got following setups tested:

Android 2x SHA512

Android 1x SHA512 -> CryptoJS 1x SHA512

PHP 2x SHA512

So, when I do the first 2x Android hashing, I get the same result as when I do the 1x android -> 1x cryptojs. However, when I do the PHP 2x, I get the same result as I get on the first Android pass, but the second encryption pass of the PHP is different.

On PHP, I've tried both the hash() and openssl_digest() functions with raw bytes as output.

PHP:

$firstpass = base64_encode(hash('sha512', $enteredPassword, true));
//$firstpass = base64_encode(hash('sha512', $enteredPassword, true));

//$secondpass = base64_encode(openssl_digest($firstpass, 'sha512', true));
$secondpass = base64_encode(hash('sha512', $firstpass, true));

Android:

public static String encryptPassword(String password) {
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("SHA-512");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    if (md != null) {
        md.update(password.getBytes());
        byte byteData[] = md.digest();
        String base64 = Base64.encodeToString(byteData, Base64.DEFAULT);

        return base64;
    }
    return password;
}

CryptoJS:

var password = cryptojs.SHA512(req.params.password);
var basepassword = password.toString(cryptojs.enc.Base64);

Why would my first hash be correct and my second not and how could I fix this?


回答1:


SHA1 is not made for security, don't use it for this. Grab any implementation of BCrypt and do security right. As for the different hashes: Most likely an encoding issue related to Strings.



来源:https://stackoverflow.com/questions/23149282/sha512-hashes-differ-on-android-php-and-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!