Skip to main content

otp_auth 1.0.3 copy "otp_auth: ^1.0.3" to clipboard
otp_auth: ^1.0.3 copied to clipboard

A Dart package for generating and verifying HOTP and TOTP one-time passwords using RFC 4226 and RFC 6238.

otp_auth #

A Dart package for generating and verifying HOTP and TOTP one-time passwords using RFC 4226 and RFC 6238.

pub package likes downloads pub points CI

Quick start #

dependencies:
  otp_auth: ^1.0.3
import 'package:otp_auth/otp_auth.dart';

final totp = TOTP(secret: 'JBSWY3DPEHPK3PXP');
print(totp.now());              // e.g. '492039'
print(TOTP.format(totp.now())); // e.g. '492 039'
print(totp.remaining);          // seconds until expiry
print(totp.verify('492039'));   // true if the code matches the current step, or one step either side

API #

Class Description
TOTP Time-based one-time password (RFC 6238). now(), at(DateTime), verify(), remaining, format().
HOTP Counter-based one-time password (RFC 4226). at(int), verify().
OTPUri Parses and builds otpauth:// URIs. parse(), extractSecret(), toTOTP(), toHOTP().
Base32 RFC 4648 Base32 codec. encode(), decode().
OTPAlgorithm sha1, sha256, sha512.

Parameters #

TOTP #

Parameter Type Default Description
secret String required Base32-encoded shared secret
digits int 6 Number of digits in the output code
algorithm OTPAlgorithm sha1 Hash algorithm
period int 30 Time step in seconds

HOTP #

Parameter Type Default Description
secret String required Base32-encoded shared secret
digits int 6 Number of digits in the output code
algorithm OTPAlgorithm sha1 Hash algorithm

Both TOTP and HOTP accept raw bytes via .fromBytes(secret: Uint8List).

URI parsing #

final uri = OTPUri.parse(
  'otpauth://totp/GitHub:user@example.com?secret=JBSWY3DPEHPK3PXP&issuer=GitHub',
);
print(uri.issuer);  // 'GitHub'
print(uri.account); // 'user@example.com'

final totp = uri.toTOTP();
print(totp.now());

// Extract just the secret from any otpauth:// URI
final secret = OTPUri.extractSecret('otpauth://totp/...?secret=ABC123');

Security notes #

  • Validation window. TOTP.verify accepts the previous, current and next time step by default (window: 1). Pass window: 0 for strict checking. HOTP.verify checks only the given counter by default; a nonzero window checks counters on both sides, so it can also accept codes for counters already used.
  • Replay protection. The package is stateless and never records used codes, and verify returns only true or false. A TOTP code stays valid for its whole window, so store the last accepted time step per user and reject any code for that step or an earlier one. For HOTP, persist the counter and move it past the accepted value after each successful check. With a window, compare against at() for each candidate step or counter to learn which one matched.
  • Secrets. Generate them with Random.secure() (at least 20 bytes for SHA-1, as RFC 4226 recommends), keep them server-side, and never log them. An otpauth:// URI embeds the secret, so treat the URI and any QR code made from it as a secret too.

RFC compliance #

Standard Title
RFC 4226 HOTP: An HMAC-Based One-Time Password Algorithm
RFC 6238 TOTP: Time-Based One-Time Password Algorithm
RFC 4648 Base16, Base32, and Base64 Data Encodings

Validated against official test vectors from RFC 4226 Appendix D and RFC 6238 Appendix B.

Requirements #

Dart SDK 3.4 or later.

Author #

Soroush Yousefpour

Buy Me A Coffee

1
likes
160
points
138
downloads

Documentation

API reference

Publisher

verified publishergabrimatic.info

Weekly Downloads

A Dart package for generating and verifying HOTP and TOTP one-time passwords using RFC 4226 and RFC 6238.

Homepage
Repository (GitHub)
View/report issues
Contributing

Topics

#otp #totp #hotp #authenticator #two-factor-auth

Funding

Consider supporting this project:

www.buymeacoffee.com

License

MIT (license)

Dependencies

crypto

More

Packages that depend on otp_auth