Skip to content

otplib API Documentation / @otplib/core / CryptoContext

Class: CryptoContext

Defined in: packages/core/src/crypto-context.ts:9

CryptoContext provides a unified interface for crypto operations using a pluggable crypto backend

Constructors

Constructor

new CryptoContext(crypto): CryptoContext

Defined in: packages/core/src/crypto-context.ts:15

Create a new CryptoContext with the given crypto plugin

Parameters

crypto

CryptoPlugin

The crypto plugin to use

Returns

CryptoContext

Accessors

plugin

Get Signature

get plugin(): CryptoPlugin

Defined in: packages/core/src/crypto-context.ts:20

Get the underlying crypto plugin

Returns

CryptoPlugin

Methods

hmac()

hmac(algorithm, key, data): Promise<Uint8Array<ArrayBufferLike>>

Defined in: packages/core/src/crypto-context.ts:33

Compute HMAC using the configured crypto plugin

Parameters

algorithm

HashAlgorithm

The hash algorithm to use

key

Uint8Array

The secret key as a byte array

data

Uint8Array

The data to authenticate as a byte array

Returns

Promise<Uint8Array<ArrayBufferLike>>

HMAC digest as a byte array

Throws

If HMAC computation fails


hmacSync()

hmacSync(algorithm, key, data): Uint8Array

Defined in: packages/core/src/crypto-context.ts:52

Synchronous HMAC computation

Parameters

algorithm

HashAlgorithm

The hash algorithm to use

key

Uint8Array

The secret key as a byte array

data

Uint8Array

The data to authenticate as a byte array

Returns

Uint8Array

HMAC digest as a byte array

Throws

If HMAC computation fails or if crypto plugin doesn't support sync operations


randomBytes()

randomBytes(length): Uint8Array

Defined in: packages/core/src/crypto-context.ts:75

Generate cryptographically secure random bytes

Parameters

length

number

Number of random bytes to generate

Returns

Uint8Array

Random bytes

Throws

If random byte generation fails

Released under the MIT License.