# Pre-request script - crypto module help

**URL:** <https://community.postman.com/t/pre-request-script-crypto-module-help/43532>\
**Category:** Help Hub\
**Tags:** encryption, pre-request-script\
**Created:** [February 2, 2023, 6:40pm UTC](https://community.postman.com/t/pre-request-script-crypto-module-help/43532 "2023-02-02T18:40:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mission-administrat9](https://avatars.discourse-cdn.com/v4/letter/m/eada6e/32.png) [@mission-administrat9](https://community.postman.com/u/mission-administrat9)\
**Post date:** [February 2, 2023, 6:40pm UTC](https://community.postman.com/t/pre-request-script-crypto-module-help/43532/1 "2023-02-02T18:40:43Z")

</div>

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

_Details (like screenshots)_:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/a/f/afc8d84bca6815517a26b430c5e342d95d363c2d.png)

modified request:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/d/e/def699d074a0e8dbd10e906b5ebcd396b87de8df.png)

_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.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [May 7, 2025, 5:00pm UTC](https://community.postman.com/t/pre-request-script-crypto-module-help/43532/2 "2025-05-07T17:00:39Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [May 7, 2025, 5:00pm UTC](https://community.postman.com/t/pre-request-script-crypto-module-help/43532/3 "2025-05-07T17:00:44Z")

</div>

Closed on May 7, 2025, 5:00pm UTC


