Crypto
The Crypto module provides functions for common cryptographic operations, including:
- Encoding/decoding (Base64, Hex, UTF-8)
- Unique identifier generation
- Utilities for buffer manipulation
- Validation and verification (Base64, Hex, Hash, UUID)
All functions are designed to work both in the browser and in Node.js, using native cryptography APIs when available.
Overview
base64Decode(input: string, urlSafe?: boolean): Uint8ArrayDecodes a Base64 or Base64URL string to a Uint8Array.
base64Encode(input: Uint8Array | ArrayBuffer, urlSafe?: boolean): stringEncodes an ArrayBuffer or Uint8Array into a Base64 or Base64URL string.
djb2(str: string): numberCalculates the hash of a string using the djb2 algorithm.
generateNonce(length?: number): Uint8ArrayGenerates a random byte vector (nonce) of the specified length.
generateUUID(): stringGenerates a version 4 UUID (Universally Unique Identifier).
hexDecode(hex: string): Uint8ArrayDecodes a hexadecimal string to a Uint8Array.
hexEncode(data: ArrayBuffer | Uint8Array): stringEncodes an ArrayBuffer or Uint8Array into a hexadecimal string.
isArrayBuffer(input: unknown): input is ArrayBufferChecks if the provided value is an ArrayBuffer.
isBase64(value: string): booleanChecks if a string is Base64 encoded.
isBase64URL(value: string): booleanChecks if a string is Base64URL encoded.
isHash(hash: string, algorithm: HashAlgorithm = 'SHA-256'): booleanChecks if a string is a valid hash for the specified algorithm.
isHex(value: string): booleanChecks if a string contains only hexadecimal characters.
isUint8Array(input: unknown): input is Uint8ArrayChecks if the provided value is a Uint8Array.
isUUID(value: string): booleanChecks if a string is in UUID (Universally Unique Identifier) format.
utf8Decode(data: ArrayBuffer | Uint8Array): stringDecodes an ArrayBuffer or Uint8Array to a UTF-8 string.
utf8Encode(input: string): Uint8ArrayEncodes a string in UTF-8, returning a Uint8Array.
