# Replacing crypto-js with the global crypto object

**URL:** <https://community.postman.com/t/replacing-crypto-js-with-the-global-crypto-object/78377>\
**Category:** Help Hub\
**Tags:** variables, scripts, modules\
**Created:** [April 11, 2025, 12:47am UTC](https://community.postman.com/t/replacing-crypto-js-with-the-global-crypto-object/78377 "2025-04-11T00:47:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimjonah](https://avatars.discourse-cdn.com/v4/letter/j/b9e5f3/32.png) [@jimjonah](https://community.postman.com/u/jimjonah)\
**Post date:** [April 11, 2025, 12:47am UTC](https://community.postman.com/t/replacing-crypto-js-with-the-global-crypto-object/78377/1 "2025-04-11T00:47:42Z")

</div>

My pre-request script has this.

```auto
var crypto = require('crypto-js');

```

I get this warning:

> Using “crypto-js” library is deprecated. Use global “crypto” object instead.

How can I fix this?

---

<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:** [April 14, 2025, 10:55am UTC](https://community.postman.com/t/replacing-crypto-js-with-the-global-crypto-object/78377/2 "2025-04-14T10:55:50Z")

</div>

Hey @jimjonah 👋

Welcome to the Postman Community! :postman:

Although the library is deprecated, it’s not going to be going away soon so you’re ok to still use that.

We have also [just released a new feature](https://github.com/postmanlabs/postman-app-support/issues/13542), in the latest version that allows you to use external packages in the sandbox - you should be able to modify your script to this:

```auto
const crypto = pm.require('npm:crypto-js@4.2.0')

```

For example:

```auto
const crypto = pm.require('npm:crypto-js@4.2.0');

// Encrypt
var ciphertext = crypto.AES.encrypt('my message', 'secret key 123').toString();

// Decrypt
var bytes = crypto.AES.decrypt(ciphertext, 'secret key 123');
var originalText = bytes.toString(crypto.enc.Utf8);

console.log(originalText)

```

 ![Screenshot 2025-04-14 at 11.59.33](https://us1.discourse-cdn.com/getpostman/original/3X/2/b/2bcc0eb70f4404d12124a7714e529e08eaf7b787.png)

---

<div class="post-metadata">

**Author:** ![scastrianni-gs](https://avatars.discourse-cdn.com/v4/letter/s/aca169/32.png) [@scastrianni-gs](https://community.postman.com/u/scastrianni-gs)\
**Post date:** [May 7, 2025, 3:44pm UTC](https://community.postman.com/t/replacing-crypto-js-with-the-global-crypto-object/78377/3 "2025-05-07T15:44:24Z")

</div>

That is great, but what is the answer to the original question? How do I use the global “crypto” object instead?

---

<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, 4:31pm UTC](https://community.postman.com/t/replacing-crypto-js-with-the-global-crypto-object/78377/4 "2025-05-07T16:31:05Z")

</div>

Hey @scastrianni-gs 👋

I have no insights into the scripts being used, as that wasn’t shared, but here’s an example.

I’m just using the `crypto` module without requiring it at the top of the script.

 ![Screenshot 2025-05-07 at 17.26.22](https://us1.discourse-cdn.com/getpostman/original/3X/e/b/ebc9d3018de8c59712279b858b892ad1cbe80efe.png)

```auto
console.log(crypto.randomUUID());

async function sha256(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('');
    console.log("SHA-256:", hashHex);
}

console.log(sha256("Hello, world!"));

```

What are you having issues with on your side?

If you can share more details, we will be able to help you. 🙏

---

<div class="post-metadata">

**Author:** ![scastrianni-gs](https://avatars.discourse-cdn.com/v4/letter/s/aca169/32.png) [@scastrianni-gs](https://community.postman.com/u/scastrianni-gs)\
**Post date:** [May 7, 2025, 5:41pm UTC](https://community.postman.com/t/replacing-crypto-js-with-the-global-crypto-object/78377/5 "2025-05-07T17:41:02Z")

</div>

My current function is:

```auto
const Crypto = require("crypto-js");

function parseJWT(token, part) {
    var partB64 = token.split('.')[part];
    var partWds = Crypto.enc.Base64.parse(partB64);
    var partStr = Crypto.enc.Utf8.stringify(partWds);
    var partJson = JSON.parse(partStr);
    return partJson;
}

```

My initial question is how to access this global “crypto” object? Do I have to import anything or do I just start referencing `crypto` in my code?

I tried just deleting the `const Crypto = require("crypto-js");` line from my script and everything still works. Is there a global `Crypto` object that is a drop in replacement?

---

<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:55pm UTC](https://community.postman.com/t/replacing-crypto-js-with-the-global-crypto-object/78377/6 "2025-05-07T17:55:04Z")

</div>

This could also be an option here without using the `crypto` module and use `Buffer` for decoding a JWT’s header or payload:

```auto
function parseJWT(token, part) {
    const partB64Url = token.split('.')[part];
    const partB64 = partB64Url.replace(/-/g, '+').replace(/_/g, '/');
    const padded = partB64.padEnd(partB64.length + (4 - partB64.length % 4) % 4, '=');
    const decoded = Buffer.from(padded, 'base64').toString('utf8');
    return JSON.parse(decoded);
}

const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.' +
              'eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.' +
              'SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c';

// Parse header
const header = parseJWT(token, 0);
console.log("Header:", header);

// Parse payload
const payload = parseJWT(token, 1);
console.log("Payload:", payload);

```

---

<div class="post-metadata">

**Author:** ![scastrianni-gs](https://avatars.discourse-cdn.com/v4/letter/s/aca169/32.png) [@scastrianni-gs](https://community.postman.com/u/scastrianni-gs)\
**Post date:** [May 8, 2025, 2:18am UTC](https://community.postman.com/t/replacing-crypto-js-with-the-global-crypto-object/78377/7 "2025-05-08T02:18:15Z")

</div>

Works beautifully! Thanks so much for this! You saved the day!
