为什么在C ++和PHP SHA256哈希之间得到不同的结果?

伊壁鸠鲁4

我正在尝试在PHP中生成SHA256哈希,它将存储在数据库中并由C ++程序进行身份验证。

在php中,哈希看起来像这样:

$mail = strtoupper(utf8_decode('[email protected]'));
$password = strtoupper(utf8_decode('password'));

$email = hash_init('sha256');
         hash_update($email, $mail);
$mail  = strtoupper(hash_final($email)); // In C++ the output is uppercase

$sha   = hash_init('sha256');
         hash_update($sha, $mail);
         hash_update($sha, ':');
         hash_update($sha, $password);
$pass  = hash_final($sha);

或用另一种方式写:

$pass = hash('sha256', strtoupper(hash('sha256', $mail)).':'.$password);

在C ++中,过程如下所示:

Utf8ToUpperOnlyLatin(email);
Utf8ToUpperOnlyLatin(password);

SHA256Hash email;
email.UpdateData(name);
email.Finalize();

SHA256Hash sha;
sha.UpdateData(ByteArrayToHexStr(email.GetDigest(), email.GetLength()));
sha.UpdateData(":");
sha.UpdateData(password);
sha.Finalize();

return ByteArrayToHexStr(sha.GetDigest(), sha.GetLength(), true);

以及方法:

std::string ByteArrayToHexStr(uint8 const* bytes, uint32 arrayLen, bool reverse /* = false */)
{
    int32 init = 0;
    int32 end = arrayLen;
    int8 op = 1;

    if (reverse)
    {
        init = arrayLen - 1;
        end = -1;
        op = -1;
    }

    std::ostringstream ss;
    for (int32 i = init; i != end; i += op)
    {
        char buffer[4];
        sprintf(buffer, "%02X", bytes[i]);
        ss << buffer;
    }

    return ss.str();
}

bool Utf8ToUpperOnlyLatin(std::string& utf8String)
{
    std::wstring wstr;
    if (!Utf8toWStr(utf8String, wstr))
        return false;

    std::transform(wstr.begin(), wstr.end(), wstr.begin(), wcharToUpperOnlyLatin);

    return WStrToUtf8(wstr, utf8String);
}

SHA256Hash::SHA256Hash()
{
    SHA256_Init(&mC);
    memset(mDigest, 0, SHA256_DIGEST_LENGTH * sizeof(uint8));
}

void SHA256Hash::UpdateData(const uint8 *dta, int len)
{
    SHA256_Update(&mC, dta, len);
}

void SHA256Hash::UpdateData(const std::string &str)
{
    UpdateData((uint8 const*)str.c_str(), str.length());
}

void SHA256Hash::Initialize()
{
    SHA256_Init(&mC);
}

void SHA256Hash::Finalize(void)
{
    SHA256_Final(mDigest, &mC);
}

使用这两种方法的输出是:

C++: 09FEBAB417CF2FA563AC89963519CCAC53D5F556F8BF20D7EEB818A0584A514E
PHP: 4e514a58a018b8eed720bff856f5d553accc19359689ac63a52fcf17b4bafe09

如果我要交换

$mail  = strtoupper(hash_final($email)); // In C++ the output is uppercase

为了

$mail  = hash_final($email);

PHP的输出将是

89ba15a964331258bcc763f44473c492854bf9c2694cc2306da64ccef8ffeab2

为什么我似乎无法让PHP和C ++产生相同的结果?

谢谢你。

伊壁鸠鲁4

C ++实现使用哈希返回的字节的倒数。两种方法都是正确的,但是输出结果与轻微的疏忽不符。感谢Geoffliu指出这一点。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么sha256哈希键与命令提示符和Powershell不同

SHA256哈希从OpenSSL和Sha256Sum实用程序Java不同产生。为什么?

为什么HMAC sha256在PHP和Javascript上返回不同的值

为什么我在系统DLL上的Powershell和32bit-Python之间得到不同的SHA1哈希?

目标C和Java中的Sha256哈希

Go和PHP中的SHA256提供不同的结果

SHA256哈希算法使用Common Crypto和OpenSSL在iOS中产生不同的结果

为什么我在Powershell和C#之间得到不同的结果

为什么我在C#和VB.NET之间得到不同的CRC16结果?

SAML令牌:为什么要使用多种哈希/摘要算法:sha256和sha1?

为什么我的Java和命令行SHA256输出不同?

Java SHA256向PHP SHA256输出不同的哈希吗?

Java和C#之间的SHA1哈希结果不同

为什么Go sha256与Ubuntu命令sha256sum给出不同的结果?

SHA256哈希结果在Android和iOS上对于大数字而言有所不同

为什么AppLocker使用的SHA256哈希与其他生成器不同?

当文件来自system32文件夹时,文件的哈希值MD5和SHA256会有所不同。为什么?

Javascript Sha256与PHP Sha256:换行符会创建不同的哈希值吗?

Android Java和PHP HASH HMAC SHA256不同的结果

C ++中的SHA256哈希计算

为什么在检查文件的sha256sum时会得到2个不同的结果?

SHA256 C# 与 PHP 的区别

OpenSSL和PHP SHA256摘要不同

Node.js和Kotlin中的SHA256不同的哈希

Swift SHA256哈希与PHP SHA256哈希不匹配

为什么`time`和`strace -c`不同?

为什么Java和Go的gzip会得到不同的结果?

为什么萨摩亚的pytz和dateutil会得到不同的结果?

在 C# 中從 Ascii 文本創建 SHA256 哈希 - 相當於 PHP bin2hex