The CreatorCon Call for Content is officially open! Get started here.

SHA256 encode data with key

Travis_C
Giga Contributor

I have an API I'm attempting to call from ServiceNow, but I am running into issues with the SHA256 encoded data. In the request to the API, the Authentication header contains SHA256 encoded that is then converted to base64. The API documentation has an example for Node.js of how to get this to work, but it uses Crypto-JS. Crypto-JS is not included in ServiceNow and I could add it in if needed, but I would prefer to use to use something else that is already built into ServiceNow. 

 

Node.js

var crypto = require("crypto");
var hex = crypto.createHmac("sha256", apiKey).update(data).digest("hex");
var signature = new Buffer(hex).toString('base64');

python

signature = base64.b64encode(hmac.new(apiKey.encode(),msg=data.encode(),digestmod=hashlib.sha256).hexdigest().encode())

Is there a way to SHA256 encode a message with a specific key and then base64 encode the output in ServiceNow? Or do I need to add Crypto-JS to ServiceNow?

1 ACCEPTED SOLUTION

Travis_C
Giga Contributor

Just a follow up on this. I was able to get this working by:

  • Created a new app for CryptoJS to be created in. This was required to get it working due to scoping.
  • Created script include for CryptoJS using the instructions provided in the video Crypto-JS into ServiceNow
  • Created a system property to store the apiKey in as a password.

At this point, you can call the function HmacSHA256 from the script include CryptoJS in the application NodeJS with an application scope of x_fise_nodejs. Example below:

  • var sig = (x_fise_nodejs.CryptoJS.HmacSHA256(requestVars, apiKey));

My specific use case also required base64 encoding, which can be completed with by using GlideStringUil.base64Encode. Example below:

  • var signature = GlideStringUtil.base64Encode(sig);

With this complete, signature and some other information was stored in a variable authSig input into an HTTP Auth header and using a RestMessage to send the API. RestMessagev2 documentation is here.

View solution in original post

16 REPLIES 16

Travis_C
Giga Contributor

Just a follow up on this. I was able to get this working by:

  • Created a new app for CryptoJS to be created in. This was required to get it working due to scoping.
  • Created script include for CryptoJS using the instructions provided in the video Crypto-JS into ServiceNow
  • Created a system property to store the apiKey in as a password.

At this point, you can call the function HmacSHA256 from the script include CryptoJS in the application NodeJS with an application scope of x_fise_nodejs. Example below:

  • var sig = (x_fise_nodejs.CryptoJS.HmacSHA256(requestVars, apiKey));

My specific use case also required base64 encoding, which can be completed with by using GlideStringUil.base64Encode. Example below:

  • var signature = GlideStringUtil.base64Encode(sig);

With this complete, signature and some other information was stored in a variable authSig input into an HTTP Auth header and using a RestMessage to send the API. RestMessagev2 documentation is here.

 Crypto-JS into ServiceNow video is unavailable now.

It seems that the CreatorCon 2017 video Crypto-JS into ServiceNow has been marked as private by ServiceNow. I'll go over what the video covers here.

 

The video shows how to get Crypto-JS into ServiceNow. The difficult process of this is to get the crypto-js module from Node-JS. To do this, install nodejs and npm on a linux box. Once that has been completed use npm to install crypto-js from the home directory (npm install crypto-js). Old versions required that you use grunt to create the full js file with all dependencies in it, but the newer version of crypto-js includes the complete js file already created.

File is located here: /home/user/node_modules/crypto-js/crypto-js.js

Once you have this file, you copy the data in the file and paste it into a script include. I recommend using a new application to store the the script include. 

It seems that the CreatorCon 2017 video Crypto-JS into ServiceNow has been marked as private by ServiceNow. I'll go over what the video covers here.

 

The video shows how to get Crypto-JS into ServiceNow. The difficult process of this is to get the crypto-js module from Node-JS. To do this, install nodejs and npm on a linux box. Once that has been completed use npm to install crypto-js from the home directory (npm install crypto-js). Old versions required that you use grunt to create the full js file with all dependencies in it, but the newer version of crypto-js includes the complete js file already created.

File is located here: /home/user/node_modules/crypto-js/crypto-js.js

Once you have this file, you copy the data in the file and paste it into a script include. I recommend using a new application to store the the script include. 

Travis_C
Giga Contributor

It seems that the CreatorCon 2017 video Crypto-JS into ServiceNow has been marked as private by ServiceNow. I'll go over what the video covers here.

 

The video shows how to get Crypto-JS into ServiceNow. The difficult process of this is to get the crypto-js module from Node-JS. To do this, install nodejs and npm on a linux box. Once that has been completed use npm to install crypto-js from the home directory (npm install crypto-js). Old versions required that you use grunt to create the full js file with all dependencies in it, but the newer version of crypto-js includes the complete js file already created.

File is located here: /home/user/node_modules/crypto-js/crypto-js.js

Once you have this file, you copy the data in the file and paste it into a script include. I recommend using a new application to store the the script include.