Coder Social home page Coder Social logo

otp-gen's Introduction

OTP Gen

Build Status codecov npm

A One Time Password generator with a customisable implementation of the HMAC based One Time Password (HOTP) and Time based One Time Password (TOTP).

Installation

npm install @codejockie/otp-gen

Code Sample:

  import { hotp, totp } from "@codejockie/otp-gen"

  // HOTP
  const hotpToken = hotp({ key: "shared_secret", counter: 20 })

  // TOTP
  const totpToken = totp({ key: "shared_secret" })

API

HOTP

HMAC based One Time Password (HOTP) function takes an object as an argument as follows:

Key Type Default Value Description
key string - Unique shared secret key for encrypting C (counter) values for HMAC algorithm
counter number - 8-byte incrementing counter value
algorithm HashAlgo "sha1" HMAC Algorithm to use
digits number 6 Return digits of HOTP value, according to RFC4226, length >= 6

TOTP

Time based One Time Password (TOTP) function takes an object as an argument as follows:

Key Type Default Value Description
key string - Shared secret used for encryption
epochTime number Current Unix time Unix Epoch time, defaults to current Unix time
t0 number 0 Unix time used to start counting time steps
timeStepX number 30 Time step in seconds, default 30s
algorithm HashAlgo "sha1" HMAC Algorithm to use
digits number 6 Return digits of TOTP value, according to RFC6238, length >= 6

HashAlgo is an enum consisting of these values: "sha1" | "sha256" | "sha512"

Examples:

Using a different hashing algorithm

import { hotp } from "@codejockie/otp-gen"

hotp({
  key: "shared_secret",
  counter: 12345,
  algorithm: "sha256",
})

Using a different return digits

import { totp } from "@codejockie/otp-gen"

hotp({
  key: "shared_secret",
  counter: 12345,
  digits: 10,
})

Using a different unix time

import { totp } from "@codejockie/otp-gen"

totp({
  key: "shared_secret",
  epochTime: 123456
})

Using a different T0 and Time Step X

import { totp } from "@codejockie/otp-gen"

totp({
  key: "shared_secret",
  t0: 10,
  timeStepX: 60,
})

TypeScript

This library has first hand support for TS.

TypeScript Usage

  import { HashAlgo, hotp, totp } from "@codejockie/otp-gen"

  // HOTP
  const hotpToken = hotp({ key: "shared_secret", counter: 20, algorithm: HashAlgo.SHA512 })

  // TOTP
  const totpToken = totp({ key: "shared_secret", algorithm: HashAlgo.SHA512 }) 

Contributing

Pull Requests are welcomed!

Issues

Please file an issue if you encounter any.

Buy Me A Coffee

otp-gen's People

Contributors

codejockie avatar dependabot[bot] avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.