[英]Convert C# Hash Code to PHP for REST API Connection
Below is an excerpt from documentation for connecting to an API. 以下是用于连接到API的文档摘录。 I need to make this connection with PHP, but I am coming up short. 我需要使用PHP建立此连接,但是我要简短了。 Examples of what the public and private keys look like are: 公钥和私钥的示例如下:
The documentation with a C# example is below. 下面是带有C#示例的文档。 From what I have read, my issue appears to be a difference between the hashing operations in C# and PHP, but I can't seem to get the right combination going. 从我所读的内容来看,我的问题似乎是C#和PHP中的哈希运算之间的区别,但是我似乎找不到正确的组合。 Every attempt I make results in a response from the API of 'Invalid Username or Password'. 我的每一次尝试都会导致来自“无效的用户名或密码” API的响应。 My keys being returned from hash_hmac('sha256', $data, $privateKey) are lowercase and look nothing like the example. 我从hash_hmac('sha256',$ data,$ privateKey)返回的密钥是小写的,看起来与示例不一样。 I understand the UTF8 and Base64 encoding requirements, but the hashing is getting me. 我了解UTF8和Base64编码要求,但是散列让我感到困惑。 Any help is greatly appreciated. 任何帮助是极大的赞赏。 Excerpt below: 摘录如下:
Authorization Token 授权令牌
The authorization token consists of {Rep Public Key}:{Request Signature} (color coded for emphasis). 授权令牌由{Rep Public Key}:{Request Signature}(颜色编码为强调)组成。 The public key identifies the user making the request. 公钥标识发出请求的用户。 The request signature is a secure, system-generated string similar to a password, consisting of several pieces of information about the request and encrypted using HMAC SHA-256 and the rep's private key. 请求签名是一个安全的,系统生成的字符串,类似于密码,由关于请求的几条信息组成,并使用HMAC SHA-256和代表的私钥进行了加密。 Upon receiving the request, the API attempts to match the signature. 收到请求后,API会尝试匹配签名。
Create an Authorization Token 创建授权令牌
Follow these steps to encrypt the request signature and rep public key: 请按照以下步骤加密请求签名和rep公钥:
Encryption Code in .NET C# .NET C#中的加密代码
var encoding = new System.Text.UTF8Encoding();
var keyByte = encoding.GetBytes(secret);
var messageBytes = encoding.GetBytes(message);
using (var hmacsha256 = new HMACSHA256(keyByte))
{
var hashmessage = hmacsha256.ComputeHash(messageBytes);
return Convert.ToBase64String(hashmessage);
}
Resolved. 解决。 Step three should have a second step. 第三步应该有第二步。 You have to Base64 encode the signature string, too. 您也必须对Base64编码签名字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.