What is otpauth?
The otpauth npm package is a library for generating and validating one-time passwords (OTPs) using the HOTP (HMAC-based One-Time Password) and TOTP (Time-based One-Time Password) algorithms. It is useful for implementing two-factor authentication (2FA) in applications.
What are otpauth's main functionalities?
Generate TOTP
This feature allows you to generate a time-based one-time password (TOTP). The code sample demonstrates how to create a TOTP instance with a secret key and generate a TOTP.
const { TOTP } = require('otpauth');
const totp = new TOTP({ secret: 'KVKFKRCPNZQUYMLXOVYDSQKJKZDTSRLD' });
console.log(totp.generate());
Validate TOTP
This feature allows you to validate a time-based one-time password (TOTP). The code sample demonstrates how to create a TOTP instance, generate a TOTP, and validate it.
const { TOTP } = require('otpauth');
const totp = new TOTP({ secret: 'KVKFKRCPNZQUYMLXOVYDSQKJKZDTSRLD' });
const token = totp.generate();
console.log(totp.validate({ token }));
Generate HOTP
This feature allows you to generate an HMAC-based one-time password (HOTP). The code sample demonstrates how to create an HOTP instance with a secret key and generate an HOTP using a counter.
const { HOTP } = require('otpauth');
const hotp = new HOTP({ secret: 'KVKFKRCPNZQUYMLXOVYDSQKJKZDTSRLD' });
console.log(hotp.generate({ counter: 1 }));
Validate HOTP
This feature allows you to validate an HMAC-based one-time password (HOTP). The code sample demonstrates how to create an HOTP instance, generate an HOTP, and validate it using a counter.
const { HOTP } = require('otpauth');
const hotp = new HOTP({ secret: 'KVKFKRCPNZQUYMLXOVYDSQKJKZDTSRLD' });
const token = hotp.generate({ counter: 1 });
console.log(hotp.validate({ token, counter: 1 }));
Other packages similar to otpauth
speakeasy
Speakeasy is a library for generating and verifying one-time passwords (OTPs) using both HOTP and TOTP algorithms. It provides similar functionality to otpauth but also includes additional features such as QR code generation for easier 2FA setup.
notp
notp is a simple library for generating and verifying one-time passwords (OTPs) using the HOTP and TOTP algorithms. It is lightweight and easy to use, making it a good alternative to otpauth for basic OTP needs.
otp-generator
otp-generator is a library focused on generating one-time passwords (OTPs) with customizable length and character sets. While it does not provide validation functionality, it is useful for generating OTPs in various formats.
OTPAuth
One Time Password library for Node.js, Deno, Bun and browsers.
It supports the generation and validation of
HMAC-Based One-Time Passwords (HOTP) as specified in RFC 4226 and
Time-Based One-Time Passwords (TOTP) as specified in RFC 6238.
Frequently used in Multi-Factor Authentication (MFA) / Two-Factor Authentication (2FA) systems.
[!TIP]
You can try the library with the demo application available at otpauth.molinero.dev.
If you wish to interact with the library in your browser console, the following snippet can be used:
const OTPAuth = await import("otpauth");
Usage
This section presents an overview of the most common usage patterns, along with some security recommendations.
import * as OTPAuth from "otpauth";
let totp = new OTPAuth.TOTP({
issuer: "ACME",
label: "Alice",
algorithm: "SHA1",
digits: 6,
period: 30,
secret: "US3WHSG7X5KAPV27VANWKQHF3SH3HULL",
});
let secret = new OTPAuth.Secret({ size: 20 });
let token = totp.generate();
let delta = totp.validate({ token, window: 1 });
let seconds = totp.period - (Math.floor(Date.now() / 1000) % totp.period);
let uri = totp.toString();
totp = OTPAuth.URI.parse(uri);
import * as OTPAuth from "jsr:@hectorm/otpauth";
<script type="importmap">
{
"imports": { "otpauth": "https://cdn.jsdelivr.net/npm/otpauth@%VERSION%/dist/otpauth.esm.min.js" },
"integrity": { "https://cdn.jsdelivr.net/npm/otpauth@%VERSION%/dist/otpauth.esm.min.js": "%HASH%" }
}
</script>
<script type="module">
import * as OTPAuth from "otpauth";
</script>
<script
src="https://cdn.jsdelivr.net/npm/otpauth@%VERSION%/dist/otpauth.umd.min.js"
integrity="%HASH%"
crossorigin="anonymous"
referrerpolicy="no-referrer"
></script>
<script>
</script>
Documentation
For additional information, please refer to the documentation page at hectorm.github.io/otpauth/.
License
MIT License
© Héctor Molinero Fernández.