方法适用于C ++,但如何在PHP中重现它?

时间:2014-12-28 00:16:38

标签: php c++ hashcode

我在c ++上有以下代码

std::string Battlenet::AccountMgr::CalculateShaPassHash(std::string const& name, std::string const& 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();
}

我正在尝试在PHP上重现相同的结果,以及到目前为止我所做的:

public function RegisterBattleNetAccount($email, $password)
{
      $GLOBALS['mysqli']->query("use {$GLOBALS['db_auth']}");
      //strtoupper
      $pass = hash('sha256', strrev(strtoupper(hash('sha256', $email))).':'.$password);
      $pass = strtoupper(strrev($pass));
      $email = strtoupper($email);
      $stmt = $GLOBALS['mysqli']->prepare("INSERT INTO battlenet_accounts (`email`,`sha_pass_hash`) VALUES (?, ?)");

      $stmt->bind_param("ss", $email, $pass);
      $stmt->execute();       
}

结果:

C++: 09FEBAB417CF2FA563AC89963519CCAC53D5F556F8BF20D7EEB818A0584A514E
PHP: 4e514a58a018b8eed720bff856f5d553accc19359689ac63a52fcf17b4bafe09

我能做些什么来获得与C ++相同的结果?

1 个答案:

答案 0 :(得分:4)

如果你仔细观察,你会看到(尽管如此)两个结果是相同的,除了每个十六进制对以相反的顺序写出。

使用reverse作为true调用您的C ++版本;所以,根本不要这样做!

(然后在两个程序之间保持一致。)