In IBM API Connect I am trying to use 'crypto' module in IBM API Connect gatewayscript. When I tested whether the crypto module is supported in gatewascript or not, I got the response as below
Code in Gatewayscript:
var crypto = require('crypto');
session.output.write(crypto);
Output:
*{
"getHashes": {},
"getCiphers": {},
"createHash": {},
"createHmac": {},
"createSign": {},
"createVerify": {},
"createCipheriv": {},
"createDecipheriv": {},
"randomBytes": {}
}*
But when I tried to make use of it, I got 500 Internal Server Error:
Code:
var crypto = require('crypto');
var key = "Alice";
var hmac = crypto.createHmac('hmac-sha256', key);
var input = "This is plaintext to hash";
var result = hmac.update(input).digest('base64');
session.output.write(result);
output:
{
"httpCode": "500",
"httpMessage": "Internal Server Error",
"moreInformation": "Internal Error"
}
Not sure where the things are going wrong. I am copy pasting exact example from IBM website. Here is the reference to crypto:https://www.ibm.com/support/knowledgecenter/SS9H2Y_7.7.0/com.ibm.dp.doc/crypto_js.html#crypto.createHmac
By using
var key = "Alice";
you tell the datapower to use the sharedkey stored with alias 'Alice'.If you want to use the 'Alice' string then you need to use a buffer like
var key = new Buffer("Alice");
Nevertheless it won't work as HMAC expects a 160 bits key for hmac-sha1. You can generate it like that
20 as 20 Bytes (20x8 bits=160 bits)
If you want to store it in a shared object you can follow what's describe here : http://rcbj.net/blog01/2012/03/17/generating-and-uploading-a-shared-key-symmetric-key-to-datapower-appliances/