✨ types(hooks): crypto.randomBytes und randomToken (#61)

randomBytes gibt je nach encoding Int8Array oder string zurück,
randomToken immer string (Default ungepolstertes base64url). Der neue Typ
ByteEncoding (hex|base64|base64url|bytes) wird von den kommenden
base64-/crypto-Erweiterungen mitgenutzt.

Refs cms/tibi-server#61
This commit is contained in:
apairon committed 2026-11-04 13:22:30 +00:00
1 parent eb681a73da
commit 779dd574fc
1 file changed
+39
Vendored
+39
View File
@@ -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 {