crypto module is not working in IBM API Connect

1k views Asked by At

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

1

There are 1 answers

0
Valentin C. On

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

$ dd if=/dev/random count=20 bs=1 | xxd -ps
a73e3406e7dcc5fc168d9ae9954ec6e0d85e4444

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/

Put the hex string generated by this command into a file called secret.key. Upload the key to the cert:/// directory on the appliance. Navigate to Objects->Crypto Configuration->Crypto Shared Secret Key. Click Add. Enter a name for the shared key. From the drop down, chose the secret.key file that was uploaded a moment ago. Click Apply. If no errors are displayed, the key was successfully read. Click Save.