1

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();

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

It's similar to what I tried. But your code worked. Thanks
github.com/brix/crypto-js: "Active development of CryptoJS has been discontinued. This library is no longer maintained."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.