Zero-dependency
1kB gzipped
My implementation of 2FA HOTP/TOTP algorithms in TypeScript + base32 encoder for creating links for authenticator programs like Google Authenticator
Read more about otpauth://
links
You can compile .js files by command yarn build
or npm run build
And test code with yarn test
or npm test
Algorithm and length can be changed in example by passing args (both optional): yarn test sha256 16
Thanks @intech
DeprecationWarning
warninggenerateKey
functionThanks @intech
npm i 2fa-hotp-totp
or
yarn add 2fa-hotp-totp
import { HOTP, TOTP, base32, generateKey } from '2fa-hotp-totp';
OR
const { HOTP, TOTP, base32, generateKey } = require('2fa-hotp-totp');
HOTP.generate({
key: 'test',
algorithm: 'sha512', // optional
counter: 0, // optional
});
// => 941117
HOTP.validate({
token: '123123', // length must be 6
key: 'test',
algorithm: 'sha512', // optional
window: 1, // optional
counter: 0, // optional
});
// => time-step (number) or null
TOTP.generate({
key: 'test',
algorithm: 'sha512', // optional
time: 30, // optional
});
// => 432486
TOTP.validate({
token: '123123', // length must be 6
key: 'test',
algorithm: 'sha512', // optional
window: 1, // optional
time: 30, // optional
});
// => time-step (number) or null
base32('test');
// => ORSXG5A
generateKey(64);
// => <Buffer 51 84 24 8d 9a d7 2c 47>
All code also covered with JSDoc with links to specifications and its pages
Implementation of RFC 4226
HOTP(K,C) = Truncate(HMAC-SHA-1(K,C))
*Since RFC 6238, SHA256/SHA512 can be used for generating token
Arguments (object):
obj.* | Required | Description | Default |
---|---|---|---|
key |
✅ | unique secret key for user | |
algorithm |
❌ | custom algorithm for crypto.createHmac (sha1/sha256/sha512) | sha1 |
counter |
❌ | moving factor (read page 6) | 0 |
Returns string of 6 digit, because it must be always 6 digit length and first can be zero
Arguments (object):
obj.* | Required | Description | Default |
---|---|---|---|
token |
✅ | code, provided by user | |
key |
✅ | unique secret key for user | |
algorithm |
❌ | custom algorithm for crypto.createHmac (sha1/sha256/sha512) | sha1 |
window |
❌ | counter values window | 1 |
counter |
❌ | moving factor (read page 6) | 0 |
Returns null if nothing found or number between -window to +window
if same code in steps found
window
:For example, if you using TOTP (HOTP with time) with 0 window, only current XX (30 by default) second code will be checked for verification. If you set 1, neighboring seconds code (+30 and -30) also checked.
One more example with time-step 30 sec:
04:20:00 - 04:20:30
will be checked04:19:30 - 04:20:00
, 04:20:00 - 04:20:30
and 04:20:30 - 04:21:00
all steps codes (-1, 0, 1) checkedImplementation of RFC 6238
TOTP = HOTP(K, T)
Arguments (object):
obj.* | Required | Description | Default |
---|---|---|---|
key |
✅ | unique secret key for user | |
algorithm |
❌ | custom algorithm for crypto.createHmac (sha1/sha256/sha512) | sha1 |
time |
❌ | time-step in seconds (default recomended) | 30 |
Returns string of 6 digit, because it must be always 6 digit length and first can be zero
Arguments (object):
obj.* | Required | Description | Default |
---|---|---|---|
token |
✅ | code, provided by user | |
key |
✅ | unique secret key for user | |
algorithm |
❌ | custom algorithm for crypto.createHmac (sha1/sha256/sha512) | sha1 |
window |
❌ | counter values window | 1 |
time |
❌ | time-step in seconds (default recomended) | 30 |
Returns null if nothing found or number between -window to +window
if same code in steps found
Implementation of RFC 4648 without paddings (thanks to @LinusU)
Can take secret as string
or Buffer
Returns base32 encoded secret as string
Generate random key with length
Optional param length
(number
)
Returns hmac as Buffer
from generateKeySync
To all contributors
Generated using TypeDoc