Ionic 4 can't perform sha256 hashing with key using crypto-js

Hello, I am using crypto-js to perform sha256 hashing on data. The algorithm works fine without key like

CryptoJS.SHA256(message).toString(CryptoJS.enc.Hex)

But when I use a key for hashing it is generating wrong data.

Here is full code

import CryptoJS from 'crypto-js';

let key = 'the shared secret key here';
let message = 'the message to hash here';

console.log(message,key)

let ans = CryptoJS.SHA256(message,key).toString(CryptoJS.enc.Hex)
console.log(ans)

What I am doing wrong?
Thank you in advance

If you don’t get any better answers, I would use WebCrypto instead for this. I hope you’re not really hardcoding secrets into your app binary, though. Anybody can read them.

1 Like