diff --git a/index.d.ts b/index.d.ts index ee9f6d1..190bd8c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1724,6 +1724,9 @@ declare global { ): any } + /** Byte encodings shared by the crypto/base64 hook helpers. */ + export type ByteEncoding = "hex" | "base64" | "base64url" | "bytes" + interface CryptoPackage { /** * get hex of sha256 hash @@ -1742,6 +1745,42 @@ declare global { * @returns hex hash */ md5(data: string): string + + /** + * Cryptographically secure random bytes (`crypto/rand`, never `Math.random`). + * + * `n` is the number of **bytes**, not characters; allowed range 1–4096. + * Without options the raw bytes are returned (JSON output base64-encodes them, + * like `fs.readFile(..., { bytes: true })`). + * + * @param n number of random bytes (1–4096) + * @param options.encoding `hex` | `base64` | `base64url` | `bytes` (default `bytes`) + */ + randomBytes(n: number): Int8Array + randomBytes(n: number, options: { encoding: "bytes" }): Int8Array + randomBytes( + n: number, + options: { encoding: "hex" | "base64" | "base64url" } + ): string + randomBytes( + n: number, + options: { encoding: ByteEncoding } + ): string | Int8Array + + /** + * URL-safe token from `n` random bytes. + * + * Default encoding is unpadded `base64url`: `randomToken(32)` yields 43 + * characters and 256 bit. `encoding: "bytes"` is rejected — a token is a string. + * + * @param n number of random bytes (1–4096) + * @param options.encoding `base64url` (default) | `base64` | `hex` + */ + randomToken(n: number): string + randomToken( + n: number, + options: { encoding: "hex" | "base64" | "base64url" } + ): string } interface JsonPackage {