OTPAuth

One Time Password (HOTP/TOTP) library for Node.js, Deno, Bun and browsers.

README


OTPAuth


One Time Password (HOTP/TOTP) library for Node.js, Deno, Bun and browsers.

Usage


Node.js


  1. ```javascript
  2. import * as OTPAuth from "otpauth";

  3. // Create a new TOTP object.
  4. let totp = new OTPAuth.TOTP({
  5.   issuer: "ACME",
  6.   label: "AzureDiamond",
  7.   algorithm: "SHA1",
  8.   digits: 6,
  9.   period: 30,
  10.   secret: "NB2W45DFOIZA", // or 'OTPAuth.Secret.fromBase32("NB2W45DFOIZA")'
  11. });

  12. // Generate a token (returns the current token as a string).
  13. let token = totp.generate();

  14. // Validate a token (returns the token delta or null if it is not found in the search window, in which case it should be considered invalid).
  15. let delta = totp.validate({ token, window: 1 });

  16. // Convert to Google Authenticator key URI:
  17. // otpauth://totp/ACME:AzureDiamond?issuer=ACME&secret=NB2W45DFOIZA&algorithm=SHA1&digits=6&period=30
  18. let uri = totp.toString(); // or 'OTPAuth.URI.stringify(totp)'

  19. // Convert from Google Authenticator key URI.
  20. totp = OTPAuth.URI.parse(uri);
  21. ```

Deno


  1. ```javascript
  2. import * as OTPAuth from "https://deno.land/x/otpauth@VERSION/dist/otpauth.esm.js"

  3. // Same as above.
  4. ```

Bun


  1. ```javascript
  2. import * as OTPAuth from "otpauth";

  3. // Same as above.
  4. ```

Browsers


  1. ```html
  2. <script src="https://cdnjs.cloudflare.com/ajax/libs/otpauth/VERSION/otpauth.umd.min.js"></script>
  3. <script>
  4.   // Same as above.
  5. </script>
  6. ```

Documentation


See the documentation page.


Supported hashing algorithms


In Node.js, the same algorithms as
[Crypto.createHmac](https://nodejs.org/api/crypto.html#crypto_crypto_createhmac_algorithm_key_options)
function are supported, since it is used internally. In Deno, Bun and browsers, the SHA1, SHA224, SHA256, SHA384,
SHA512, SHA3-224, SHA3-256, SHA3-384 and SHA3-512 algorithms are supported by using the
jsSHA library.

License