I'm trying to hash a string using the crypto-js library in a react/typescript project. I'm using crypto-js 3.1.9 and @types/crypto-js 3.1.33.
Here's some code:
import CryptoJS = require("crypto-js");
export const hashString= (str: string): string => {
const hash = CryptoJS.MD5(str);
return hash;
}
I expect hash to be of type string, as specified in the documentation of the crypto-js implementation. But the function returns an object, that contains a wordarray.
I also tried calling
hash.toString(CryptoJS.enc.Hex)
but that didn't work, because typescript also assumes that hash will be a string. So a parameterized toString function is not allowed.
What am I doing wrong?
hash.toString(CryptoJS.enc.Hex)should be correct, but you can also tryhash.toString()hash.toString()is working fine for me.