Home > Enterprise >  Postman generate crypto hash gives error: TypeError: crypto.createHash is not a function
Postman generate crypto hash gives error: TypeError: crypto.createHash is not a function

Time:06-27

I am trying to create a Postman pre-requisite script by converting the below TS code.

import crypto from 'crypto';

const createTestingUserHash = (emailAddress: string, timestamp: string, salt: string) =>
  crypto
    .createHash('sha256')
    .update(`testinguserhash::${emailAddress}::${timestamp}::${salt}`)
    .digest('base64');

export const getLoginLink = async (email: string): Promise<string | undefined> => 
{

  const currentTimeStamp = new Date().toISOString();
  const testingHash = createTestingUserHash(email, currentTimeStamp, testingUserSalt);
  .
  .
  .

My Postman script:

const crypto = require('crypto-js');

const signToken = () => {
    const timestamp = "2022-06-23T14:51:34.694Z";
    const salt = "salt_value";
    const email = "[email protected]";

    var hash = crypto.createHash('sha256').update(`testinguserhash::${emailAddress}::${timestamp}::${salt}`).digest("base64");
    return (hash);
}

const signedToken = signToken();
console.log(`successfully generated hash : ${signedToken}`)

I also tried var hash = crypto.createHmac('SHA256', "secret").update("Message").digest('base64'); but Postman throws a similar error.

Can someone help me build this script?

CodePudding user response:

crypto-js has a completely different but well documented interface compared to node.js crypto library

The equivalent crypto-js code is

const crypto = require('crypto-js');

const signToken = () => {
    const timestamp = "2022-06-23T14:51:34.694Z";
    const salt = "salt_value";
    const emailAddress = "[email protected]";

    return crypto.SHA256(`testinguserhash::${emailAddress}::${timestamp}::${salt}`).toString(crypto.enc.Base64);
}

const signedToken = signToken();
console.log(`successfully generated hash : ${signedToken}`)
  • Related