我在 PHP 中有一个字符串正在转换为字节数组并进行哈希处理。
转换为字节数组的字符串如下所示:
“克”。字符(0)。 “便便”;
我需要 C# 中的等效字节数组,以便我可以获得相同的哈希值..
编辑:这是完整的问题,结果哈希值不一样。
PHP
$api_secret = '5432919427bd18884fc2a6e48b65dfba48fd9a1a46e3468b52fadbc6d6b463425';
$data = 'payment_currency=USD&group_orders=0&count=100&nonce=1385689989977529';
$endpoint = '/info/orderbook';
$signature = hash_hmac('sha512', $endpoint . chr(0) . $data, $api_secret);
$result = base64_encode($signature);
C#
var apiSecret = "5432919427bd18884fc2a6e48b65dfba48fd9a1a46e3468b52fadbc6d6b463425";
var data = "payment_currency=USD&group_orders=0&count=100&nonce=1385689989977529";
var endPoint = "/info/orderbook";
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
String message = endpPoint + Convert.ToChar(0) + data;
var hmacsha512 = new HMACSHA512(encoding.GetBytes(message));
var result = Convert.ToBase64String(hmacsha512.Hash);
我尝试过不同的 base64 编码,例如:
public static string ByteToString(byte[] buff)
{
string sbinary = "";
for (int i = 0; i < buff.Length; i++)
sbinary += buff[i].ToString("X2"); /* hex format */
return sbinary;
}
但最终问题似乎是由于 chr(0) php 使用而被散列的 byteArray。
最佳答案
我再次回答,因为你已经改变了整个问题,所以你的新问题有一个新的解决方案。
首先,HMACSHA512 不会给出与您的 php 代码相同的结果。顺便说一下,您的 PHP 生成的哈希是:
41028bb90af31dc6e7fa100a8ceb1e220bfedf67ea723292db9a4e1c14f69c73adf30eeba61ab054cdc91c82f6be2f76dd602392be630b5e99b1f86da1460cbe
为了在 C# 中获得相同的结果,我创建了 BillieJeansSHA512
类来使散列与 PHP 相同。此外,使用 encoding.GetBytes
将 byte[] 转换为 String 我创建了方法 ByteToString
以正确转换。
哦,下面的代码不像 PHP 那样简单,但我向您或任何人挑战,使用相同的 PHP 散列来更简单地完成此代码!我敢你,我敢你!让我们转到代码:
//These are not default imports, so you need to use it
using System.Text;
using System.Security.Cryptography;
//Before your actual class, you need to make your custom 512
public class BillieJeansSHA512 : HMAC
{
public BillieJeansSHA512(byte[] key)
{
HashName = "System.Security.Cryptography.SHA512CryptoServiceProvider";
HashSizeValue = 512;
BlockSizeValue = 128;
Key = (byte[])key.Clone();
}
}
//Now, there's your actual class
public class HelloWorld{
//First, use this method to convert byte to String like a boss
static string ByteToString(byte[] buff)
{
string sbinary = "";
for (int i = 0; i < buff.Length; i++)
sbinary += buff[i].ToString("x2"); /* hex format */
return sbinary;
}
//Now let's get it started!
public static void Main(String []args){
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
//Your data
var apiSecret = "5432919427bd18884fc2a6e48b65dfba48fd9a1a46e3468b52fadbc6d6b463425";
var data = "payment_currency=USD&group_orders=0&count=100&nonce=1385689989977529";
var endPoint = "/info/orderbook";
String message = endPoint + Convert.ToChar(0) + data;
//Hash will be stored here
String hash = "";
//put your key at your custom 512
BillieJeansSHA512 hmacsha512 = new BillieJeansSHA512(encoding.GetBytes(apiSecret));
//hash'em all
byte[] result = hmacsha512.ComputeHash(encoding.GetBytes(message));
//convert bytes to string
hash = ByteToString(result);
//See it :)
Console.WriteLine(hash);
//Or if you using it at a web-page, this is it.
//Response.Write(hash)
//Now the easy part, convert it to base64
var bytesTo64 = System.Text.Encoding.UTF8.GetBytes(hash);
String hash64 = System.Convert.ToBase64String(bytesTo64);
}
}
……好吧,就是这样。 String hash
具有与 PHP 相同的哈希值:
41028bb90af31dc6e7fa100a8ceb1e220bfedf67ea723292db9a4e1c14f69c73adf30eeba61ab054cdc91c82f6be2f76dd602392be630b5e99b1f86da1460cbe
和 String hash64
具有与 PHP 相同的编码 base64 值:
NDEwMjhiYjkwYWYzMWRjNmU3ZmExMDBhOGNlYjFlMjIwYmZlZGY2N2VhNzIzMjkyZGI5YTRlMWMxNGY2OWM3M2FkZjMwZWViYTYxYWIwNTRjZGM5MWM4MmY2YmUyZjc2ZGQ2MDIzOTJiZTYzMGI1ZTk5YjFmODZkYTE0NjBjYmU=
关于c# - 具有 Convert.ToChar(0) 散列结果的字符串在散列 hash_hmac 时与 PHP 中的 chr(0) 不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20276909/