Skip to content

otplib API Documentation / @otplib/plugin-crypto-noble / NobleCryptoPlugin

Class: NobleCryptoPlugin

Defined in: index.ts:27

Pure JavaScript implementation of CryptoPlugin

This plugin uses @noble/hashes which provides:

  • Pure JavaScript implementations of hash functions
  • Zero dependencies and audited code
  • Cross-platform compatibility (Node.js, browser, edge)
  • Fallback for environments without native crypto APIs

Example

ts
import { NobleCryptoPlugin } from '@otplib/plugin-crypto-noble';

const crypto = new NobleCryptoPlugin();
const hmac = crypto.hmac('sha1', key, data);
const random = crypto.randomBytes(20);

Implements

Constructors

Constructor

new NobleCryptoPlugin(): NobleCryptoPlugin

Returns

NobleCryptoPlugin

Properties

name

readonly name: "noble" = "noble"

Defined in: index.ts:31

Plugin name for identification

Implementation of

CryptoPlugin.name

Methods

constantTimeEqual()

constantTimeEqual(a, b): boolean

Defined in: index.ts:73

Constant-time comparison to prevent timing side-channel attacks

@noble/hashes doesn't provide a constant-time comparison, so we Use the core utility implementation.

Parameters

a

First value to compare

string | Uint8Array<ArrayBufferLike>

b

Second value to compare

string | Uint8Array<ArrayBufferLike>

Returns

boolean

true if values are equal, false otherwise

Implementation of

CryptoPlugin.constantTimeEqual


hmac()

hmac(algorithm, key, data): Uint8Array

Defined in: index.ts:43

Compute HMAC using @noble/hashes

Synchronous implementation using pure JS.

Parameters

algorithm

Hash algorithm to use

"sha1" | "sha256" | "sha512"

key

Uint8Array

Secret key

data

Uint8Array

Data to authenticate

Returns

Uint8Array

HMAC digest

Implementation of

CryptoPlugin.hmac


randomBytes()

randomBytes(length): Uint8Array

Defined in: index.ts:59

Generate cryptographically secure random bytes

Uses @noble/hashes' randomBytes which is backed by:

  • Node.js crypto.randomBytes in Node.js
  • crypto.getRandomValues in browsers
  • A PRNG as fallback

Parameters

length

number

Number of bytes to generate

Returns

Uint8Array

Random bytes

Implementation of

CryptoPlugin.randomBytes

Released under the MIT License.