legacy_authenticator_item.dart 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import 'package:pb_authenticator_totp/totp.dart';
  2. import 'package:uuid/uuid.dart';
  3. class LegacyAuthenticatorItem {
  4. /// Legacy GUID id
  5. final String id;
  6. final TotpItem totp;
  7. LegacyAuthenticatorItem(this.id, this.totp);
  8. static LegacyAuthenticatorItem newAuthenticatorItemFromUri(String uri) {
  9. var id = Uuid().v4();
  10. return LegacyAuthenticatorItem(id, TotpItem.fromUri(uri));
  11. }
  12. static LegacyAuthenticatorItem newAuthenticatorItem(TotpItem item) {
  13. var id = Uuid().v4();
  14. return LegacyAuthenticatorItem(id, item);
  15. }
  16. // static LegacyAuthenticatorItem newAuthenticatorItem(String secret,
  17. // [int digits = 6,
  18. // int period = 60,
  19. // OtpHashAlgorithm algorithm = OtpHashAlgorithm.sha1,
  20. // String issuer = "",
  21. // String accountName = ""]) {
  22. // var item = TotpItem(secret, digits, period, algorithm, issuer, accountName);
  23. // var id = Uuid().v4();
  24. // return LegacyAuthenticatorItem(id, item);
  25. // }
  26. /// Legacy decode.
  27. LegacyAuthenticatorItem.fromMap(Map<String, dynamic> json)
  28. : id = json['id'],
  29. totp = TotpItem.fromJSON(json);
  30. /// Legacy encode.
  31. Map<String, dynamic> toMap() => {
  32. 'id': id,
  33. 'accountName': totp.accountName,
  34. 'issuer': totp.issuer,
  35. 'secret': totp.secret,
  36. 'digits': totp.digits,
  37. 'period': totp.period,
  38. 'algorithm': totp.algorithm.name
  39. };
  40. @override
  41. bool operator ==(Object other) =>
  42. identical(this, other) ||
  43. other is LegacyAuthenticatorItem && id == other.id && totp == other.totp;
  44. @override
  45. int get hashCode => id.hashCode ^ totp.hashCode;
  46. }