# CryptoJS does not work in pre-request script

**URL:** <https://community.postman.com/t/cryptojs-does-not-work-in-pre-request-script/13158>\
**Category:** Help Hub\
**Created:** [June 5, 2020, 12:54pm UTC](https://community.postman.com/t/cryptojs-does-not-work-in-pre-request-script/13158 "2020-06-05T12:54:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rvitalik34](https://avatars.discourse-cdn.com/v4/letter/r/e36b37/32.png) [@rvitalik34](https://community.postman.com/u/rvitalik34)\
**Post date:** [June 5, 2020, 12:54pm UTC](https://community.postman.com/t/cryptojs-does-not-work-in-pre-request-script/13158/1 "2020-06-05T12:54:13Z")

</div>

Simple script:  
const​ hash = CryptoJS.HmacSHA256(‘message’, ‘secret’);  
console.log(‘hash:’ + hash);

What I see from console is:  
SyntaxError: Invalid or unexpected token

Version 7.25.2 for Linux

What should I do to make it work?

---

<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:** [June 5, 2020, 10:02pm UTC](https://community.postman.com/t/cryptojs-does-not-work-in-pre-request-script/13158/2 "2020-06-05T22:02:06Z")

</div>

Hey @rvitalik34

Welcome to the Postman community!! 🏆

I did get the same error message when I copied your snippet, might be just the forum syntax weirdness.

I went over to the documentation and copied the same encryption method line from here:

> **[CryptoJS](https://cryptojs.gitbook.io/docs/#hmac)**
>
> Original documentation: https://code.google.com/archive/p/crypto-js/

That seemed to work for me, could you give it a go please?

---

<div class="post-metadata">

**Author:** ![o.kulychok](https://avatars.discourse-cdn.com/v4/letter/o/838e76/32.png) [@o.kulychok](https://community.postman.com/u/o.kulychok)\
**Post date:** [February 21, 2021, 6:26am UTC](https://community.postman.com/t/cryptojs-does-not-work-in-pre-request-script/13158/3 "2021-02-21T06:26:50Z")

</div>

When you copy-paste examples from CryptoJS website then you copiend invisible characters also(zero width whitespace) They generate parsing errors which you mentioned: SyntaxError: Invalid or unexpected token. To solve this just type code manually. Or search for editor when you can view all non-printable characters and remove them.

---

<div class="post-metadata">

**Author:** ![sahuvikramp](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/sahuvikramp/32/10828_2.png) [@sahuvikramp](https://community.postman.com/u/sahuvikramp)\
**Post date:** [February 28, 2022, 4:12pm UTC](https://community.postman.com/t/cryptojs-does-not-work-in-pre-request-script/13158/4 "2022-02-28T16:12:14Z")

</div>

I do agree that sometimes the examples didn’t work as expected may be copy/paste issue. You can find examples related to Crypto JS now on Postman collection.

Just fork it and use it directly.

[![Run in Postman](https://run.pstmn.io/button.svg)](https://god.gw.postman.com/run-collection/18070393-66f7caac-3005-44b2-8620-16dfb64a35f4?action=collection%2Ffork&collection-url=entityId%3D18070393-66f7caac-3005-44b2-8620-16dfb64a35f4%26entityType%3Dcollection%26workspaceId%3Daa5fb3b8-0090-4b5e-b3b4-fa5c1f2d080d)

Reference for HMAC example : [Postman](https://www.postman.com/postman/workspace/postman-answers/request/18070393-cef8bd0f-11eb-4684-a724-f5c7a63a62d8)

Cheers.

---

<div class="post-metadata">

**Author:** ![yeaser.khan](https://avatars.discourse-cdn.com/v4/letter/y/a88e4f/32.png) [@yeaser.khan](https://community.postman.com/u/yeaser.khan)\
**Post date:** [April 26, 2023, 9:33am UTC](https://community.postman.com/t/cryptojs-does-not-work-in-pre-request-script/13158/5 "2023-04-26T09:33:45Z")

</div>

I got it to work with this 🙂

var CryptoJS = require(“crypto-js”);

// Set your secret key and data to be hashed  
var secretKey = “YOUR\_SECRET\_KEY”;  
var data = “YOUR\_DATA\_TO\_BE\_HASHED”;

// Generate the hash using HMAC-SHA256 and encode to Base64  
var hash = CryptoJS.HmacSHA256(data, secretKey).toString(CryptoJS.enc.Base64);

// Set the hash as a header  
pm.request.headers.add({  
key: “Authorization”,  
value: "Bearer " + hash  
});

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [August 22, 2023, 1:59pm UTC](https://community.postman.com/t/cryptojs-does-not-work-in-pre-request-script/13158/6 "2023-08-22T13:59:40Z")

</div>

Closed on August 22, 2023, 1:59pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
