第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

C# HMACSHA256 在 PHP 中等效

C# HMACSHA256 在 PHP 中等效

PHP
忽然笑 2021-09-18 15:57:09
我正在嘗試使用hash_hmac sha256來創(chuàng)建數(shù)字簽名,php但問題是結(jié)果與api使用C#. 這是C#代碼:public static String createSignature(string apiSecret, long nonce, string customerId, string apiKey){    byte[] keyBytes = StringToByteArray(apiSecret);    using (HMACSHA256 hmac = new HMACSHA256(keyBytes))    {        byte[] data = hmac.ComputeHash(Encoding.UTF8.GetBytes(nonce + customerId + apiKey));        return bytesToHex(data);    }}public static String bytesToHex(byte[] bytes){    char[] hexArray = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };    char[] hexChars = new char[bytes.Length * 2];    for (int j = 0; j < bytes.Length; ++j)    {        int v = bytes[j] & 255;        hexChars[j * 2] = hexArray[(uint)v >> 4];        hexChars[j * 2 + 1] = hexArray[v & 15];    }    return new String(hexChars);}public static byte[] StringToByteArray(string hex){    return Enumerable.Range(0, hex.Length)                     .Where(x => x % 2 == 0)                     .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))                     .ToArray();}這是我的php代碼:$string = $nonce.$customer.$api_key;$signature = hash_hmac("sha256", utf8_encode($string), utf8_encode($api_secret));我只能更改php代碼以匹配C#結(jié)果。到目前為止,我使用utf8_encode,base64_encode和 simplestring作為hash_hmacfuncton 中的輸入,它們的結(jié)果都與C#版本不同。注:測試我使用固定nonce,customer并api_key與secret在php和C#PS:我知道有有關(guān)這個問題的現(xiàn)場工作解決多個問題。我嘗試了其中的大部分,但沒有一個對我有用。
查看完整描述

1 回答

?
躍然一笑

TA貢獻(xiàn)1826條經(jīng)驗(yàn) 獲得超6個贊

這應(yīng)該可以解決問題:


string result = createSignature("AABBCC", 0, "1", "mykey");


// FFB95E1E991734F1C2AE0B7C7ACECFAA5D3BEE943189539C3439B344A9A82E39

Console.WriteLine(result);

<?php


$nonce = 0;

$customer = '1';

$api_key = 'mykey';

$api_secret = hex2bin('AABBCC');


$string = $nonce . $customer . $api_key;

$signature = strtoupper(hash_hmac('sha256', $string, $api_secret));


// FFB95E1E991734F1C2AE0B7C7ACECFAA5D3BEE943189539C3439B344A9A82E39

echo $signature;


查看完整回答
反對 回復(fù) 2021-09-18
  • 1 回答
  • 0 關(guān)注
  • 219 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號