简体   繁体   English

等效 JavaScript 代码生成 SHA256 hash

[英]Equivalent JavaScript code to generate SHA256 hash

I have this code in Java that generates a SHA256 hash:我在 Java 中有这个代码,它生成一个 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.我试图在 JavaScript/Node 上做同样的事情,具有相同的value会返回相同的结果。

I tried usind crypto-js but without success (it returns a hash string but different from the one generated with the Java code).我尝试使用crypto-js但没有成功(它返回 hash 字符串,但与使用 Java 代码生成的字符串不同)。

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

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否则,如果您可以提供与 JAVA 代码相对应的输入和预期 output 的示例,它将更容易

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM