Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@open-charging-cloud/totp

TOTP is a TypeScript library for creating Time-based One-Time Passwords (TOTPs).

https://www.npmjs.com/package/@open-charging-cloud/totp

It can be used e.g. for Secure Dynamic QR-Codes in E-Mobility or a secure alternative for legacy HTTP BASIC Authentication mechanisms using Authorization: TOTP <token> <totp>.

Installation

npm install @open-charging-cloud/totp

Usage

import { generateTOTPs } from "@open-charging-cloud/totp";

const totps = generateTOTPs("secure!Charging!");

console.log(totps.current);
console.log(totps.remainingTime);

You can also pass an options object:

const totps = generateTOTPs({
  sharedSecret:  "secure!Charging!",
  validityTime:   30,
  totpLength:     12,
  alphabet:      "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
  timestamp:      Date.now(),
  hashAlgorithm: "sha256"
});

API

generateTOTPs(sharedSecret, validityTime, totpLength, alphabet, timestamp, hashAlgorithm)

Generates TOTP values for the previous, current, and next time slot.

Parameters:

Parameter Description Default
sharedSecret Shared secret string, at least 16 characters, without whitespace. Required
validityTime Slot duration in seconds. 30
totpLength Generated token length. 12
alphabet Alphabet used for token characters. Digits, lowercase letters, and uppercase letters
timestamp Unix timestamp in milliseconds or Date. Date.now()
hashAlgorithm HMAC hash algorithm. Must be sha256, sha384, or sha512. sha256

Returns:

{
  previous:       string;
  current:        string;
  next:           string;
  remainingTime:  number;
}

Development

npm install
npm test

About

A TypeScript library for creating Time-based One-Time Passwords (TOTPs).

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages