我如何将此功能转换为PHP?

时间:2017-10-12 17:18:23

标签: php .net hash sha512

我在.net中有这个功能,但我需要在php代码中使用它。 我怎样才能做到这一点? 谢谢。

originalPassword=user+password;
public static string Encrypt(string originalPassword)
  {
string key = "7f9facc418f74439c5e9709832;0ab8a5:OCOdN5Wl,q8SLIQz8i|8agmu¬s13Q7ZXyno/yv.XSN1DsgKq9zi]XrE^gx8vPC^Av8=e/bF4pX1Oe hfqGb#JK~RONkS1wx5w=RE0$DxZSu7evPfshBw7p5Gb&suEkw=RE0DxZSu7e´vPfshBw7p+5GbsuEkw=H1fTWFXfsXo}z0fOd{KTt[IdDG2y6E=";

SHA512 sha512 = new SHA512CryptoServiceProvider();
byte[] inputBytes = (new UnicodeEncoding()).GetBytes(originalPassword + key);
        byte[] hash = sha512.ComputeHash(inputBytes);
        return Convert.ToBase64String(hash);
}

1 个答案:

答案 0 :(得分:0)

这应该可以解决问题:

<?php

function hash_data($data)
{
    $data = mb_convert_encoding($data, 'UTF-16LE', 'UTF-8');
    $hash = hash('sha512', $data, true);
    return base64_encode($hash);
}

$user = 'admin';
$password = 'secret';
$key = "7f9facc418f74439c5e9709832;0ab8a5:OCOdN5Wl,q8SLIQz8i|8agmu¬s13Q7ZXyno/yv.XSN1DsgKq9zi]XrE^gx8vPC^Av8=e/bF4pX1Oe hfqGb#JK~RONkS1wx5w=RE0$" . "DxZSu7evPfshBw7p5Gb&suEkw=RE0DxZSu7e´vPfshBw7p+5GbsuEkw=H1fTWFXfsXo}z0fOd{KTt[IdDG2y6E=";
$data = $user . $password . $key;

// 6xecArT38JVtGKH2yQs/T6btOUF41vW5ptaPjgrd8hTaZZKnbJed5551LuYV7vR/Dr3Jb873JMvX0je+8XUpxw==
echo hash_data($data);