I have the following code in C#
var apiKey = "SBB3aWxsIG1ha2UgbXbcQVBJIHN|Y3VyZQ==";
var apiSecret = "QaTW3xlf1U5ljdlAJSdltzT71fFF+eZ=";
var key = Convert.FromBase64String(apiSecret);
var provider = new System.Security.Cryptography.HMACSHA256(key);
var hash = provider.ComputeHash(Encoding.UTF8.GetBytes(apiKey));
var signature = Convert.ToBase64String(hash);
I am trying to get the same result in Javascript using the CryptJS library but from what i can tell i am not converting the key and secret to byte arrays and the encoding is incorrect. first try looks like:
var apiKey = "SBB3aWxsIG1ha2UgbXbcQVBJIHN|Y3VyZQ==";
var apiSecret = "QaTW3xlf1U5ljdlAJSdltzT71fFF+eZ=";
var hash = CryptoJS.HmacSHA256(apiKey, apiSecret);
var sig = hash.toString(CryptoJS.enc.Base64);
I have the following code in C#
var apiKey = "SBB3aWxsIG1ha2UgbXbcQVBJIHN|Y3VyZQ==";
var apiSecret = "QaTW3xlf1U5ljdlAJSdltzT71fFF+eZ=";
var key = Convert.FromBase64String(apiSecret);
var provider = new System.Security.Cryptography.HMACSHA256(key);
var hash = provider.ComputeHash(Encoding.UTF8.GetBytes(apiKey));
var signature = Convert.ToBase64String(hash);
I am trying to get the same result in Javascript using the CryptJS library but from what i can tell i am not converting the key and secret to byte arrays and the encoding is incorrect. first try looks like:
var apiKey = "SBB3aWxsIG1ha2UgbXbcQVBJIHN|Y3VyZQ==";
var apiSecret = "QaTW3xlf1U5ljdlAJSdltzT71fFF+eZ=";
var hash = CryptoJS.HmacSHA256(apiKey, apiSecret);
var sig = hash.toString(CryptoJS.enc.Base64);
Share
Improve this question
edited Aug 16, 2016 at 19:02
billy jean
asked Aug 12, 2016 at 22:11
billy jeanbilly jean
1,4194 gold badges27 silver badges46 bronze badges
7
|
Show 2 more comments
1 Answer
Reset to default 21 +50Inspire by https://stackoverflow.com/a/13837543/1810391
Javascript
var CryptoJS = require('crypto-js');
var apiKey = "SBB3aWxsIG1ha2UgbXbcQVBJIHN|Y3VyZQ==";
var apiSecret = "QaTW3xlf1U5ljdlAJSdltzT71fFF+eZ=";
// var key = Convert.FromBase64String(apiSecret);
var key = CryptoJS.enc.Base64.parse(apiSecret);
console.log('key:' + key);
// var prehash = Encoding.UTF8.GetBytes(apiKey);
var prehash = CryptoJS.enc.Utf8.parse(apiKey);
console.log('Pre-hash:' + prehash);
// var provider = new System.Security.Cryptography.HMACSHA256(key);
// var hash = provider.ComputeHash(prehash);
var hash = CryptoJS.HmacSHA256(prehash, key);
console.log('hash:' + hash);
//var signature = Convert.ToBase64String(hash);
var signature = hash.toString(CryptoJS.enc.Base64);
console.log('signature:' + signature);
Javascript Output
key:41a4d6df195fd54e658dd940252765b734fbd5f145f9e6
Pre-hash:53424233615778734947316861325567625862635156424a49484e7c593356795a513d3d
hash:ecb6cdf5dd39872bb2cbce4321e2725e11b99c01af9c2a620ebbaf3d8d8607e7
signature:7LbN9d05hyuyy85DIeJyXhG5nAGvnCpiDruvPY2GB+c=
C#
using System;
using System.Text;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
var apiKey = "SBB3aWxsIG1ha2UgbXbcQVBJIHN|Y3VyZQ==";
var apiSecret = "QaTW3xlf1U5ljdlAJSdltzT71fFF+eZ=";
var key = Convert.FromBase64String(apiSecret);
Console.Write("key:");
prtByte(key);
Console.Write("Pre-hash:");
prtByte(Encoding.UTF8.GetBytes(apiKey));
var provider = new System.Security.Cryptography.HMACSHA256(key);
var hash = provider.ComputeHash(Encoding.UTF8.GetBytes(apiKey));
Console.Write("hash:");
prtByte(hash);
var signature = Convert.ToBase64String(hash);
Console.WriteLine("signature:" + signature);
}
public static void prtByte(byte[] b)
{
for (var i = 0; i < b.Length; i++)
{
Console.Write(b[i].ToString("x2"));
}
Console.WriteLine();
}
}
}
C# Output
key:41a4d6df195fd54e658dd940252765b734fbd5f145f9e6
Pre-hash:53424233615778734947316861325567625862635156424a49484e7c593356795a513d3d
hash:ecb6cdf5dd39872bb2cbce4321e2725e11b99c01af9c2a620ebbaf3d8d8607e7
signature:7LbN9d05hyuyy85DIeJyXhG5nAGvnCpiDruvPY2GB+c=
apiSecret
in CryptoJS. Even worse, you forgot to passapiKey
andapiSecret
toCryptoJS.HmacSHA256
entirely. – Artjom B. Commented Aug 12, 2016 at 22:27CryptoJS.HmacSHA256
? – Artjom B. Commented Aug 13, 2016 at 15:38"apiKey"
rather than the variableapiKey
to the HmacSHA256 function. – Heretic Monkey Commented Aug 16, 2016 at 18:42