将 php 加密和 hash_hmac('sha512' 代码转换为 c#



我有麻烦转换密码代码从php到c#,我真的很感激一些帮助,这里是php代码

$postdata = http_build_query($param, '', '&');
$hash = '/' .$method . hash('sha256', $nonce . $postdata, true);
$signature = hash_hmac('sha512', $hash, base64_decode($this->secret), true);
$signature = base64_encode($signature);

示例数据如下:

$method = 'auth/register';
$param = [
"email"                => 'test@test.com',
"password"             => 'Password1234',
"customerId"               => '1234',
];

到目前为止我所做的是

string nonce ="123456789"
string param="email=test@test.com.lb&password=Devilmaycry@5&customerId=20210309035037"
public string CalculateSignature(string nonce,string pth)
{

string hash =pth+ ComputeSha256Hash(nonce);


Encoding ascii = Encoding.ASCII;                       

HMACSHA512 hmac = new HMACSHA512(ascii.GetBytes(client_secret));
string calc_sig = Convert.ToBase64String(hmac.ComputeHash(ascii.GetBytes(hash)));
return calc_sig;
}
static string ComputeSha256Hash(string rawData)
{
// Create a SHA256   
using (SHA256 sha256Hash = SHA256.Create())
{
// ComputeHash - returns byte array  
byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));
// Convert byte array to a string   
StringBuilder builder = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
builder.Append(bytes[i].ToString("x2"));
}
return builder.ToString();
}
}

我被困在这两行,我不知道我是否翻译正确

$signature = hash_hmac('sha512', $hash, base64_decode($this->secret), true);
$signature = base64_encode($signature);

为了比较两种实现,示例数据是有用的。

下面的PHP代码:

$nonce = 'nonce';           // sample data
$secret = 'c2VjcmV0';       // sample data
$method = 'auth/register';
$param = [
"email"                => 'test@test.com',
"password"             => 'Password1234',
"customerId"           => '1234',
];
$postdata = http_build_query($param, '', '&');
print('Postdata:  ' . $postdata . PHP_EOL);
$hash = '/' . $method . hash('sha256', $nonce . $postdata, true);
$signature = hash_hmac('sha512', $hash, base64_decode($secret), true);
$signature = base64_encode($signature);
print('Signature: ' . $signature . PHP_EOL);

返回结果:

Postdata:  email=test%40test.com&password=Password1234&customerId=1234
Signature: EYvVUz9oyis+0EA+PkfSIbDlD2uhd75BTQmuOM8ousmXSUALnB4l/9CkdxjgVy0X2oMsSKiDlzPQq/RLDeU70w==

下面的c#实现给出了相同的结果:

string method = "auth/register";
string nonce = "nonce";
string postdata = "email=test%40test.com&password=Password1234&customerId=1234";
string secret = "c2VjcmV0";
string signatureB64 = CalculateSignature(method, nonce, postdata, secret);
Console.WriteLine(signatureB64);

public static string CalculateSignature(string method, string nonce, string postdata, string secret)
{
// Encoding
byte[] methodBytes = Encoding.ASCII.GetBytes("/" + method);
byte[] noncePostdataBytes = Encoding.ASCII.GetBytes(nonce + postdata);
byte[] secretBytes = Convert.FromBase64String(secret);
// $hash = '/' . $method . hash('sha256', $nonce . $postdata, true);
byte[] nonceHash = SHA256.Create().ComputeHash(noncePostdataBytes);
byte[] hash = concat(methodBytes, nonceHash);
// $signature = hash_hmac('sha512', $hash, base64_decode($secret), true);
byte[] signature = new HMACSHA512(secretBytes).ComputeHash(hash);
// $signature = base64_encode($signature);
string signatureB64 = Convert.ToBase64String(signature);
return signatureB64; // EYvVUz9oyis+0EA+PkfSIbDlD2uhd75BTQmuOM8ousmXSUALnB4l/9CkdxjgVy0X2oMsSKiDlzPQq/RLDeU70w==
}

public static byte[] concat(byte[] arr1, byte[] arr2)
{
byte[] newArr = new byte[arr1.Length + arr2.Length];
Buffer.BlockCopy(arr1, 0, newArr, 0, arr1.Length);
Buffer.BlockCopy(arr2, 0, newArr, arr1.Length, arr2.Length);
return newArr;
}

最新更新