c# - 将 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;
}
推荐阅读
- c++ - 返回 unique_ptr 指向值副本的向量
- php - 如何从 SQL 中选择数据作为 INT 而不是字符串?
- r - 在 RStudio 中运行 cron 作业
- postgresql - 将 postgres 表从一台服务器迁移到另一台服务器
- android - Android Studio 上没有文本视图或操作栏
- php - 无法加载 URL:此 URL 的域不包含在应用程序的域中。为了能够加载此 URL... Codeigniter
- django - 如何在 Django 的 CreateView 中创建对象?
- lte - LTE传播模型
- ajax - 创建 post 方法并使用 ajax 和 laravel 传递 id
- ruby - Ruby HTTPclient:不推荐使用位置标头中的相对 URI