Catalog npm
Back to packageRELEASE DETAILS

1.1.11

npm

2fa-utils

TOTP and HOTP utilities.

2fahotpotptotptwo factor authentication
Published1 Sept 2021
Direct dependencies16
Transitive dependenciesNot calculated

README

Current package documentation

Two-Factor Authentication

Using the algorithm provided in RFC 4226, can generate and verify HMAC-based one-time password (HOTP) and time-based one-time password (TOTP).

Features

  • Generate base-32 encoded secrets.
  • Generate HMAC-based one-time passwords (HOTP) at a specific length.
  • Generate time-based HOTPs at a specific amount of windows.
  • Verify generated tokens.

Usage

Generating Secret

For each account, a secret must be generated and shared between the server and the client. This secret will be used to create and verify HOTPs.

const secret = twoFA.generateSecret();

Generating HOTP

HOTP requires a base32-encoded secret and a counter with time-step.

// Generate base32 secret
const secret = twoFA.generateSecret();
// Create counter with 30 seconds interval 
const counter = Math.floor(Date.now() / 30000);

const hotp = twoFA.generateHOTP(secret, counter, 6)

Generating TOTP

Generating TOTP allows you to get a HOTP in a specific time window.

const secret = twoFA.generateSecret();

// Get the current time window's token
const currentTotp = twoFA.generateTOTP(secret, 0);

// Get the future time window's token (1 window ahead)
const futureTotp = twoFA.generateTOTP(secret, 1);

// Get the past time window's token (1 window behind)
const pastTotp = twoFA.generateTOTP(secret, -1);

Verifying HOTP

Verify tokens supplied via user input.

function verifyHOTP(inputToken) {
  const secret = twoFA.generateSecret();
  const counter = Math.floor(Date.now() / 30000);

  // Actual token generated by the server
  const actualToken = twoFA.generateHOTP(secret, counter)

  if (inputToken === actualToken) return true;
  return false
}

Verifying TOTP

Verify tokens supploed via user input with a time tolerance.

const secret = twoFA.generateSecret();

const inputToken = '111111';

/* This will return true if the input token 
  - is currently valid,
  - was previously valid in the last window,
  - will be valid in the next window.
*/
const isTokenValid = twoFA.verifyTOTP(inputToken, secret, 1);

Dependencies

Author

  • Boran Seckin

License

This project is licensed under the MIT License - see the LICENSE file for details.

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 16

Native requirements
16 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
hi-base32
Environment
{
  "local_name": "hi-base32",
  "alias": false,
  "peer_metadata": null
}
^0.5.0runtime
jest
Environment
{
  "local_name": "jest",
  "alias": false,
  "peer_metadata": null
}
^25.5.4development
eslint
Environment
{
  "local_name": "eslint",
  "alias": false,
  "peer_metadata": null
}
^6.8.0development
lodash
Environment
{
  "local_name": "lodash",
  "alias": false,
  "peer_metadata": null
}
^4.17.21development
codecov
Environment
{
  "local_name": "codecov",
  "alias": false,
  "peer_metadata": null
}
^3.7.2development
ts-jest
Environment
{
  "local_name": "ts-jest",
  "alias": false,
  "peer_metadata": null
}
^25.5.1development
typedoc
Environment
{
  "local_name": "typedoc",
  "alias": false,
  "peer_metadata": null
}
^0.17.8development
typescript
Environment
{
  "local_name": "typescript",
  "alias": false,
  "peer_metadata": null
}
^3.9.7development
@types/jest
Environment
{
  "local_name": "@types/jest",
  "alias": false,
  "peer_metadata": null
}
^25.2.3development
@types/node
Environment
{
  "local_name": "@types/node",
  "alias": false,
  "peer_metadata": null
}
^13.13.21development
eslint-plugin-jest
Environment
{
  "local_name": "eslint-plugin-jest",
  "alias": false,
  "peer_metadata": null
}
^23.20.0development
eslint-plugin-import
Environment
{
  "local_name": "eslint-plugin-import",
  "alias": false,
  "peer_metadata": null
}
^2.22.1development
@typescript-eslint/parser
Environment
{
  "local_name": "@typescript-eslint/parser",
  "alias": false,
  "peer_metadata": null
}
^2.34.0development
eslint-config-airbnb-base
Environment
{
  "local_name": "eslint-config-airbnb-base",
  "alias": false,
  "peer_metadata": null
}
^14.2.0development
eslint-config-airbnb-typescript
Environment
{
  "local_name": "eslint-config-airbnb-typescript",
  "alias": false,
  "peer_metadata": null
}
^7.2.0development
@typescript-eslint/eslint-plugin
Environment
{
  "local_name": "@typescript-eslint/eslint-plugin",
  "alias": false,
  "peer_metadata": null
}
^2.34.0development

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "shasum": "78f0d651db35ba315f53423058e9d7fc96df204a",
  "tarball": "https://registry.npmjs.org/2fa-utils/-/2fa-utils-1.1.11.tgz",
  "fileCount": 6,
  "integrity": "sha512-tEsTOKt20rbpCibLr2Gm+62/p/qfxYSBRWMtYRpwQTdTDcd46DP3dMAuocZs4qqos2vs2fM8g2rsRrLlxLtOxg==",
  "signatures": [
    {
      "sig": "MEUCIBiAz4XVtCUGjSLGjl79dDPrSplau/+fsAzr8CL8UlmGAiEAlAVJdyndwTIc9gaSYw//I4zfCs66kKYL/LJ8unjA5V4=",
      "keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"
    }
  ],
  "unpackedSize": 15706,
  "npm-signature": "-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhLyzOCRA9TVsSAnZWagAARiwP/RL3Jg4vSphhDnFtUTPl\n9dkdX41dPMMeEHiosDyxU1GVW2rhZ/gShkChzicHaW6YGNJ480u5cRteatA7\nsNdaWM9DPJm8i4vaeTq2Ol05w/WbBZGBN5uTqL22nwqUOrehwmbm8fgMzwil\nC3hgnbu6E5vCw6QOdhnXarnSWekizKo3SfISCWW7ykv98sHZjdyM3ons2P3X\nK8nH0xsBlZOkdXl9/AFFKZcHdKabG9TYaLCu7FXZMVfvuN5qfZbmAVka+65y\nVsfDeka+Hc9Ltb8autKop3bUqsCJujx/SsSRbg2+u2r1aN6rnMfdEfPrneV5\nCzHJzJHTJm3zqG7ofLs+U4l4UKtD5HgS9L8BkiYcXRX+pmbC5k87xWruKdMU\n7VVRNJylsMpAcrbwIYoHwaxTUltlNV5n0/CGC5gxU24Avy64ieU2QXco+PzJ\n6y1IIJO++GDinKlFX/FIrCxFhsPcaXSeDp4NgZlY7XHBeIU3eRKQXu0/6hHx\nkDz0dVw0RnXEYnVyNdCsQ6IUfeV7eqoInuBJM8XegkhsizDjlky+MMVPLGXw\nR+x825NeI9oHq5rzjR21bSoP4xiWoKjMkkgbCf8tvtGkyE1jyx0A7tUnQNrZ\n3HRYmzDuQlu2XM73Z7lbCnKwm8x9Wnv3ZNk07smmq/UoDbQy72WCdyg8RN/g\n9txa\r\n=l39b\r\n-----END PGP SIGNATURE-----\r\n"
}

Repository

ACCESSIBLE
boranseckin/2fa-utils

Shared repository data · GitHub

License detected in repository
MIT
License branch / ref
master
Last push
17 Oct 2026
Archived
No
Contributor identities
— · partial collection

Public sponsor counts have not been collected. These signals belong to the repository owner.

Downloads & popularity

3count
Gauge · observed snapshots21 Oct 2026
31.50
2026-10-21: 3 · observed 2026-10-21T11:29:11.1849273Z
21 Oct21 Oct

github · 1 observations shown. Missing days are unknown.

View observed values
Date / source periodValueObserved at
2026-10-2132026-10-21T11:29:11.1849273Z

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 19:12 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:14b41fd8a92a7c2920be488e5c223a113f47a164edfb02aefd83ca6811a22d95

Other collected versions

1.2.722 Aug1.2.620 Oct1.2.524 Jun1.2.417 Apr1.2.325 Feb1.2.26 NovView all collected versions →