Below is an excerpt from documentation for connecting to an API. I need to make this connection with PHP, but I am coming up short. Examples of what the public and private keys look like are:
- Public Key: 9cd52302-85cf-406d-98df-d3e19eea907c
- Private Key: 7cd0e56a-a62f-4f05-ab8c-e1a2255c9732
The documentation with a C# example is below. 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. Every attempt I make results in a response from the API of 'Invalid Username or Password'. My keys being returned from hash_hmac('sha256', $data, $privateKey) are lowercase and look nothing like the example. I understand the UTF8 and Base64 encoding requirements, but the hashing is getting me. Any help is greatly appreciated. Excerpt below:
Authorization Token
The authorization token consists of {Rep Public Key}:{Request Signature} (color coded for emphasis). 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. Upon receiving the request, the API attempts to match the signature.
Create an Authorization Token
Follow these steps to encrypt the request signature and rep public key:
- Apply UTF8 encoding to the rep private key and request signature, i.e. {HTTP Method}|{URI}|{Time Stamp}.
- Convert the signature string to lower case.
- Encrypt the signature string using HMAC SHA-256 and the rep's private key.
- Example: get|/api/accounts/1052932|thu, 18 jun 2015 07:25:43 gmt
- Becomes: RVg3YCtybLImF68rEumuPLCCApLnPIyNvb0hfxH+ek4=
- Apply Base64 encoding to the {Rep Public Key}:{Encrypted Request Signature} string.
- Example: 3383259B-9FBA-486E-97A8-E81893741F83:RVg3YCtybLImF68rEumuPLCCApLnPIyNvb 0hfxH+ek4=
- Becomes: MzM4MzI1OUItOUZCQS00ODZFLTk3QTgtRTgxODkzNzQxRjgzOlJWZzNZQ3R5YkxJbU Y2OHJFdW11UExDQ0FwTG5QSXlOdmIwaGZ4SCtlazQ9
Encryption Code in .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);
}