[英]Need to convert c# hashing function into PHP
我在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; }
最終代碼:
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;
}
如果您想要最后三行問題的特定答案(同時添加哈希和返回),則為:
// Append the two arrays
$toHash = array_merge($_secretBytes, $_saltBytes);
$computedHash = sha1(implode(array_map("chr", $toHash)), true);
return $computedHash;
您想要轉換的任何其他代碼都可以隨時詢問。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.