@open-charging-cloud/totp
v0.2.0
Published
Time-based one-time password generator for Open Charging Cloud applications.
Downloads
221
Maintainers
Readme
@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/totpUsage
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