使用C#sha512散列与php API通信

使用C#sha512散列与php API通信,c#,php,json,C#,Php,Json,我有一个web应用程序,希望使用PHP开发的支付API。连接API的php代码运行良好,如下所示: //set variables $api = 'https://voguepay.com/api/'; $ref = time().mt_rand(0,999999); $task = 'pay'; $merchant_id = '6619-0048444'; $my_username = 'atah'; $merchant_email_on_voguepay = 'omec1010@yahoo

我有一个web应用程序,希望使用PHP开发的支付API。连接API的php代码运行良好,如下所示:

//set variables
$api = 'https://voguepay.com/api/';
$ref = time().mt_rand(0,999999);
$task = 'pay'; 
$merchant_id = '6619-0048444';
$my_username = 'atah';
$merchant_email_on_voguepay = 'omec1010@yahoo.com';
$ref = time().mt_rand(0,9999999);
$command_api_token = 'XPuz39v2RFzgdEUbPqcyTMg8HRkjGA';
$hash = hash('sha512',$command_api_token.$task.$merchant_email_on_voguepay.$ref);

$fields['task'] = $task;
$fields['merchant'] = $merchant_id;
$fields['ref'] = $ref;
$fields['hash'] = $hash;
$fields['amount'] = 20;
$fields['seller'] = 'omec1010@gmail.com';
$fields['memo'] = 'Payment '; 

$fields_string = 'json='.urlencode(json_encode($fields));


//open curl connection
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $api);
curl_setopt($ch,CURLOPT_HEADER, false); //we dont need the headers
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);// data coming back is put into a string
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,TRUE);
curl_setopt($ch,CURLOPT_MAXREDIRS,2);
$reply_from_voguepay = curl_exec($ch);//execute post
curl_close($ch);//close connection
下面是我连接API的C代码,但当我运行代码时,会收到错误消息,上面写着“无效哈希”。我觉得C#产生的哈希与PHP产生的哈希不同。 我的代码还可以通过哪种方式精确地从PHP散列中提取

class Program
{
    private const string task = "Pay";

    //Replace with your unique command api token
    private const string command_api_token = "XPuz39v2RFzgdEUbPqcyTMg8HRkjGA";

    //your registered email on voguepay
    private const string merchant_email_on_voguepay = "omec1010@yahoo.com";



    static void Main(string[] args)
    {

        Random rnd = new Random();
        string refl = string.Format("{0}{1}", (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds, new Random().Next(0, 9999999));
        byte[] hash_target = Encoding.Default.GetBytes(command_api_token + task + merchant_email_on_voguepay + refl);
        SHA512 sha512 = new SHA512CryptoServiceProvider();

        byte[] cryptString = sha512.ComputeHash(hash_target);
        string hashD = BitConverter.ToString(cryptString).Replace("-", String.Empty).ToUpper();


        //load all fields as json and serialize
        var keyValues = new Dictionary<string, string>
           {
                         { "task", "pay"},
                         { "merchant", "6619-0048444"},
                         { "ref",refl},
                         { "hash",hashD},
                         { "amount", "20"},
                         { "seller", "omec1010@gmail.com"},
                         { "remarks", "Payment"},
           };

        //serialization using Newtonsoft JSON 
        string json = JsonConvert.SerializeObject(keyValues);

        //url encode the json
        var postString = WebUtility.UrlEncode(json);

        //calling API with Restsharp
        var client = new RestClient("https://voguepay.com/api/");
        var request = new RestRequest(Method.POST);
        request.AddParameter("json", json);
        IRestResponse response = client.Execute(request);

        Console.WriteLine(response.Content);
        Console.ReadKey();


    }
}
类程序
{
private const string task=“Pay”;
//替换为您的唯一命令api令牌
私有常量字符串命令\u api\u token=“xpuz39v2rfzgduebpqcytmg8hrkjga”;
//您在voguepay上的注册电子邮件
private const string商户\u电子邮件\u on\u voguepay=”omec1010@yahoo.com";
静态void Main(字符串[]参数)
{
随机rnd=新随机();
string refl=string.Format(“{0}{1}”,(Int32)(DateTime.UtcNow.Subtract(new DateTime(1970,1,1))).TotalSeconds,new Random().Next(09999999));
byte[]hash\u target=Encoding.Default.GetBytes(命令\u api\u令牌+任务+商家\u电子邮件\u on\u voguepay+refl);
SHA512 SHA512=新的SHA512CryptoServiceProvider();
byte[]cryptString=sha512.ComputeHash(散列\目标);
string hashD=BitConverter.ToString(cryptString).Replace(“-”,string.Empty).ToUpper();
//将所有字段作为json加载并序列化
var keyValues=新字典
{
{“任务”、“支付”},
{“商人”,“6619-0048444”},
{“ref”,refl},
{“hash”,hash},
{“金额”,“20”},
{“卖方”omec1010@gmail.com"},
{“备注”,“付款”},
};
//使用Newtonsoft JSON进行序列化
字符串json=JsonConvert.SerializeObject(keyValues);
//url编码json
var postString=WebUtility.UrlEncode(json);
//用Restsharp调用API
var client=新的RestClient(“https://voguepay.com/api/");
var请求=新的重新请求(Method.POST);
AddParameter(“json”,json);
IRestResponse response=client.Execute(请求);
Console.WriteLine(response.Content);
Console.ReadKey();
}
}
注:我提供的参数值是真实的数据,我设置它是为了测试,没关系,你可以用它来检查我的工作