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) // 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
)
func main() {
hash := sha256.Sum256([]byte("hello")) // returns a [32]byte, not a slice
fmt.Println(hex.EncodeToString(hash[:])) // 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
}// Cargo.toml: sha2 = "0.11"
use sha2::{Digest, Sha256};
fn main() {
let mut hasher = Sha256::new();
hasher.update(b"hello");
let hash = hasher.finalize(); // a fixed-size 32-byte array, not a Vec
let hex: String = hash.iter().map(|b| format!("{b:02x}")).collect();
println!("{hex}"); // 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
}import CryptoKit
import Foundation
let digest = SHA256.hash(data: Data("hello".utf8))
let hex = digest.map { String(format: "%02x", $0) }.joined()
print(hex) // 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824import java.security.MessageDigest;
import java.util.HexFormat;
void main() throws Exception {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest("hello".getBytes());
IO.println(HexFormat.of().formatHex(hash)); // 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
}Reference: github.com/miguelmota/golang-for-nodejs-developers#crypto