hash_hmac('sha256', $payload, $this->client_secret, true)
在C#中的SHA256算法只有一个参数,没有地方输入key参与加密。所以哪个大婶帮帮小弟啊
The problem must be the actual representation of the key/message data.
See the following tests:
PHP
#!/usr/bin/php <?php print strtoupper(hash_hmac("sha256", "message", "key"));
?>
Output (live via http://writecodeonline.com/php/): 6E9EF29B75FFFC5B7ABAE527D58FDADB2FE42E7219011976917343065F58ED4A
C#
using System;
using System.Text;
using System.Security.Cryptography;
public class Program {
private const string key = "key";
private const string message = "message";
private static readonly Encoding encoding = Encoding.UTF8;
static void Main(string[] args) {
var keyByte = encoding.GetBytes(key);
using (var hmacsha256 = new HMACSHA256(keyByte)) { hmacsha256.ComputeHash(encoding.GetBytes(message));
Console.WriteLine("Result: {0}", ByteToString(hmacsha256.Hash));
}
}
static string ByteToString(byte[] buff) {
string sbinary = "";
for (int i = 0; i < buff.Length; i++)
sbinary += buff[i].ToString("X2");
/* hex format */ return sbinary; }
}
Output (live via http://ideone.com/JdpeL): Result: 6E9EF29B75FFFC5B7ABAE527D58FDADB2FE42E7219011976917343065F58ED4A
So, check the character set/encoding of the PHP input data. Also check the actual algorithm (in $pbx_hash).
爱编程,不求甚解。
其实我不懂PHP,也不懂C#,看着上面的代码好象是对的,在网上试运行了一下,发现暂时也是对的,就当他是对的,等不对的时候再Google。
http://php.net/manual/en/function.hash-hmac.php
private Object hash_hmac(string signatureString, string secretKey, bool raw_output = false) { var enc = Encoding.UTF8; HMACSHA1 hmac = new HMACSHA1(enc.GetBytes(secretKey)); hmac.Initialize(); byte[] buffer = enc.GetBytes(signatureString); if (raw_output) { return hmac.ComputeHash(buffer); } else { return BitConverter.ToString(hmac.ComputeHash(buffer)).Replace("-", "").ToLower(); } }
不懂php,直接把key加到‘sha256’后面不行吗?
hash_hmac('sha256'.key, $payload, $this->client_secret, true)
哦看错。
就是把key加到你要加密的字符串后面
LZ要利用好msdn啊。
HMACSHA256 的构造函数之一就是Key.
答案1感觉是在搞笑啊 :D