Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SHA-1 dynamic key in Postman

I'm trying to use Postman to send a GET http request which contains a parameter which is dynamically generated by taking the full request query string (everything to the right of the question mark in the URL, after URL encoding), concatenating a previously assigned shared secret key, and then performing a SHA-1 hash of the resulting string.

I would use a Pre-request Script to achieve this.

Thank you.

like image 670
Boogieman Avatar asked May 19 '16 21:05

Boogieman


People also ask

How to generate hash in Postman?

You can use Crypto JS's MD5 function, within postman directly CryptoJS. MD5(message). toString(); . Also, you can check the Postman collection on Crypto JS for more information.

How do you submit a signature to the Postman?

In Postman, go to Signature Requests → Get Signature Request, enter your signature_request_id , and press “Send”.


1 Answers

I actually found a solution and would like to share it.

var params = [
    ["client_id", "222"]
    ,["account_id", ""]
];

// Build the request body string from the Postman request.data object
var requestBody = "";
var firstpass = true;
for(var i=0;i < params.length; i++) {
        if(!firstpass){
            requestBody += "&";
        }
        requestBody += params[i][0] + "=" + params[i][1];
        firstpass = false;
        postman.setGlobalVariable(params[i][0], params[i][1]);
}
requestBody += postman.getEnvironmentVariable("sharedSecretKey");
postman.setGlobalVariable("requestBody", requestBody);

var mac = "";
if(requestBody){
    // SHA1 hash
    mac = CryptoJS.SHA1(requestBody);
}

postman.setGlobalVariable("mac", mac);

Then I just need to set the parameters in the URL : {{baseUrl}}/get?client_id={{client_id}}&account_id={{account_id}}&mac={{mac}}

where {{baseUrl}} is an environment variable and {{client_id}}, {{account_id}} are global variables

Hope it can be helpful to someone.

Thank you.

like image 187
Boogieman Avatar answered Sep 24 '22 02:09

Boogieman