GardenTools & experiments

MD5 digests and keyed SHA-256 in Node.js

Two small crypto examples that perform different operations: an MD5 digest and an HMAC.

The original note paired two short Node.js examples. They are useful to keep separate because a plain digest and a keyed message-authentication code are different operations.

An MD5 digest

const crypto = require('crypto')
let hash = crypto.createHash('md5').update('some_string').digest("hex")

This returns a hexadecimal MD5 digest of the input string. MD5 should not be treated as a collision-resistant integrity or password-storage primitive.

An HMAC using SHA-256

const crypto = require('crypto');
let t = crypto.createHmac('sha256', 'key').update('input').digest("hex");

The second example calls createHmac, so it uses a key as well as an input. It is not the same operation as an unkeyed SHA-256 hash. The strings key and input are example values.

These snippets are preserved from the earlier CommonJS note; no application authentication scheme is established by either isolated line.

← Back to the garden