# Using crypto.subtle to Verify JWT Signing Keys with Newman

**URL:** <https://community.postman.com/t/using-crypto-subtle-to-verify-jwt-signing-keys-with-newman/77758>\
**Category:** Help Hub\
**Tags:** newman-project, promise, crypto, jwt\
**Created:** [March 27, 2025, 6:33pm UTC](https://community.postman.com/t/using-crypto-subtle-to-verify-jwt-signing-keys-with-newman/77758 "2025-03-27T18:33:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthew-beck-cpc](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@matthew-beck-cpc](https://community.postman.com/u/matthew-beck-cpc)\
**Post date:** [March 27, 2025, 6:33pm UTC](https://community.postman.com/t/using-crypto-subtle-to-verify-jwt-signing-keys-with-newman/77758/1 "2025-03-27T18:33:48Z")

</div>

> **Archived topic — this may be out of date.**  
> Newman has been replaced by the **[Postman CLI](https://learning.postman.com/docs/postman-cli/postman-cli-overview/)**, Postman’s official, signed command-line tool. It runs collections as Newman did, and also lints API specs, runs mocks and monitors, and syncs local files with Postman from your terminal, CI/CD pipeline or coding agent.  
> Working with an AI coding agent? The [Postman plugin](https://github.com/postmanlabs/postman-plugin) lets agents in Claude Code, Cursor, Codex and others design, mock, test and ship APIs with Postman.  
> For current help, see the [Postman docs](https://learning.postman.com/docs/), try the free [Postman tools](https://www.postman.com/tools) (no account needed), or join us on [Discord](https://discord.gg/58aNNsRBGR).

Hello Postmanauts,

I am able to verify jwt signatures in Postman, but am not able to get it to work with Newman.

This works in the normal Postman “Scripts” section:

```auto
const CryptoJS = require('crypto-js');
pm.test('JWT Signature is Valid', function () {
    let recreatedSignature;
    try {
        // Generate the hash using HMAC-SHA256 and encode to Base64
        recreatedSignature = CryptoJS.HmacSHA256(
                `${jwtHeaderEncoded}.${jwtPayloadEncoded}`,
                jwtSigningKey
            )
            .toString(CryptoJS.enc.Base64)
            .replace(/=*$/, '');
        console.log('recreatedSignature', recreatedSignature);
    }
    catch(e) {
        throw new Error('Could not recreate signature | ' + e)
    }
    
    pm.expect(jwtSignature).to.be.a('string').that.equals(recreatedSignature);
});

```

However, when I run this in Newman, I get the following error:

![image](https://us1.discourse-cdn.com/getpostman/original/3X/6/0/60ef14daa4d58e70c7b1d24efe1efa5de51fc39b.png)

When I remove `require('crypto-js')`, that error goes away.

I found [this post here](https://community.postman.com/t/how-to-use-cryptojs-package-in-postman-flows-evaluate-block/74443) where Daniel Kimmelmann states that `crypto.subtle` is supported, and not `require('crypto-js')`.

Following this post, I came up with:

```auto

pm.test('JWT Signature is Valid', function () {
    let recreatedSignature;
    try {
        const encoder = new TextEncoder();
        const data = encoder.encode(`${jwtHeaderEncoded}.${jwtPayloadEncoded}`)
        crypto.subtle.importKey('raw', new TextEncoder().encode(jwtSigningKey), {
            name: 'HMAC',
            hash: 'SHA-256',
        }, false, ['sign'])
            .then((key) => crypto.subtle.sign('HMAC', key, data))
            .then((signature) => {
                recreatedSignature = _arrayBufferToBase64(signature);
                pm.expect(jwtSignature).to.be.a('string').that.equals(recreatedSignature);
            })
            .catch((error) => {console.log(error)});
    }
    catch(e) {
        throw new Error('Could not recreate signature | ' + e)
    }
});

// https://stackoverflow.com/a/9458996/27294094
function _arrayBufferToBase64( buffer ) {
    var binary = '';
    var bytes = new Uint8Array( buffer );
    var len = bytes.byteLength;
    for (var i = 0; i < len; i++) {
        binary += String.fromCharCode( bytes[i] );
    }
    return btoa( binary );
}

```

There are 2 issues:

1. The computed signature does not match
2. The Promise flow breaks the Chai tests - it reports success when there is a failure.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/5/8/58d04679f529d2270632bf5ce22a33d52b92492d.png)

Thank you for taking time to help me out!

---

<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:** [March 28, 2025, 3:30pm UTC](https://community.postman.com/t/using-crypto-subtle-to-verify-jwt-signing-keys-with-newman/77758/2 "2025-03-28T15:30:34Z")

</div>

Hey @matthew-beck-cpc 👋

Thanks for posting the question - Daniel is talking about the Postman Flows _Evaluate_ block which is a different context to the Script sandbox of the request.

Which version of Newman is this? There were some updates to Postman that included new global objects that might not be part of Newman?

> **[Process data and script workflows using Postman JavaScript objects | Postman...](https://learning.postman.com/docs/tests-and-scripts/write-scripts/postman-sandbox-api-reference/#use-global-objects)**
>
> Postman is a collaboration platform for API development. Postman's features simplify each step of building an API and streamline collaboration so you can create better APIs—faster.

Do you get the same output when using the Postman CLI?

---

<div class="post-metadata">

**Author:** ![matthew-beck-cpc](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@matthew-beck-cpc](https://community.postman.com/u/matthew-beck-cpc)\
**Post date:** [March 31, 2025, 6:12pm UTC](https://community.postman.com/t/using-crypto-subtle-to-verify-jwt-signing-keys-with-newman/77758/3 "2025-03-31T18:12:20Z")

</div>

Hi Danny,

I am using Newman version 6.2.1 with Node.js 22.11.0 on Windows 11.

I think I have resolved the promise flow issue by using async/await, as well as passing a `done` callback to the test function, thanks to [this StackOverflow answer here](https://stackoverflow.com/a/34366985/27294094).

This is now working without `require('crypto-js')` - and it is properly calculating the signature, and it properly reports successes and failures to me in the Postman desktop application:

```auto
pm.test('JWT Signature is Valid', async function (done) {
    try {
        
        const encoder = new TextEncoder();
        const data = encoder.encode(`${jwtHeaderEncoded}.${jwtPayloadEncoded}`);

        let key = await crypto.subtle.importKey('raw', encoder.encode(jwtSigningKey), {
            name: 'HMAC',
            hash: 'SHA-256',
        }, false, ['sign']);

        let signature = await crypto.subtle.sign('HMAC', key, data);

        let recreatedSignature = _arrayBufferToBase64(signature).replace(/=*$/, '');
        console.log('recreatedSignature', recreatedSignature);

        pm.expect(jwtSignature).to.be.a('string').that.equals(recreatedSignature);
        
        done();
    } catch(err) {
        done(err)
    }
});

```

However, I am getting this error now:  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/3/5/35be510eb06a7edc887f9d8c015a1e48f5c68680.png)

Even though it says it is supported on the link you provided:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/8/c/8c6f716330c5a67c0bb127208c67e477be51b587.png)

I was not aware of the Postman CLI before this. I just installed Postman CLI version 1.13.1. Interestingly, the Postman CLI works for all my other requests that use `require('crypto-js')`, but also fails with `const encoder = new TextEncoder();`, even though it does work in the regular Postman desktop app:

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

So the only remaining mystery that I can not figure out is this:

Why is `new TextEncoder()` not working in Postman CLI or Newman, while it does work in the Postman Windows desktop app?

---

<div class="post-metadata">

**Author:** ![matthew-beck-cpc](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@matthew-beck-cpc](https://community.postman.com/u/matthew-beck-cpc)\
**Post date:** [March 31, 2025, 6:54pm UTC](https://community.postman.com/t/using-crypto-subtle-to-verify-jwt-signing-keys-with-newman/77758/4 "2025-03-31T18:54:27Z")

</div>

Update:

I have now gotten this to work without having to call `new TextEncoder()` - with this `str2ab` function to replace the `encoder.encode` function used previously (thanks to [this StackOverflow answer](https://stackoverflow.com/a/77643978/27294094)):

```auto
function str2ab(str) {
    const buffer = new Uint8Array(str.length);
    for (let i = 0; i < str.length; i++) {
      buffer[i] = str.charCodeAt(i);
    }
    return buffer;
}

```

However, now I am getting this error in both Postman CLI and Newman:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/8/c/8ce71dd1d43eb964f81c6d6d0fd75a1272f184bb.png)

Again, just like `TextEncoder`, the `crypto` property is purportedly supported

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

I am calling it like `crypto.subtle` - which works in the desktop app. Is there another way to call it?

---

<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 1, 2025, 8:56am UTC](https://community.postman.com/t/using-crypto-subtle-to-verify-jwt-signing-keys-with-newman/77758/5 "2025-04-01T08:56:10Z")

</div>

Hey @matthew-beck-cpc 👋

Thanks for the updates, those help - As those have been recently introduced to Postman, there may be a slight delay in them being available outside of the desktop or web platform.

I will need to check with the team to know for sure 🙏

---

<div class="post-metadata">

**Author:** ![matthew-beck-cpc](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@matthew-beck-cpc](https://community.postman.com/u/matthew-beck-cpc)\
**Post date:** [April 1, 2025, 2:38pm UTC](https://community.postman.com/t/using-crypto-subtle-to-verify-jwt-signing-keys-with-newman/77758/6 "2025-04-01T14:38:46Z")

</div>

Thank you for your attention to this!

It seems I would be able to get the Postman CLI to work with crypto-js, but support for this does seem to be on its way out - I get this warning in the desktop app:

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

I’ll probably be writing a lot of Postman tests in the upcoming months, and by writing I really mean copy-pasting existing request scripts, so the sooner I know which way to go in this fork in the road the better 🙂

---

<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 1, 2025, 2:58pm UTC](https://community.postman.com/t/using-crypto-subtle-to-verify-jwt-signing-keys-with-newman/77758/7 "2025-04-01T14:58:21Z")

</div>

I’m still waiting to hear back from the team about any updates to the Postman CLI.

@malvika-chaudhary are you able to provide any insights here 🙏
