Перейти до вмісту

Довідник API node:crypto

Хешування, шифри, ключі, підписи, узгодження ключів і випадкові дані

На цій сторінці
import { createHash, randomUUID } from "node:crypto";

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

Підтримуваний інтерфейс

  • Хешування та HMAC: createHash, createHmac, hash, getHashes
  • Шифри: createCipheriv, createDecipheriv, getCiphers, getCipherInfo (AES CBC, CTR, ECB, CFB, OFB, GCM, CCM і key wrap; ChaCha20 і ChaCha20-Poly1305; 3DES)
  • Ключі: KeyObject, createSecretKey, createPublicKey, createPrivateKey з імпортом і експортом PEM, DER (PKCS#1, PKCS#8, SPKI, SEC1, зашифрований PKCS#8 і застарілий зашифрований PEM) та JWK
  • Генерування ключів: generateKeyPair, generateKeyPairSync, generateKey, generateKeySync для RSA, RSA-PSS, DSA, EC, Ed25519, X25519, DH, ML-DSA і ML-KEM
  • Підписи: sign, verify, createSign, createVerify (PKCS#1 v1.5, PSS, кодування DER та IEEE-P1363 для ECDSA/DSA)
  • Шифрування RSA: publicEncrypt, privateDecrypt, privateEncrypt, publicDecrypt
  • Узгодження ключів: createDiffieHellman, getDiffieHellman, createECDH, ECDH.convertKey, diffieHellman, getCurves
  • Виведення ключів і прості числа: hkdf, pbkdf2, scrypt, generatePrime, checkPrime
  • Випадкові дані: randomBytes, randomFill, randomInt, randomUUID
  • timingSafeEqual, constants і webcrypto (той самий об’єкт, що й globalThis.crypto)

Криптографія Elide працює на AWS-LC, форку BoringSSL, і подає себе так само, як Node, зібраний з BoringSSL: process.versions.openssl дорівнює 1.1.1, а process.features.openssl_is_boringssl — true. Помилки містять рядки помилок AWS-LC (наприклад, error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT) з library, reason і кодом ERR_OSSL_*. Ed448, X448 і OCB недоступні; режим FIPS увімкнути неможливо.

Стандартні форми аргументів описано в документації Node.js щодо crypto.

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.

Чи була ця сторінка корисною?