Skip to main content

@rtorcato/js-common / crypto

crypto

Functions

hashString()

hashString(str, algorithm?): string

Defined in: crypto/index.ts:17

Hashes a string using the specified algorithm.

Example

hashString('hello')
// '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824'
hashString('hello', 'md5') // '5d41402abc4b2a76b9719d911017c592'

Parameters

str

string

The string to hash.

algorithm?

string = 'sha256'

The hash algorithm (default: 'sha256').

Returns

string

The hex-encoded hash.


randomHex()

randomHex(length?): string

Defined in: crypto/index.ts:33

Generates a random hex string of the specified length (in bytes).

Example

randomHex(4) // '9f3c1ab7' (8 hex chars)
randomHex() // 32 hex chars (16 bytes)

Parameters

length?

number = 16

The number of bytes (not hex chars).

Returns

string

A random hex string.


hmacHash()

hmacHash(str, secret, algorithm?): string

Defined in: crypto/index.ts:52

Creates an HMAC hash of a string using a secret and algorithm.

Example

hmacHash('payload', 's3cret')
// 'd1d0f5b6...' (64 hex chars, stable for the same input + secret)
hmacHash('payload', 's3cret', 'sha1') // 40 hex chars

Parameters

str

string

The string to hash.

secret

string

The secret key.

algorithm?

string = 'sha256'

The hash algorithm (default: 'sha256').

Returns

string

The hex-encoded HMAC.


base64Encode()

base64Encode(str): string

Defined in: crypto/index.ts:61

Encodes a string to base64.

Parameters

str

string

The string to encode.

Returns

string

The base64-encoded string.


base64Decode()

base64Decode(b64): string

Defined in: crypto/index.ts:70

Decodes a base64 string.

Parameters

b64

string

The base64 string to decode.

Returns

string

The decoded string.