Skip to content
velt/crypto exports

velt/crypto

API reference

SHA-256 and SHA-1, HMAC, secure randomness and constant-time comparison: what a program needs to sign and check tokens and webhooks, store content by hash, or make session ids. For a walkthrough, read the guide.

import { sha256Hex, hmacSha256, randomBytes } from "velt/crypto";

Exports

function

timingSafeEqual

#std/crypto/compare.vlt:7

Re-exported from velt/crypto/compare

function timingSafeEqual(a: u8[], b: u8[]): bool

Whether a and b are equal, in time that depends only on their lengths (not on where they differ), for comparing MACs and tokens. Different lengths return false at once.

function

hmacSha1

#std/crypto/hmac.vlt:23

Re-exported from velt/crypto/hmac

function hmacSha1(key: u8[], data: u8[]): u8[]

HMAC-SHA1 (RFC 2104 / RFC 2202) of data under key.

function

hmacSha256

#std/crypto/hmac.vlt:11

Re-exported from velt/crypto/hmac

function hmacSha256(key: u8[], data: u8[]): u8[]

HMAC-SHA256 (RFC 2104 / RFC 4231) of data under key.

function

randomBytes

#std/crypto/random.vlt:7

Re-exported from velt/crypto/random

function randomBytes(n: usize): u8[]

n cryptographically secure random bytes (OS generator).

function

randomInt

#std/crypto/random.vlt:22

Re-exported from velt/crypto/random

function randomInt(min: i64, max: i64): i64

A uniformly distributed secure random integer in [min, max) (Node's crypto.randomInt); rejection sampling avoids modulo bias. Throws CryptoError, with Node's messages, when min or max is not a safe integer, max <= min, or max - min is 2^48 or more.

Re-exported from velt/crypto/sha1

function sha1(data: u8[]): u8[]

SHA-1 digest (20 bytes) of data. Not collision resistant; see the module comment.

Re-exported from velt/crypto/sha1

function sha1Hex(s: string): string

Lowercase hex SHA-1 of the UTF-8 bytes of s.

Re-exported from velt/crypto/sha256

function sha256(data: u8[]): u8[]

SHA-256 digest (32 bytes) of data.

function

sha256Hex

#std/crypto/sha256.vlt:53

Re-exported from velt/crypto/sha256

function sha256Hex(s: string): string

Lowercase hex SHA-256 of the UTF-8 bytes of s.

class

CryptoError

#std/crypto/errors.vlt:4

Re-exported from velt/crypto/errors

class CryptoError extends Error

Thrown by randomInt for an empty or too large range, or a bound that is not a safe integer.

constructor(message: string)No doc comment yet