Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

ServiceNow JS equivalent of the Python SHA256 hash with key?

Mosh Jah_n1
Giga Expert
I am trying to do the ServiceNow JS equivalent of the following Python code:

hmac = hmac.new("key".encode(), "message".encode(), digestmod=hashlib.sha256).hexdigest()


I thought it would be this, but it's not resulting in the same hash as Python:

var encryption = new GlideCertificateEncryption();
var digest = new GlideDigest();
var util = new GlideStringUtil();

var key = utf8Encode("key");  // utf8Encode() is my own function which is working fine
var message = utf8Encode("message");

var signatureMacBase64 = encryption.generateMac(util.base64Encode(key), "HmacSHA256", message);
var signatureDigest = digest.getSHA256Hex(signatureMacBase64);

 

The output I'm expecting (for "key" and "message" as inputs) in hex is:

6e9ef29b75fffc5b7abae527d58fdadb2fe42e7219011976917343065f58ed4a


Any help appreciated!

1 ACCEPTED SOLUTION

Mosh Jah_n1
Giga Expert

I solved it:

var HexUtil = 
{
  convertByteArrayToHex : function(byteArray)
  {
    var hex = "";

 

    byteArray.forEach(function(byteValue) { hex += HexUtil.convertByteToHex(byteValue); });

 

    return hex;  
  },

 

  convertByteToHex : function(b) 
  {
    var hexChar = ["0", "1", "2", "3", "4", "5", "6", "7","8", "9", "a", "b", "c", "d", "e", "f"];
  
    return hexChar[(b >> 4) & 0x0f] + hexChar[b & 0x0f];
  }
};

 

var key = "key";
key = encodeURIComponent(key);
key = GlideStringUtil.base64Encode(key);

 

var msg = "message";
msg = encodeURIComponent(msg);

 

var mac = new GlideCertificateEncryption();
signature = mac.generateMac(key, "HmacSHA256", msg);
gs.print(signature);

 

// Yes! bp7ym3X//Ft6uuUn1Y/a2y/kLnIZARl2kXNDBl9Y7Uo=

 

var bytes = GlideStringUtil.base64DecodeAsBytes(signature);
gs.print(JSON.stringify(bytes));
gs.print(bytes.length);

 

// Yes! [110,-98,-14,-101,117,-1,-4,91,122,-70,-27,39,-43,-113,-38,-37,47,-28,46,114,25,1,25,118,-111,115,67,6,95,88,-19,74]

 

var hex = HexUtil.convertByteArrayToHex(bytes);
gs.print(hex);

 

// Yes! 6e9ef29b75fffc5b7abae527d58fdadb2fe42e7219011976917343065f58ed4a

 

var hexB64 = GlideStringUtil.base64Encode(hex);
gs.print(hexB64);

 

// Yes! NmU5ZWYyOWI3NWZmZmM1YjdhYmFlNTI3ZDU4ZmRhZGIyZmU0MmU3MjE5MDExOTc2OTE3MzQzMDY1ZjU4ZWQ0YQ==

View solution in original post

1 REPLY 1

Mosh Jah_n1
Giga Expert

I solved it:

var HexUtil = 
{
  convertByteArrayToHex : function(byteArray)
  {
    var hex = "";

 

    byteArray.forEach(function(byteValue) { hex += HexUtil.convertByteToHex(byteValue); });

 

    return hex;  
  },

 

  convertByteToHex : function(b) 
  {
    var hexChar = ["0", "1", "2", "3", "4", "5", "6", "7","8", "9", "a", "b", "c", "d", "e", "f"];
  
    return hexChar[(b >> 4) & 0x0f] + hexChar[b & 0x0f];
  }
};

 

var key = "key";
key = encodeURIComponent(key);
key = GlideStringUtil.base64Encode(key);

 

var msg = "message";
msg = encodeURIComponent(msg);

 

var mac = new GlideCertificateEncryption();
signature = mac.generateMac(key, "HmacSHA256", msg);
gs.print(signature);

 

// Yes! bp7ym3X//Ft6uuUn1Y/a2y/kLnIZARl2kXNDBl9Y7Uo=

 

var bytes = GlideStringUtil.base64DecodeAsBytes(signature);
gs.print(JSON.stringify(bytes));
gs.print(bytes.length);

 

// Yes! [110,-98,-14,-101,117,-1,-4,91,122,-70,-27,39,-43,-113,-38,-37,47,-28,46,114,25,1,25,118,-111,115,67,6,95,88,-19,74]

 

var hex = HexUtil.convertByteArrayToHex(bytes);
gs.print(hex);

 

// Yes! 6e9ef29b75fffc5b7abae527d58fdadb2fe42e7219011976917343065f58ed4a

 

var hexB64 = GlideStringUtil.base64Encode(hex);
gs.print(hexB64);

 

// Yes! NmU5ZWYyOWI3NWZmZmM1YjdhYmFlNTI3ZDU4ZmRhZGIyZmU0MmU3MjE5MDExOTc2OTE3MzQzMDY1ZjU4ZWQ0YQ==