Pre-request script - crypto module help

Hello, I am having trouble working out this pre-request script.

Details (like screenshots):

modified request:

How I found the problem:

I’ve already tried:
first error I get is “cannot find module ‘crypto’”
I have tried to use crypto-js, this then kick an error that crypto.createHash(‘sha256’) is not a function

I then use crypto.SHA256 to fix that and then i get a “hash.update is not a function”

not totally up to speed with the crypto.js library so it could just be my approach is all wrong.

1 Like

Thanks for your post, and sorry we didn’t get to this sooner. This isn’t the experience we want for our users.

An example of something that could be used:

async function generateSHA256Hash(message) {
    const encoder = new TextEncoder();
    const data = encoder.encode(message);
    const hashBuffer = await crypto.subtle.digest('SHA-256', data);
    const hashArray = Array.from(new Uint8Array(hashBuffer));
    const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
    return hashHex;
}

async function getToken() {
    const timestamp = (Date.now() / 1000 | 0).toString();
    const userNonce = pm.globals.get('usernonce');
    const userKey = pm.globals.get('userkey');
    const integrationIdentifier = '';

    const toHash = `${timestamp}:${userKey}`;
    const encrypted = await generateSHA256Hash(toHash);

    const authToken = integrationIdentifier.length > 0
        ? `${userNonce}:${timestamp}:${encrypted}:${integrationIdentifier}`
        : `${userNonce}:${timestamp}:${encrypted}`;

    pm.globals.set("authtoken", authToken);
    console.log(authToken);
}

// Call the async function
getToken();

Since the post is a bit older, we’re closing it to make room for newer discussions. If you’re still experiencing this or have new details to share, we’d love for you to start a new discussion