# node:crypto

> How Elide supports Node's `crypto` module: hashing, ciphers, keys, signatures, ML-DSA and ML-KEM, its error strings, and the APIs that are not available.

`node:crypto` provides [Node's crypto API](https://nodejs.org/api/crypto.html) for hashing, ciphers, keys, signatures,
key agreement, key derivation, and random values, including ML-DSA and ML-KEM keys. `webcrypto` is the same object as
`globalThis.crypto`.

## Example

```js
// crypto.mjs
import { createCipheriv, createDecipheriv, createHash, createHmac, randomBytes } from "node:crypto";

console.log(createHash("sha256").update("elide").digest("hex"));
console.log(createHmac("sha256", "secret").update("elide").digest("base64url"));

const key = randomBytes(32);
const iv = randomBytes(12);
const cipher = createCipheriv("aes-256-gcm", key, iv);
const sealed = Buffer.concat([cipher.update("attack at dawn"), cipher.final()]);
const decipher = createDecipheriv("aes-256-gcm", key, iv).setAuthTag(cipher.getAuthTag());
console.log(Buffer.concat([decipher.update(sealed), decipher.final()]).toString());
```

`elide run crypto.mjs` prints:

```text
bdfdf1cc04d072e4128275829659a2296e17d52f848a06528e36bd2a5a7d142a
XtA6v3HkpDq8cpjaFeOfRO2VxSoKx5bR11f7dK9U25g
attack at dawn
```

## Differences from Node

- `process.versions.openssl` is `1.1.1` and `process.features.openssl_is_boringssl` is `true`.
- Errors use BoringSSL-style strings (`error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT`) and
  `ERR_OSSL_*` codes.
- `X509Certificate` and `Certificate` are not exported.
- Ed448 and X448 throw `ERR_CRYPTO_UNSUPPORTED_OPERATION`; OCB ciphers are absent.
- `argon2` and `argon2Sync` throw `ERR_CRYPTO_ARGON2_NOT_SUPPORTED`.
- `setFips` throws `ERR_CRYPTO_FIPS_UNAVAILABLE`; `getFips()` returns `0`.
- `setEngine` throws `ERR_CRYPTO_CUSTOM_ENGINE_NOT_SUPPORTED`.

<!-- api-reference:start 912f1ca702e3 -->
## 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`](https://github.com/elide-dev/testsuite/blob/main/reports/1.6.0%2B6e718933f/4cbcae6c33a8/node-api/node-api.md): 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.
<!-- api-reference:end -->
