Hash functions

node v10.23.3
version: 5.0.0
endpointsharetweet
Variable c will refer to the CryptoJS library.
const c = require("crypto-js")
We input the data as a string. Good practice for data exchange in JS is to use the JSON encoding of an object.
const data = { a: 1, b: 2 } const dataString = JSON.stringify(data)
Different hashing algorithms will return different number of bytes.
const md5 = c.MD5(dataString) md5.words
const sha256 = c.SHA256(dataString) sha256.words
const sha512 = c.SHA512(dataString) sha512.words
There are different encodings available. Encoding mismatch can break your signature verification later.
Object.keys(c.enc)
Most often used visual representation for a hash is the default hexadecimal format.
console.log(md5.toString()) console.log(md5.toString(c.enc.Base64)) console.log(md5.toString(c.enc.Latin1)) md5.toString(c.enc.Utf16)
console.log(sha256.toString()) console.log(sha256.toString(c.enc.Base64)) console.log(sha256.toString(c.enc.Latin1)) sha256.toString(c.enc.Utf16)
console.log(sha512.toString()) console.log(sha512.toString(c.enc.Base64)) console.log(sha512.toString(c.enc.Latin1)) sha512.toString(c.enc.Utf16)
Loading…

no comments

    sign in to comment