1.0.4 • Published 4 years ago

@codejockie/otp-gen v1.0.4

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

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:

KeyTypeDefaultValue Description
keystring-Unique shared secret key for encrypting C (counter) values for HMAC algorithm
counternumber-8-byte incrementing counter value
algorithmHashAlgo"sha1"HMAC Algorithm to use
digitsnumber6Return 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:

KeyTypeDefaultValue Description
keystring-Shared secret used for encryption
epochTimenumberCurrent Unix timeUnix Epoch time, defaults to current Unix time
t0number0Unix time used to start counting time steps
timeStepXnumber30Time step in seconds, default 30s
algorithmHashAlgo"sha1"HMAC Algorithm to use
digitsnumber6Return 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.

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago