Thư viện chuẩn
Crypto (hash)
createHash của Node.js so với crypto/sha256 (Go), sha2 (Rust), CryptoKit (Swift) và MessageDigest (Java) để tính hash SHA-256.
- Phiên bản tối thiểu
- Node.js ≥ 12.20Go ≥ 1.0Rust ≥ 1.85Swift ≥ 5.1Java ≥ 25
- Đã chạy thử trên
- Node.js 24.12.0Go 1.27.1Rust 1.98.1Swift 6.2.4Java 25.0.4.1
Code Node.js là ES module: lưu file .mjs hoặc đặt "type": "module" trong package.json.
Node.js gom mọi thuật toán hash vào một API chung createHash(algorithm). Go tách mỗi thuật toán thành một package riêng (crypto/sha256, crypto/md5, …), trả về mảng byte cố định thay vì object — bạn tự encode sang hex bằng encoding/hex. Rust không có hash trong std nên dùng crate sha2, cũng trả về mảng byte cố định phải tự encode hex. Swift dùng framework CryptoKit của Apple. Java dùng MessageDigest (rất lâu đời, chọn thuật toán qua chuỗi tên) cùng HexFormat (từ Java 17) để encode hex.
Hash SHA-256
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")) // trả về [32]byte, không phải 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(); // mảng cố định 32 byte, không phải 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
}Tham khảo: github.com/miguelmota/golang-for-nodejs-developers#crypto