SHA512哈希不同在Android上,PHP和JavaScript [英] SHA512 hashes differ on android, php and javascript

查看:391
本文介绍了SHA512哈希不同在Android上,PHP和JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的是SHA512哈希我的应用程序之间传输的一些加密数据,它的后端。不过,我有一个奇怪的情况,而且不知道什么可能导致它。

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的2倍SHA512

Android 2x SHA512

Android的1X SHA512 - > CryptoJS 1X SHA512

Android 1x SHA512 -> CryptoJS 1x SHA512

PHP 2倍SHA512

PHP 2x SHA512

所以,当我做的第一2X And​​roid的散列,我得到了相同的结果,当我做了1个机器人 - > 1X cryptojs。 但是,当我做了PHP的2倍,我得到了相同的结果,因为我得到了第一款Android传球,但第二加密通的PHP是不同的。

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.

在PHP中,我都试过散列()和openssl_digest()函数与原始字节作为输出。

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的:

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:

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?

推荐答案

SHA1是不是安全,则不要使用它这一点。 抓住任何实现BCrypt,做担保权。 而对于不同的哈希值。最有可能的编码问题涉及到字符串

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.

这篇关于SHA512哈希不同在Android上,PHP和JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆