Skip to content

Instantly share code, notes, and snippets.

@joecliff
Last active March 16, 2023 20:32
Show Gist options
  • Save joecliff/10948592 to your computer and use it in GitHub Desktop.
Save joecliff/10948592 to your computer and use it in GitHub Desktop.
cryptojs WordArray usage
/**
* cryptojs use WordArray (CryptoJS.lib.WordArray) as parameter/result frequently.
* A WordArray object represents an array of 32-bit words. When you pass a string,
* it's automatically converted to a WordArray encoded as UTF-8.
*/
var CryptoJS = require("crypto-js");
// convert String to WordArray
var wordArray = CryptoJS.enc.Utf8.parse('Hello, World!');
// convert WordArray To String
var result1=wordArray.toString(CryptoJS.enc.Utf8);
var result2=CryptoJS.enc.Utf8.stringify(wordArray);
console.log(result1);
console.log(result1===result2);
@kirin-yuen
Copy link

kirin-yuen commented Jun 16, 2018

Hi, I've a question about how can I do this following PHP Code

$signStr = base64_encode(hash_hmac('SHA1', 'msg', $secret_key, true).'msg');

into JS?

CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA1('msg', secret_key) + 'msg') ;

It's not work and throw error
crypto-js.js:789 Uncaught TypeError: wordArray.clamp is not a function

@RobinSmithTHR
Copy link

Base64.stringify wants a wordArray as the input, not a string. Do that conversion first.

@gamelife1314
Copy link

How to get a WordArray from Buffer, this buffer may be created by s.readFileSync(chunkLocalPath)

@rankun203
Copy link

@gamelife 1314 CryptoJS.enc.Hex.parse()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment