Crypto

interface

interface Crypto

This Web Crypto API interface provides basic cryptographic functionality. It is accessed via the global crypto property, which gives access to cryptographically strong random number generation and to the low-level primitives exposed by SubtleCrypto through Crypto.subtle.

variable

var Crypto: { … }

The constructor object for Crypto.

The Crypto instance is accessed via the global crypto property rather than constructed directly, so calling the constructor throws.

Constructors

constructor

new Crypto(): never

Properties

subtle

readonly subtle: SubtleCrypto

prototype

readonly prototype: Crypto

Methods

getRandomValues

getRandomValues<T extends ArrayBufferView>(array: T): T

Mutates the provided typed array with cryptographically secure random values.

Returns

The same typed array, now populated with random values.

Example

const array = new Uint32Array(4);
crypto.getRandomValues(array);
console.log(array);
// output: Uint32Array(4) [ 3629234207, 1947236412, 3171234560, 4294901234 ]

See also

randomUUID

randomUUID(): `${string}-${string}-${string}-${string}-${string}`

Generates a random RFC 4122 version 4 UUID using a cryptographically secure random number generator.

Returns

A randomly generated, 36-character long v4 UUID.

Example

const uuid = crypto.randomUUID();
console.log(uuid);
// Example output: '36b8f84d-df4e-4d49-b662-bcde71a8764f'

The randomUUID method generates a version 4 UUID, which is purely random. If you require other versions of UUIDs, such as time-based (v1) or name-based (v3 and v5), consider using the @ende/uuid package available at https://jsr.io/@ende/uuid.

Example

import { v1 } from 'jsr:@ende/uuid';

// Generate a time-based UUID (v1)
const uuidV1 = v1.generate();
console.log(uuidV1);
// output: 'a0c74f7e-82f1-11eb-8dcd-0242ac130003'

See also

crypto

variable

var crypto: Crypto

The global instance of Crypto that provides access to the Web Crypto API, including cryptographically secure random number generation via Crypto.getRandomValues, random UUID generation via Crypto.randomUUID, and the low-level primitives exposed by SubtleCrypto through Crypto.subtle.