首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将php加密和hash_hmac(“sha512”代码)转换为c#

将php加密和hash_hmac(“sha512”代码)转换为c#
EN

Stack Overflow用户
提问于 2021-03-09 14:08:47
回答 1查看 276关注 0票数 1

我在将密码代码从php转换为c#时遇到了麻烦,我非常希望得到一些帮助,下面是php代码

代码语言:javascript
复制
$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);

示例数据如下:

代码语言:javascript
复制
$method = 'auth/register';
$param = [
 "email"                => 'test@test.com',
 "password"             => 'Password1234',
 "customerId"               => '1234',
];

到目前为止我所做的是

代码语言:javascript
复制
    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();
        }
    }

我被困在这两行里,我不知道我是否把它们翻译正确。

代码语言:javascript
复制
$signature = hash_hmac('sha512', $hash, base64_decode($this->secret), true);
$signature = base64_encode($signature);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-09 15:39:35

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

以下PHP代码:

代码语言:javascript
复制
$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);

因此返回:

代码语言:javascript
复制
Postdata:  email=test%40test.com&password=Password1234&customerId=1234
Signature: EYvVUz9oyis+0EA+PkfSIbDlD2uhd75BTQmuOM8ousmXSUALnB4l/9CkdxjgVy0X2oMsSKiDlzPQq/RLDeU70w==

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

代码语言:javascript
复制
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);

使用

代码语言:javascript
复制
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==
}

代码语言:javascript
复制
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;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66548460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档