Skip to content

otplib API Documentation / otplib / / NobleCryptoPlugin

Class: NobleCryptoPlugin

Defined in: packages/plugin-crypto-noble/src/index.ts:54

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

algorithms

readonly algorithms: readonly HashAlgorithm[] = SUPPORTED_ALGORITHMS

Defined in: packages/plugin-crypto-noble/src/index.ts:63

Algorithms this plugin can compute

Implementation of

CryptoPlugin.algorithms


name

readonly name: "noble" = "noble"

Defined in: packages/plugin-crypto-noble/src/index.ts:58

Plugin name for identification

Implementation of

CryptoPlugin.name

Methods

constantTimeEqual()

constantTimeEqual(a, b): boolean

Defined in: packages/plugin-crypto-noble/src/index.ts:114

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

string | Uint8Array<ArrayBufferLike>

First value to compare

b

string | Uint8Array<ArrayBufferLike>

Second value to compare

Returns

boolean

true if values are equal, false otherwise

Implementation of

CryptoPlugin.constantTimeEqual


hmac()

hmac(algorithm, key, data): Uint8Array

Defined in: packages/plugin-crypto-noble/src/index.ts:81

Compute HMAC using @noble/hashes

Synchronous implementation using pure JS.

The algorithm is matched ignoring case, with an optional - or _ before the digest size, so 'SHA1', 'Sha1' and 'SHA-1' all resolve to 'sha1'. Any other digest throws AlgorithmUnsupportedError rather than falling back to a default.

Parameters

algorithm

HashAlgorithm

Hash algorithm to use

key

Uint8Array

Secret key

data

Uint8Array

Data to authenticate

Returns

Uint8Array

HMAC digest

Throws

If the algorithm is not supported

Implementation of

CryptoPlugin.hmac


randomBytes()

randomBytes(length): Uint8Array

Defined in: packages/plugin-crypto-noble/src/index.ts:100

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.