Hashing with Node.js
[Node.js Home][Home]
The Crypto library in Node.js supports a rangeo of hashing methods, including MD5, SHA-1 and SHA-256..
|
Outline
The code used is:
const crypto = require("crypto"); var algorithm="sha256"; var message="hello"; var args = process.argv; if (args.length>2) message=args[2]; if (args.length>3) algorithm=args[3]; var hash = crypto.createHash(algorithm); hash.update(message); console.log("Message:\t",message); console.log("Method:\t\t",algorithm); var myhash = hash.digest('hex'); console.log("\nHash is:\t",myhash); hash = crypto.createHash(algorithm); console.log("Hash is:\t",hash.digest('base64')); console.log("\nLength of hash: ",myhash.length*4," bits");
A sample run for SHA-1:
Message: Test Method: sha1 Hash is: 640ab2bae07bedc4c163f679a746f7ab7fb5d1fa Hash is: 2jmj7l5rSw0yVb/vlWAYkK/YBwk=