需要将c#散列函数转换为PHP

时间:2017-05-03 07:35:10

标签: c# php hash

我在C#中有以下功能,我需要将其转换为PHP。

C#:

public string ComputeSaltedHash()
    {
        // Create Byte array of password string
        ASCIIEncoding encoder = new ASCIIEncoding();
        Byte[] _secretBytes = encoder.GetBytes(_password);

        // Create a new salt
        Byte[] _saltBytes = new Byte[4];
        _saltBytes[0] = (byte)(_salt >> 24);
        _saltBytes[1] = (byte)(_salt >> 16);
        _saltBytes[2] = (byte)(_salt >> 8);
        _saltBytes[3] = (byte)(_salt);

        // append the two arrays
        Byte[] toHash = new Byte[_secretBytes.Length + _saltBytes.Length];
        Array.Copy(_secretBytes, 0, toHash, 0, _secretBytes.Length);
        Array.Copy(_saltBytes, 0, toHash, _secretBytes.Length, _saltBytes.Length);

        SHA1 sha1 = SHA1.Create();
        Byte[] computedHash = sha1.ComputeHash(toHash);

        return encoder.GetString(computedHash);
    }

注意:我不能在c#端做任何事情。

修改1:

PHP代码(到目前为止已转换):

function ComputeSaltedHash($Pass, $Salt) {

$secretBytes = array();
for($i = 0; $i < strlen($Pass); $i++)
{
   $secretBytes[] = ord($Pass[$i]);
}

$saltBytes = array(4);
$saltBytes[0] = ConverttoByte($Salt >> 24);
$saltBytes[1] = ConverttoByte($Salt >> 16);
$saltBytes[2] = ConverttoByte($Salt >> 8);
$saltBytes[3] = ConverttoByte($Salt);

$result = array_merge($secretBytes, $saltBytes);

// Need to convert SHA1 & onward }

需要转换最后3行c#代码。

编辑2 :(答案)

function ComputeSaltedHash($Pass, $Salt) {

// Create Byte array of password string
$secretBytes = array();
for($i = 0; $i < strlen($Pass); $i++)
{
   $secretBytes[] = ord($Pass[$i]);
}

// Create a new salt
$saltBytes = array(4);
$saltBytes[0] = ConverttoByte($Salt >> 24);
$saltBytes[1] = ConverttoByte($Salt >> 16);
$saltBytes[2] = ConverttoByte($Salt >> 8);
$saltBytes[3] = ConverttoByte($Salt);

// Append the two arrays
$result = array_merge($secretBytes, $saltBytes);

// Convert array into string for SHA1
$output = sha1(implode(array_map("chr", $result)), true);

// ASCII only defines mappings for values 0–127. Replace values greater than 127 with ?
for ($i = 0; $i < strlen($output); $i++) {
    if ($output[$i] > chr(127)) {
        $output[$i] = '?';
    }       
}   

// Final Result
return $output; }

1 个答案:

答案 0 :(得分:0)

最终守则:

function ComputeSaltedHash($Pass, $Salt) {
 // Create Byte array of password string
 $secretBytes = array();
 for($i = 0; $i < strlen($Pass); $i++)
 {
   $secretBytes[] = ord($Pass[$i]);
 }

 // Create a new salt
 $saltBytes = array(4);
 $saltBytes[0] = ConverttoByte($Salt >> 24);
 $saltBytes[1] = ConverttoByte($Salt >> 16);
 $saltBytes[2] = ConverttoByte($Salt >> 8);
 $saltBytes[3] = ConverttoByte($Salt);

 // Append the two arrays
 $result = array_merge($secretBytes, $saltBytes);

 // Convert array into string for SHA1
 $output = sha1(implode(array_map("chr", $result)), true);

 // ASCII only defines mappings for values 0–127. Replace values greater than 127 with ?
 for ($i = 0; $i < strlen($output); $i++) {
  if ($output[$i] > chr(127)) {
     $output[$i] = '?';
  }       
 }   

 // Final Result
 return $output; 
}