Problem getting HmacSHA512 with CryptoJS to work

Simon Larsson
Tera Contributor

Hello,

I've tried to implement some variant of CryptoJS for making an authentication token. The problem is when i run the code snippet below I encounter this error:

Evaluator.evaluateString() problem: java.lang.SecurityException: Encountered scriptable of unknown type Object

 

var key = "sample_key"; 
var secret = CryptoJS.enc.Base64.parse(key);
var str = CryptoJS.HmacSHA512("sample_data", secret);
str = CryptoJS.enc.Base64.stringify(str);

I think the error is occuring when trying to create the HmacSHA512 encoding. The code works fine running in a node.js environment.
I have created a Script Include called CryptoJS with the code attached in the file.
find_real_file.png

Anyone have some experience working with HmacSHA512, any help is appriciated.

1 ACCEPTED SOLUTION

Not sure why that is. However I'll attach your file modified by me as described; I've tested it in both San Diego and Quebec. I assume if it works in those two, it should also work in Rome.

View solution in original post

11 REPLIES 11

Got it working, i could not be running the script from an scoped application. So i have to move the workflow to global to be able to run it. Thank you for the help

Your welcome! But not working from scope is strange. I did run all those examples in a scope. In fact I haven't tried running it in global :-). Perhaps you configured it to only be accessible from its own scope and tried to call it from a different scope?

 

Dirk Kruger
Tera Contributor

I am encountering the same issue on line 68, I need to generate a signature using the following function:

gs.info(getSignatureKey('xxxxxxxxxxxxxx', '20220530', 'us-east-1', 'iam'));


function getSignatureKey(key, dateStamp, regionName, serviceName) {
    var kDate = global.CryptoJS.HmacSHA256(dateStamp, "AWS4" + key);
    var kRegion = global.CryptoJS.HmacSHA256(regionName, kDate);
    var kService = global.CryptoJS.HmacSHA256(serviceName, kRegion);
    var kSigning = global.CryptoJS.HmacSHA256("aws4_request", kService);
    return kSigning;
}
 
My result is:
Javascript compiler exception: Primitive type expected (had org.mozilla.javascript.Undefined instead) (sys_script_include.d955951db5b745102ed6e29bdb2d14e6.script; line 68) in:
var CryptoJS = Class.create();

Any suggestions would be greatly appreciated

pavan_yakkala1
ServiceNow Employee
ServiceNow Employee

Hello  @-O- , It looks great to see this script is working as expected. 

I do have a similar requirement and trying to generate the str by following the steps

1. Created script include with name CryptoJS by copying the text from your file into script part.

2. Executed the below script from Background scripts

var key = "sample_key";
var secret = CryptoJS.enc.Base64.parse(key);
var str = CryptoJS.HmacSHA512("sample_data", secret);
str = CryptoJS.enc.Base64.stringify(str);
gs.info("str::"+str);

 

Unfortunately, below is the error received at line 1927. It would be great help if you can guide us to have it implemeted. Thanks in advance!

 

Script execution error: Script Identifier: null.null.script, Error Description: Cannot read property "0" from undefined, Script ES Level: 0
Evaluator: com.glide.script.RhinoEcmaError: Cannot read property "0" from undefined script : Line(1927) column(0)

 

pavan_yakkala1_0-1733041986184.png

 

Have tried to "convert" the original library to a Script Include/ServiceNow "friendly" format.

Maybe it is useful.

It DOES NOT contain all the functionality in the original library; it only contains minimal SHA256 and SHA512 HMAC "support".

For something like:

(
	function () {
		var C = global.CryptoLib

		var key = 'SecretKey01@';
		var hasher = new C.algo.SHA512();
		var hmac = new C.algo.HMAC(hasher, key);

		var data = hmac.finalize('Lorem Ipsum...');

		gs.debug(data.toString());
		gs.debug(data.toString(C.enc.Base64));
	}
)();