Saltar al contenido

Referencia de la API node:crypto

Hashing, cifrados, claves, firmas, acuerdo de claves y datos aleatorios

En esta página
import { createHash, randomUUID } from "node:crypto";

console.log(createHash("sha256").update("abc").digest("hex"));
console.log(randomUUID());

Superficie admitida

  • Hashing y HMAC: createHash, createHmac, hash, getHashes
  • Cifrados: createCipheriv, createDecipheriv, getCiphers, getCipherInfo (AES CBC, CTR, ECB, CFB, OFB, GCM, CCM y key wrap; ChaCha20 y ChaCha20-Poly1305; 3DES)
  • Claves: KeyObject, createSecretKey, createPublicKey, createPrivateKey, con importación y exportación en PEM, DER (PKCS#1, PKCS#8, SPKI, SEC1, PKCS#8 cifrado y PEM cifrado heredado) y JWK
  • Generación de claves: generateKeyPair, generateKeyPairSync, generateKey, generateKeySync para RSA, RSA-PSS, DSA, EC, Ed25519, X25519, DH, ML-DSA y ML-KEM
  • Firmas: sign, verify, createSign, createVerify (PKCS#1 v1.5, PSS, y codificaciones DER e IEEE-P1363 de ECDSA/DSA)
  • Cifrado RSA: publicEncrypt, privateDecrypt, privateEncrypt, publicDecrypt
  • Acuerdo de claves: createDiffieHellman, getDiffieHellman, createECDH, ECDH.convertKey, diffieHellman, getCurves
  • Derivación y números primos: hkdf, pbkdf2, scrypt, generatePrime, checkPrime
  • Datos aleatorios: randomBytes, randomFill, randomInt, randomUUID
  • timingSafeEqual, constants y webcrypto (el mismo objeto que globalThis.crypto)

La criptografía de Elide se basa en AWS-LC, una bifurcación de BoringSSL, y se identifica igual que Node cuando se compila con BoringSSL: process.versions.openssl es 1.1.1 y process.features.openssl_is_boringssl es true. Los fallos llevan las cadenas de error de AWS-LC (por ejemplo error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT) con library, reason y un código ERR_OSSL_*. Ed448, X448 y OCB no están disponibles; no se puede activar el modo FIPS.

Consulte la documentación de crypto de Node.js para ver las formas estándar de los argumentos.

Reference

Status: Partial · 95 of 112 suite tests pass (84.8%) · since 1.1.0

Exports

ExportKindStatusNotes
HashclassFullupdate(), digest(), copy()
HmacclassFullupdate(), digest()
createHash()functionFullCreates and returns a Hash object for the given algorithm
createHmac()functionFullCreates and returns an Hmac object for the given algorithm and key
randomBytes()functionFullGenerates cryptographically strong pseudorandom bytes
randomFill(), randomFillSync()functionFullFills a buffer with random bytes
randomInt()functionFullReturns a random integer n such that min <= n < max
randomUUID()functionFullGenerates a random RFC 4122 version 4 UUID string
pbkdf2(), pbkdf2Sync()functionFullDerives a key using PBKDF2
scrypt(), scryptSync()functionFullDerives a key using scrypt
timingSafeEqual()functionFullCompares two buffers in constant time to prevent timing attacks
getHashes()functionFullReturns the list of supported hash algorithm names
getCiphers()functionFullReturns the list of supported cipher algorithm names
constantspropertyFullCrypto constants such as RSA_PKCS1_PADDING
webcryptopropertyFullThe Web Crypto API (SubtleCrypto) instance

Not implemented

Certificate, X509Certificate, decapsulate, encapsulate.

Tests: node-api node:crypto: 95 pass, 17 fail, 0 error, 69 skipped. Failing: test-crypto-certificate.js, test-crypto-dh-leak.js, test-crypto-encap-decap.js, test-crypto-key-objects-messageport.js, test-crypto-keyobject-hidden-slots.js, test-crypto-pqc-encrypted-pkcs8.js, test-crypto-pqc-key-objects-ml-dsa.js, test-crypto-pqc-key-objects-ml-kem.js, and 9 more.

¿Te resultó útil esta página?