Skip to content

wzychla/tiny-totp.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tiny-totp.js

An RFC 6328 compliant Two Factor authentication code generator. Implemented purely in Javascript. There are existing generators, e.g. the totp.js however, usually they depend on external libraries that support at least

  • base32 encoding and decoding
  • hmacsha1 signatures

The totp_light has no additional external dependencies, all you need is the totp.js.

Features

  • works in node.js (uses crypto module)
  • works in a browser (uses subtle.crypto)
  • TOTP code generation based on known master key (these are usually base32 encoded)
  • no QR codes or any extra stuff

Examples

const key  = '2JLXFRTKDX7EVJ2ZRETEW655JA';
const totp = new TOTP(key);

// generate code
const code = await totp.gen();
console.log( code );

Q&A

Q: Does this implementation work with Google/Microsoft Authenticator applications?

A: Yes, this implementation works correctly as a code generator for Google/Microsoft Authenticators.

Q: Should I use it server side or client side?

A: Both, depending on what you want to do. If you implement a server-side node.js app, you generate codes server side and validate codes sent by users (which are generated at client-side with compliant applications like Google/Microsoft Authenticator). If you implement a client-side replacement for Google/Microsoft Authenticator, codes generated with this library will be exactly the same as codes generated with Google/Microsoft Authenticators.

Repository content

  • totp.js - actual TOTP implementation
  • test.html + client.js - browser demo
  • app.js - node.js demo

Version history and TODOs

1.0

Initial release

Todos

  • validation should consider a time window to catch codes that are ±5 minutes from the current code

About

A tiny javascript TOTP implementation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published