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 и обёртывание ключей; 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
| Export | Kind | Status | Notes |
|---|---|---|---|
Hash | class | Full | update(), digest(), copy() |
Hmac | class | Full | update(), digest() |
createHash() | function | Full | Creates and returns a Hash object for the given algorithm |
createHmac() | function | Full | Creates and returns an Hmac object for the given algorithm and key |
randomBytes() | function | Full | Generates cryptographically strong pseudorandom bytes |
randomFill(), randomFillSync() | function | Full | Fills a buffer with random bytes |
randomInt() | function | Full | Returns a random integer n such that min <= n < max |
randomUUID() | function | Full | Generates a random RFC 4122 version 4 UUID string |
pbkdf2(), pbkdf2Sync() | function | Full | Derives a key using PBKDF2 |
scrypt(), scryptSync() | function | Full | Derives a key using scrypt |
timingSafeEqual() | function | Full | Compares two buffers in constant time to prevent timing attacks |
getHashes() | function | Full | Returns the list of supported hash algorithm names |
getCiphers() | function | Full | Returns the list of supported cipher algorithm names |
constants | property | Full | Crypto constants such as RSA_PKCS1_PADDING |
webcrypto | property | Full | The 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.