Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Replacement for Crypto library

Avatar

Community Advisor

Hi everyone,

I am looking for similar function on AC for Crypto library.

So, this piece of code should be adapted/translated to run on AC Classic:

...

const crypto = require('crypto');

sig = crypto.createHmac('sha1', appsecret).update(text).digest('bin');

...

I have found this AC function below but not sure is that good replacement.

1861084_pastedImage_0.png

Any suggestion?

Regards,

Milan

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi,

You may use hmac as follow (hmacStr is deprecated):

 

var appsecret = "SECRET"
var text = "hello"
//var sig = hmacStr(text, appsecret, 'SHA1'); // @deprecated
var mbKey = new MemoryBuffer(); // init memory buffer for the key
mbKey.fromString(appsecret, "utf-8"); // convert from string
var mbSignature = new MemoryBuffer(); // init memory buffer for the signature
mbSignature.fromString(text, "utf-8"); // convert from string
var sig = mbSignature.hmac(mbKey, 'SHA1'); // compute SHA-1 HMAC with key
logInfo('text:', text); // "hello"
logInfo('sig:', sig); // rAqO5eBQ+Z8pPOhAPsaztYNsKxQ=

 

 

However, it's not the digest in hexadecimal format as in nodeJS:

20200929-133851-screenshot-18.jpg

 

To do get the hex hmac, you have to use an external library, like jshashes:

loadLibrary('vendor:jshashes_1.0.8');
var appsecret = "SECRET";
var text = "hello";
var SHA1 = new Hashes.SHA1;
logInfo('SHA1: ' + SHA1.hex_hmac(appsecret, text)); // "SHA1: ac0a8ee5e050f99f293ce8403ec6b3b5836c2b14"

Follow this link to import a js library in adobe campaign.

Best

Florian

View solution in original post

0 Replies

Avatar

Correct answer by
Community Advisor

Hi,

You may use hmac as follow (hmacStr is deprecated):

 

var appsecret = "SECRET"
var text = "hello"
//var sig = hmacStr(text, appsecret, 'SHA1'); // @deprecated
var mbKey = new MemoryBuffer(); // init memory buffer for the key
mbKey.fromString(appsecret, "utf-8"); // convert from string
var mbSignature = new MemoryBuffer(); // init memory buffer for the signature
mbSignature.fromString(text, "utf-8"); // convert from string
var sig = mbSignature.hmac(mbKey, 'SHA1'); // compute SHA-1 HMAC with key
logInfo('text:', text); // "hello"
logInfo('sig:', sig); // rAqO5eBQ+Z8pPOhAPsaztYNsKxQ=

 

 

However, it's not the digest in hexadecimal format as in nodeJS:

20200929-133851-screenshot-18.jpg

 

To do get the hex hmac, you have to use an external library, like jshashes:

loadLibrary('vendor:jshashes_1.0.8');
var appsecret = "SECRET";
var text = "hello";
var SHA1 = new Hashes.SHA1;
logInfo('SHA1: ' + SHA1.hex_hmac(appsecret, text)); // "SHA1: ac0a8ee5e050f99f293ce8403ec6b3b5836c2b14"

Follow this link to import a js library in adobe campaign.

Best

Florian