如何将此函数转换为 php?
How I can convert this function to php?
我在 .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);
}
这应该可以解决问题:
<?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);
我在 .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);
}
这应该可以解决问题:
<?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);