thinker
thinker

Reputation: 176

how to set hash in Postman Pre-Request Script for Marvel API

I have a pre-request script that I gathered from another post on StackOverflow, but I'm still getting invalid credentials.

Attempted to do this just with str_1 but it's not working. Not sure what request.data is supposed to do as it keeps returning NaN. I think that the problem might be there, but still at a loss. I've attempted converting all variables to a string, but that still returned the same error.

URL = https://gateway.marvel.com/v1/public/characters?ts={{timeStamp}}&apikey={{apiKey}}&hash={{hash}}

// Access your env variables like this
var ts = new Date();
ts = ts.getUTCMilliseconds(); 

var str_1 = ts + environment.apiKey + environment.privateKey;  

// Or get your request parameters
var str_2 = request.data["timeStamp"] + request.data["apiKey"];
console.log('str_2 = ' + str_2); 

// Use the CryptoJS
var hash = CryptoJS.MD5(str_1).toString(); 

// Set the new environment variable

pm.environment.set('timeStamp', ts); 
pm.environment.set('hash', hash);

{ "code": "InvalidCredentials", "message": "That hash, timestamp and key combination is invalid." }

Upvotes: 0

Views: 3773

Answers (2)

Vero Gorena
Vero Gorena

Reputation: 31

  1. I created in Android Studio, a new java class named MD5Hash, following the steps of https://javarevisited.blogspot.com/2013/03/generate-md5-hash-in-java-string-byte-array-example-tutorial.html I just simplified his (her) code, only to use it with Java utility MessageDigest

    public class MD5Hash {
    
       public static void main(String args[]) {
    
           String publickey = "abcdef";    //your api key
           String  privatekey = "123456"; //your private key
    
           Calendar calendar=Calendar.getInstance();
    
           String stringToHash = calendar
                                .getTimeInMillis()+ privatekey + publickey;
    
           System.out.println("hash : " + md5Java(stringToHash));
           System.out.println("ts : "+   calendar.getTimeInMillis());
    
      }
    
      public static String md5Java(String message){
    
          String digest = null;
          try {
              MessageDigest md = MessageDigest.getInstance("MD5");
              byte[] hash = md.digest(message.getBytes("UTF-8"));
              //converting byte array to Hexadecimal String
              StringBuilder sb = new StringBuilder(2*hash.length);
              for(byte b : hash){
                  sb.append(String.format("%02x", b&0xff));
              }
              digest = sb.toString();
              } catch (UnsupportedEncodingException ex) {
              } catch (NoSuchAlgorithmException ex) {
              }
          return digest;
      }
    
    }
    

As you can see, if you copy paste this code, it has a green arrow on the left side of the class declaration, clicking it you can run MD5Hash.main() and you'll have printed in your Run Screen the values for the time (ts) and for the hash.

  1. Then go to verify directly into the internet :

https://gateway.marvel.com/v1/public/characters?limit=20&ts=1574945782067&apikey=abcdef&hash=4bbb5dtf899th5132hjj66

Upvotes: 0

thinker
thinker

Reputation: 176

If someone can comment on why this is the solution, I would appreciate it. Here is what the issue was. The order of the hash actually matters. So had to flip the order of pvtkey + pubkey to pubkey + pvtkey. Why is this?

INCORRECT

var message = ts+pubkey+pvtkey;
var a = CryptoJS.MD5(message);
pm.environment.set("hash", a.toString());

CORRECT

var message = ts+pvtkey+pubkey;  
var a = CryptoJS.MD5(message); 
pm.environment.set("hash", a.toString());

Upvotes: 2

Related Questions