简体   繁体   English

将C#哈希代码转换为PHP以进行REST API连接

[英]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: 公钥和私钥的示例如下:

  • Public Key: 9cd52302-85cf-406d-98df-d3e19eea907c 公钥: 9cd52302-85cf-406d-98df-d3e19eea907c
  • Private Key: 7cd0e56a-a62f-4f05-ab8c-e1a2255c9732 私钥: 7cd0e56a-a62f-4f05-ab8c-e1a2255c9732

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公钥:

  1. Apply UTF8 encoding to the rep private key and request signature, ie {HTTP Method}|{URI}|{Time Stamp}. 将utf8编码应用于rep私钥和请求签名,即{HTTP方法} | {URI} | {时间戳记}。
  2. Convert the signature string to lower case. 将签名字符串转换为小写。
  3. Encrypt the signature string using HMAC SHA-256 and the rep's private key. 使用HMAC SHA-256和代表的私钥对签名字符串进行加密。
    • Example: get|/api/accounts/1052932|thu, 18 jun 2015 07:25:43 gmt 示例: get | / api / accounts / 1052932 | thu,2015年6月18日07:25:43 gmt
    • Becomes: RVg3YCtybLImF68rEumuPLCCApLnPIyNvb0hfxH+ek4= 成为: RVg3YCtybLImF68rEumuPLCCApLnPIyNvb0hfxH + ek4 =
  4. Apply Base64 encoding to the {Rep Public Key}:{Encrypted Request Signature} string. 将Base64编码应用于{Rep Public Key}:{Encrypted Request Signature}字符串。
    • Example: 3383259B-9FBA-486E-97A8-E81893741F83:RVg3YCtybLImF68rEumuPLCCApLnPIyNvb 0hfxH+ek4= 例如: 3383259B-9FBA-486E-97A8-E81893741F83:RVg3YCtybLImF68rEumuPLCCApLnPIyNvb 0hfxH + ek4 =
    • Becomes: MzM4MzI1OUItOUZCQS00ODZFLTk3QTgtRTgxODkzNzQxRjgzOlJWZzNZQ3R5YkxJbU Y2OHJFdW11UExDQ0FwTG5QSXlOdmIwaGZ4SCtlazQ9 成为: MzM4MzI1OUItOUZCQS00ODZFLTk3QTgtRTgxODkzNzQxRjgzOlJWZzNZQ3R5YkxJbU Y2OHJFdW11UExDQ0FwTG5QSXlOdmIwaGZ4SCtlazQ9

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM