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

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

example/example.dart

import 'package:otp_auth/otp_auth.dart';

void main() {
  // Generate a TOTP code (Google Authenticator compatible)
  final totp = TOTP(secret: 'JBSWY3DPEHPK3PXP');
  final code = totp.now();
  // ignore: avoid_print
  print('TOTP: ${TOTP.format(code)}'); // e.g. "492 039"
  // ignore: avoid_print
  print('Expires in: ${totp.remaining}s');

  // Verify a code (allows 1 step of clock drift)
  // ignore: avoid_print
  print('Valid: ${totp.verify(code)}');

  // Parse an otpauth:// URI
  final uri = OTPUri.parse(
    'otpauth://totp/GitHub:user@example.com?secret=JBSWY3DPEHPK3PXP&issuer=GitHub',
  );
  // ignore: avoid_print
  print('Issuer: ${uri.issuer}');
  // ignore: avoid_print
  print('Account: ${uri.account}');
  final totpFromUri = uri.toTOTP();
  // ignore: avoid_print
  print('Code: ${totpFromUri.now()}');

  // HOTP
  final hotp = HOTP(secret: 'JBSWY3DPEHPK3PXP');
  // ignore: avoid_print
  print('HOTP(0): ${hotp.at(0)}');
  // ignore: avoid_print
  print('HOTP(1): ${hotp.at(1)}');
}
0
likes
160
points
186
downloads

Documentation

API reference

Publisher

unverified uploader

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