Socket
Book a DemoInstallSign in
Socket

gen-totp

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gen-totp

A time-based One-time Password generator that uses current time as a source of uniqueness, following RFC 6238.

latest
Source
npmnpm
Version
2.0.7
Version published
Weekly downloads
4.5K
64742.86%
Maintainers
1
Weekly downloads
 
Created
Source

Generate TOTP

npm version npm downloads

Time-based One-Time Password (TOTP) is an algorithm that generates a one-time password based on the current time. TOTP is an extension of the HMAC-based One-Time Password (HOTP) algorithm and is standardized in RFC 6238. For more details, see Wikipedia.

Table of Contents

Installation

You can install gen-totp via npm or yarn:

npm install gen-totp
# or
yarn add gen-totp

Usage

Basic Usage

import genTOTP from 'gen-totp';

const otp = genTOTP('test-key');
// Returns a 6-digit OTP by default
console.log(otp);

Customizing OTP Length

import genTOTP from 'gen-totp';

const otp = genTOTP('test-key', { digits: 4 });
// Returns a 4-digit OTP
console.log(otp);

Options

The genTOTP function accepts two parameters:

  • key: A string containing the base32-encoded secret key. It can include numbers, uppercase letters, _, and -.
  • options: An optional object to customize the OTP generation. The available options are detailed in the table below:
OptionTypeDefaultDescription
digitsnumber6The number of digits in the generated OTP.
periodnumber30The time period (in seconds) after which a new OTP is generated.
algorithmstring'SHA-1'The hashing algorithm used to generate the OTP. Supported algorithms include:
- SHA-1
- SHA-224
- SHA-256
- SHA-384
- SHA-512
- SHA3-224
- SHA3-256
- SHA3-384
- SHA3-512
For more details, refer to the JsSHA documentation.

Key Format and Encoding

When providing a key to the genTOTP function, you can use different encodings specified by the encoding option:

  • UTF-8 Encoding (default):

    • Any valid UTF-8 string can be used as the key
    • Supports alphabetic characters (A-Z, a-z), numeric characters (0-9), special characters, and Unicode characters including emoji
  • Hex Encoding :

    • The key should be a valid hexadecimal string
    • Only characters 0-9 and a-f (case insensitive) are allowed
  • Base32 Encoding :

    • The key should be a valid base32 string according to RFC 4648
    • Only uppercase letters A-Z and digits 2-7 are allowed
    • Padding with '=' is optional Example of valid keys:
  • UTF-8: mySecureKey123! , secretKey你好 , emojiKey😊🔑
  • Hex: deadbeef1234 , 01a2b3c4d5e6f7
  • Base32: JBSWY3DPEHPK3PXP , GEZDGNBVGY3TQOJQ

Documentation

For more detailed documentation, visit the Official Documentation .

Contributing

Contributions are welcome! If you have any bug reports, suggestions, or feature requests, please open an issue on GitHub.

To contribute:

  • Fork the repository
  • Create a new feature branch ( git checkout -b feature/new-feature )
  • Commit your changes ( git commit -m 'Add new feature' )
  • Push to the branch ( git push origin feature/new-feature )
  • Create a new Pull Request Make sure to follow the Contributor Covenant Code of Conduct when participating in the project.

Keywords

otp

FAQs

Package last updated on 21 May 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts