Skip to content

otplib API Documentation / @otplib/core / / CryptoContext

Class: CryptoContext ​

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

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:20

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:25

Get the underlying crypto plugin

Returns ​

CryptoPlugin

Methods ​

hmac() ​

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

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

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 the algorithm is not supported

Throws ​

If HMAC computation fails


hmacSync() ​

hmacSync(algorithm, key, data): Uint8Array

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

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 the algorithm is not supported

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:107

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.