Skip to content

otplib API Documentation / @otplib/plugin-crypto-node / NodeCryptoPlugin

Class: NodeCryptoPlugin

Defined in: index.ts:56

Node.js crypto module implementation of CryptoPlugin

This plugin uses Node.js's built-in crypto module which provides:

  • OpenSSL-backed HMAC operations
  • Cryptographically secure random byte generation
  • Synchronous API for optimal performance

Example

ts
import { NodeCryptoPlugin } from '@otplib/plugin-crypto-node';

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

Implements

Constructors

Constructor

new NodeCryptoPlugin(): NodeCryptoPlugin

Returns

NodeCryptoPlugin

Properties

algorithms

readonly algorithms: readonly HashAlgorithm[] = SUPPORTED_ALGORITHMS

Defined in: index.ts:65

Algorithms this plugin can compute

Implementation of

CryptoPlugin.algorithms


name

readonly name: "node" = "node"

Defined in: index.ts:60

Plugin name for identification

Implementation of

CryptoPlugin.name

Methods

constantTimeEqual()

constantTimeEqual(a, b): boolean

Defined in: index.ts:115

Constant-time comparison using Node.js crypto.timingSafeEqual

Uses Node.js's built-in timing-safe comparison which prevents timing side-channel attacks.

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: index.ts:83

Compute HMAC using Node.js crypto module

Synchronous implementation using createHmac.

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 OpenSSL happens to support - 'md5', 'sha224', 'ripemd160' - throws AlgorithmUnsupportedError.

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: index.ts:101

Generate cryptographically secure random bytes

Uses Node.js's randomBytes which is backed by OpenSSL.

Parameters

length

number

Number of bytes to generate

Returns

Uint8Array

Random bytes

Implementation of

CryptoPlugin.randomBytes

Released under the MIT License.