Skip to content

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

typescript
base64Decode(input: string, urlSafe?: boolean): Uint8Array

Decodes a Base64 or Base64URL string to a Uint8Array.

base64Encode

typescript
base64Encode(input: Uint8Array | ArrayBuffer, urlSafe?: boolean): string

Encodes an ArrayBuffer or Uint8Array into a Base64 or Base64URL string.

djb2

typescript
djb2(str: string): number

Calculates the hash of a string using the djb2 algorithm.

generateNonce

typescript
generateNonce(length?: number): Uint8Array

Generates a random byte vector (nonce) of the specified length.

generateUUID

typescript
generateUUID(): string

Generates a version 4 UUID (Universally Unique Identifier).

hexDecode

typescript
hexDecode(hex: string): Uint8Array

Decodes a hexadecimal string to a Uint8Array.

hexEncode

typescript
hexEncode(data: ArrayBuffer | Uint8Array): string

Encodes an ArrayBuffer or Uint8Array into a hexadecimal string.

isArrayBuffer

typescript
isArrayBuffer(input: unknown): input is ArrayBuffer

Checks if the provided value is an ArrayBuffer.

isBase64

typescript
isBase64(value: string): boolean

Checks if a string is Base64 encoded.

isBase64URL

typescript
isBase64URL(value: string): boolean

Checks if a string is Base64URL encoded.

isHash

typescript
isHash(hash: string, algorithm: HashAlgorithm = 'SHA-256'): boolean

Checks if a string is a valid hash for the specified algorithm.

isHex

typescript
isHex(value: string): boolean

Checks if a string contains only hexadecimal characters.

isUint8Array

typescript
isUint8Array(input: unknown): input is Uint8Array

Checks if the provided value is a Uint8Array.

isUUID

typescript
isUUID(value: string): boolean

Checks if a string is in UUID (Universally Unique Identifier) format.

utf8Decode

typescript
utf8Decode(data: ArrayBuffer | Uint8Array): string

Decodes an ArrayBuffer or Uint8Array to a UTF-8 string.

utf8Encode

typescript
utf8Encode(input: string): Uint8Array

Encodes a string in UTF-8, returning a Uint8Array.

Released under the MIT License.