otplib API Documentation / @otplib/plugin-crypto-web / WebCryptoPlugin
Class: WebCryptoPlugin
Defined in: index.ts:62
Web Crypto API implementation of CryptoPlugin
This plugin uses the browser's native Web Crypto API which provides:
- Hardware-accelerated cryptographic operations
- Secure key storage and generation
- Async API for non-blocking operations
Example
import { WebCryptoPlugin } from '@otplib/plugin-crypto-web';
const crypto = new WebCryptoPlugin();
const hmac = await crypto.hmac('sha1', key, data);
const random = crypto.randomBytes(20);Implements
Constructors
Constructor
new WebCryptoPlugin():
WebCryptoPlugin
Returns
WebCryptoPlugin
Properties
algorithms
readonlyalgorithms: readonlyHashAlgorithm[] =SUPPORTED_ALGORITHMS
Defined in: index.ts:71
Algorithms this plugin can compute
Implementation of
CryptoPlugin.algorithms
name
readonlyname:"web"="web"
Defined in: index.ts:66
Plugin name for identification
Implementation of
CryptoPlugin.name
Methods
constantTimeEqual()
constantTimeEqual(
a,b):boolean
Defined in: index.ts:145
Constant-time comparison to prevent timing side-channel attacks
Web Crypto API doesn't provide a built-in 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):Promise<Uint8Array<ArrayBufferLike>>
Defined in: index.ts:89
Compute HMAC using Web Crypto API
Async implementation using SubtleCrypto.
The algorithm is matched ignoring case, with an optional - or _ before the digest size, so 'SHA1', 'Sha1' and Web Crypto's own 'SHA-1' all resolve to 'sha1'. Any other digest - including ones SubtleCrypto supports, such as 'SHA-384' - throws AlgorithmUnsupportedError.
Parameters
algorithm
Hash algorithm to use
key
Uint8Array
Secret key
data
Uint8Array
Data to authenticate
Returns
Promise<Uint8Array<ArrayBufferLike>>
HMAC digest
Throws
If the algorithm is not supported
Implementation of
CryptoPlugin.hmac
randomBytes()
randomBytes(
length):Uint8Array
Defined in: index.ts:123
Generate cryptographically secure random bytes
Uses Web Crypto API's getRandomValues.
Parameters
length
number
Number of bytes to generate
Returns
Uint8Array
Random bytes
Implementation of
CryptoPlugin.randomBytes