jsrosetta

Standard library

Crypto (hashing)

Node.js's createHash compared to Go's crypto/sha256, Rust's sha2, Swift's CryptoKit, and Java's MessageDigest for computing a SHA-256 hash.

Minimum versions
Node.js ≥ 12.20Go ≥ 1.0Rust ≥ 1.85Swift ≥ 5.1Java ≥ 25
Verified on
Node.js 24.12.0Go 1.27.1Rust 1.98.1Swift 6.2.4Java 25.0.4.1

Node.js code is an ES module: save it as .mjs or set "type": "module" in package.json.

Node.js bundles every hash algorithm behind one common API, createHash(algorithm). Go splits each algorithm into its own package (crypto/sha256, crypto/md5, …), and returns a fixed-size byte array instead of an object — you encode it to hex yourself with encoding/hex. Rust has no hashing in std either, so it uses the sha2 crate, also returning a fixed-size byte array you hex-encode by hand. Swift uses Apple's CryptoKit framework. Java uses the long-standing MessageDigest (picking an algorithm by name string) together with HexFormat (since Java 17) to hex-encode the result.

SHA-256 hashing

import { createHash } from 'node:crypto'
 
const hash = createHash('sha256').update('hello').digest('hex')
 
console.log(hash) // 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

Reference: github.com/miguelmota/golang-for-nodejs-developers#crypto