cryto.createHash sha512 with hexDigest input type

孤街浪徒 提交于 2020-01-14 14:29:06

问题


I am trying to get the same result I obtain at http://jssha.sourceforge.net/

where

I have the word 'testing' in question:

var word = 'testing';
var hex = toHex(word); // '740065007300740069006e006700';

on jssha when selecting input type as HEX with the value of the hex variable and SHA-512 I get the following result:

6e42b2c2a6351036b78384212774135d99d849da3066264983e495b5f74dc922e3d361b8ea9c8527169757233ed0bd4e56b2c42aab0a21bbcca67219dc53b472

although I can't achieve the same result with nodejs crypto module.

require('crypto').createHash('sha512').update(hex).digest('hex')

outputs:

9ad6d9053c6c420fe61ec2fffd094e3a325bc71708e18232fd764a5eb30315e38464e620ef0b55b92fbf6c809838652a72d9412b0398b28d61ca432962451de2

So I am wondering how can I get the same result as jssha using crypto module?

Thanks


回答1:


"testing" in hex is 74657374696e67 if you use utf8 which is pretty much standard. What your toHex method returns assumes utf16.

For that hash, the website says:

521b9ccefbcd14d179e7a1bb877752870a6d620938b28a66a107eac6e6805b9d0989f45b5730508041aa5e710847d439ea74cd312c9355f1f2dae08d40e41d50

Do this in node.js to hash a hex string:

require('crypto').createHash('sha512').update(
  new Buffer("74657374696e67", "hex")
).digest('hex')

Node gives you the same hash. Oh, and this also gives you the same hash:

require('crypto').createHash('sha512').update("testing").digest('hex')


来源:https://stackoverflow.com/questions/7956695/cryto-createhash-sha512-with-hexdigest-input-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!