Skip to content
Visal .In edited this page Aug 19, 2020 · 1 revision

The Time-based One-time Password (TOTP) is algorithm to generate a one-time password by using the current time. god_crypto has TOTP implemented out of the box.

import { TOTP } from "https://deno.land/x/god_crypto/otp.ts";

Overview


Usage

You need to generate the shared secret. You can generate your own secret or you can use TOTP.generateSecret() to help generate the secret. The recommended key size should be at least 160-bits (RFC 4226).

Then, you can use uri() to generate value for QR code.

import { TOTP } from "https://deno.land/x/god_crypto/otp.ts";

const secret = TOTP.generateSecret(20);
const otp = new TOTP(secret);
const qr = otp.uri("John", "God Crypto");

To verify if user input the correct code

const otp = new TOTP(secret);
if (otp.verify(user_input)) {
   console.log("Correct");
} else {
   console.log("Incorrect")
}

References

constructor

new TOTP(secret, digits = 6, algorithm = 'sha1', period = 30)
Parameters Description
secret Base32 shared secret key
digits Number of digit code should generate
algorithm Hash algorithm. Supporting SHA1 and SHA256
period Period in which new code will be generated in seconds

generateSecret

TOTP.generateSecret(numberOfBytes);

Generate base32 shared secret key from numberOfBytes random bytes.


uri

const otp = new TOTP(secret);
otp.uri(name, issuer)

Create URI. It can be used to generate QR code value to work with Google Authenticator, Microsoft Authenticator, etc...


verify

const otp = new TOTP(secret);
otp.verify(code)

Verify whether OTP code code is correct.


generate

const otp = new TOTP(secret);
otp.generate();

Generate the OTP code

About

Reference

Utility

Clone this wiki locally