字符串比PHP CHR(0)不同Convert.ToChar(0)的散列结果散列时hash_hmac [英] String with Convert.ToChar(0) hash result different than chr(0) in PHP when hashed hash_hmac

查看:263
本文介绍了字符串比PHP CHR(0)不同Convert.ToChar(0)的散列结果散列时hash_hmac的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP中的字符串,它被转换为字节数组和散列

I have a string in PHP which is being converted to a byte array and hashed.

的字符串转换为字节数组如下:

The string being converted to the byte array looks like:

G。字符(0)。 便便;

"g". chr(0) . "poo";

我需要相当的字节数组在C#中,所以我可以得到相同的哈希值。

I need to equivalent byte array in C# so i can get the same hash..

编辑:这里是完整的问题,得到的散列是不一样的。

Here is the FULL problem, resulting hash is not the same.

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#

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编码,如:

I've tried different base64 encoding like:

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的散列的ByteArray (0)PHP使用。

but ultimately the issue appears to be the byteArray that is hashed because of the chr(0) php uses.

推荐答案

我再回答了,因为你已经改变了整个问题,所以有一个新的解决方案为您的新问题。

I'm answering it again, because you have changed the whole question, so there's a new solution for your new question.

首先,HMACSHA512不会产生相同的结果作为你的PHP代码。顺便说一句,生成你的PHP哈希值:

First, HMACSHA512 won't give the same result as your php code. By the way, your PHP generated hash is:

41028bb90af31dc6e7fa100a8ceb1e220bfedf67ea723292db9a4e1c14f69c73adf30eeba61ab054cdc91c82f6be2f76dd602392be630b5e99b1f86da1460cbe

要在C#中相同的结果,我创建了 BillieJeansSHA512 类,使散列等于为PHP。此外,而不是使用 encoding.GetBytes 转换为字节字符串我已经创建的方法[] ByteToString 来正确地转换。

To make the same result in C#, I created the BillieJeansSHA512 class to make the hash equals as PHP. Also instead using encoding.GetBytes to convert byte[] to String I have created the method ByteToString to convert properly.

噢,下面的代码是不是简单的PHP,但我挑战你或任何人使用相同的PHP的hash做这个简单!你敢,我DOUBLE敢!让我们去的代码:

Ow, the following code is not simple as PHP, but I challenge you or anyone to do this simpler with the same PHP's hash! I dare you, I DOUBLE DARE YOU! Let's go to the code:

//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);
    }
}



...嗯,这是它。 字符串哈希具有相同的哈希作为PHP所做的:

...well, this is it. String hash have the same hash as PHP does:

41028bb90af31dc6e7fa100a8ceb1e220bfedf67ea723292db9a4e1c14f69c73adf30eeba61ab054cdc91c82f6be2f76dd602392be630b5e99b1f86da1460cbe

字符串hash64 具有相同的base64编码值PHP做:

and String hash64 has the same encoded base64 value that PHP does:

NDEwMjhiYjkwYWYzMWRjNmU3ZmExMDBhOGNlYjFlMjIwYmZlZGY2N2VhNzIzMjkyZGI5YTRlMWMxNGY2OWM3M2FkZjMwZWViYTYxYWIwNTRjZGM5MWM4MmY2YmUyZjc2ZGQ2MDIzOTJiZTYzMGI1ZTk5YjFmODZkYTE0NjBjYmU=

这篇关于字符串比PHP CHR(0)不同Convert.ToChar(0)的散列结果散列时hash_hmac的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆