It is very important that we generate HMACs in the same way so that the hashing process results in repeatable outputs. To that end here are some samples in different languages of how we generate the HMACs on our end:
JavaScript
const crypto = require('crypto'); const hmac = crypto.createHmac('sha256', 'secret'); hmac.update('Message'); console.log(hmac.digest('hex')); // aa747c502a898200f9e4fa21bac68136f886a0e27aec70ba06daf2e2a5cb5597 |
Java
import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; public class Test { public static void main(String[] args) { try { String secret = "secret"; String message = "Message"; Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256"); sha256_HMAC.init(secret_key); byte[] hash = (sha256_HMAC.doFinal(message.getBytes())); StringBuffer result = new StringBuffer(); for (byte b : hash) { result.append(String.format("%02x", b)); } System.out.println(result.toString()); // aa747c502a898200f9e4fa21bac68136f886a0e27aec70ba06daf2e2a5cb5597 } catch (Exception e){ System.out.println("Error"); } } } |
C#
using System.Security.Cryptography; namespace Test { public class MyHmac { public static void Main(string[] args){ var hmac = new MyHmac (); System.Console.WriteLine(hmac.CreateToken ("Message", "secret")); // AA747C502A898200F9E4FA21BAC68136F886A0E27AEC70BA06DAF2E2A5CB5597 } private string CreateToken(string message, string secret) { secret = secret ?? ""; var encoding = new System.Text.ASCIIEncoding(); byte[] keyByte = encoding.GetBytes(secret); byte[] messageBytes = encoding.GetBytes(message); using (var hmacsha256 = new HMACSHA256(keyByte)) { byte[] hashmessage = hmacsha256.ComputeHash(messageBytes); var sb = new System.Text.StringBuilder(); for (var i = 0; i <= hashmessage.Length - 1; i++) { sb.Append(hashmessage[i].ToString("X2")); } return sb.ToString(); } } } } |
PHP
echo hash_hmac('sha256', 'Message', 'secret'); |
Ruby require 'openssl' OpenSSL::HMAC.hexdigest('sha256', "secret", "Message") # aa747c502a898200f9e4fa21bac68136f886a0e27aec70ba06daf2e2a5cb5597 |
Do you use Intercom?We implement the same algorithms as intercom so you can use the same hashing techniques for connecting to their systems as ours. |
Comments
0 comments
Article is closed for comments.