简体   繁体   中英

Need to convert c# hashing function into PHP

I have the following function in C# which I need to convert into 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);
    }

Note: I can't do anything on the c# side.

Edit 1:

PHP Code (Converted so far) :

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 }

Need to convert last 3 lines of c# code.

Edit 2: (Answer)

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; }

Final Code:

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; 
}

As you want a Specific answer for your question of last 3 lines (Append both, hashing and return) is:

// Append the two arrays
$toHash = array_merge($_secretBytes, $_saltBytes);

$computedHash = sha1(implode(array_map("chr", $toHash)), true);

return $computedHash;

Any more codes you want to convert feel free to ask.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM