以下是生成基于sha256的哈希的过程。
生成的哈希
哈希查询字符串参数后跟该特定请求的生成哈希。生成哈希:
例如,假设提供的API机密为“ secretapikey”,并且HTTP POST正文包含以下内容:
{ "apiKey": 123, "invoiceId": 1 }
生成的哈希将是:
d48cf8a852713844603d7c8cbefb3e81cfb29e7540d98f06affdf58322c1038e
以下是生成上面生成的哈希的步骤:
HTTP POST正文=> { "apiKey": 123, "invoiceId": 1 }
秘密=> secretapikey
要散列的文本=> { "apiKey": 123, "invoiceId": 1 }secretapikey
SHA-256哈希=> d48cf8a852713844603d7c8cbefb3e81cfb29e7540d98f06affdf58322c1038e
我必须合并两个字符串({ "apiKey": 123, "invoiceId": 1 }secretapikey
),然后对其进行哈希处理以发送到api端点。但是,以下代码生成的哈希值与在线sha256生成的哈希值不同:-
$secretapikey = "secretapikey";
$postbody = array();
$postbody['apiKey'] = "123";
$postbody['invoiceId'] = 1;
$jpb = json_encode($postbody);
$hashed = $jpb.$secretapikey; //Here is Problem. It is not concatenated according to requirement
$result = hash('SHA256', $hashed);
echo $result;
这是$result
d2c5d184be42ff4ae3a0046d0727c026f38c1e92f8960cb9d17d496c7b89b7b3
应该是
d48cf8a852713844603d7c8cbefb3e81cfb29e7540d98f06affdf58322c1038e
答案 0 :(得分:2)
hash('SHA256', $hashed);
做得很好
和$hashed = $jpb.$secretapikey;
正确连接了两个字符串。
未获得期望的哈希值的原因是用于测试的JSON是
{ "apiKey": 123, "invoiceId": 1 }
json_encode($postbody);
是{"apiKey":123,"invoiceId":1}
没有空格。