marcosspn
marcosspn

Reputation: 386

Equivalent JavaScript code to generate SHA256 hash

I have this code in Java that generates a SHA256 hash:

Hashing.sha256().hashString(value,Charsets.UTF_16LE).toString()

I'm trying to do the same on JavaScript/Node, that having the same value returns the same result.

I tried usind crypto-js but without success (it returns a hash string but different from the one generated with the Java code).

I tried this, for example:

        import * as sha256 from 'crypto-js/sha256';
        import * as encutf16 from 'crypto-js/enc-utf16';
    
        ...

        let utf16le = encutf16.parse(key);
        let utf16Sha256 = sha256(utf16le);
        let utf16Sha256String = utf16Sha256.toString();

Upvotes: 1

Views: 4200

Answers (1)

Nidhin Prathap
Nidhin Prathap

Reputation: 722

Can you try something like this :-

const CryptoJS = require('crypto-js');

let utf16le = CryptoJS.enc.Utf16LE.parse(word);
let utf16Sha256 = CryptoJS.SHA256(utf16le);
return utf16Sha256.toString(CryptoJS.enc.Hex);

Or else if you can give a sample of whats the input and expected output corresponding to JAVA code it will be easier

Upvotes: 2

Related Questions