Generate Signature using MD5 or SHA256

Generate Signature using MD5 or SHA256

book

Article ID: KB0071730

calendar_today

Updated On:

Products Versions
TIBCO Cloud API Management ALL

Description

Invoking an API sometimes needs a Signature to be passed as part of the request.
Signature is MD5 or SHA256 hashed and is a concatenation of -
  1. Client ID
  2. Client Secret
  3. Epoch time (rounded off to seconds as sample below)

Issue/Introduction

Generate Signature using MD5 or SHA256 in Postman and curl

Environment

ALL

Resolution

Generating Signature in Postman -
Example API Call - 
curl -X GET \
  'http://xxx.api.mashery.com/bhavani4?apiKey=<apiKey>&sig=588ab8ecbd5f7c435c0e3b14e1915fb2'
Here 'sig' query parameter is generated in Postman through Pre-request Script. Below is the script -
var mymd5str = CryptoJS.SHA256("<clientID>" + "<clientSecret>" + Math.round((new Date()).getTime()/1000));
// for MD5, use CryptoJS.MD5
postman.clearGlobalVariable("signature");
postman.setGlobalVariable("signature", mymd5str);
URL to be used in Postman - http://xxx.api.mashery.com/bhavani4?apiKey=<apiKey>&sig={{signature}}
please note the {{signature}} element, it comes from the global variable defined in pre-request script.

Generating Signature with curl -
md5-
curl 'http://xxx.api.mashery.com/bhavani4?apiKey=<apiKey>&sig='$(php -r 'echo hash("md5","<clientID>"."clientSecret".time());')
sha256-
curl 'http://xxx.api.mashery.com/bhavani4?apiKey=<apiKey>&sig='$(php -r 'echo hash("sha256","<clientID>"."clientSecret".time());')