Skip to content

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

Class: NodeCryptoPlugin

Defined in: index.ts:22

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

name

readonly name: "node" = "node"

Defined in: index.ts:26

Plugin name for identification

Implementation of

CryptoPlugin.name

Methods

constantTimeEqual()

constantTimeEqual(a, b): boolean

Defined in: index.ts:66

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

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

Compute HMAC using Node.js crypto module

Synchronous implementation using createHmac.

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

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.