legacy_authenticator_item.dart 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. String? iconPath;
  8. LegacyAuthenticatorItem(this.id, this.totp, {this.iconPath});
  9. static LegacyAuthenticatorItem newAuthenticatorItemFromUri(String uri, {String? iconPath}) {
  10. var id = Uuid().v4();
  11. return LegacyAuthenticatorItem(id, TotpItem.fromUri(uri), iconPath: iconPath);
  12. }
  13. static LegacyAuthenticatorItem newAuthenticatorItem(TotpItem item, {String? iconPath}) {
  14. var id = Uuid().v4();
  15. return LegacyAuthenticatorItem(id, item, iconPath: iconPath);
  16. }
  17. // static LegacyAuthenticatorItem newAuthenticatorItem(String secret,
  18. // [int digits = 6,
  19. // int period = 60,
  20. // OtpHashAlgorithm algorithm = OtpHashAlgorithm.sha1,
  21. // String issuer = "",
  22. // String accountName = ""]) {
  23. // var item = TotpItem(secret, digits, period, algorithm, issuer, accountName);
  24. // var id = Uuid().v4();
  25. // return LegacyAuthenticatorItem(id, item);
  26. // }
  27. /// Legacy decode.
  28. LegacyAuthenticatorItem.fromMap(Map<String, dynamic> json)
  29. : id = json['id'],
  30. totp = TotpItem.fromJSON(json),
  31. iconPath = json['iconPath'];
  32. /// Legacy encode.
  33. Map<String, dynamic> toMap() => {
  34. 'id': id,
  35. 'accountName': totp.accountName,
  36. 'issuer': totp.issuer,
  37. 'secret': totp.secret,
  38. 'digits': totp.digits,
  39. 'period': totp.period,
  40. 'algorithm': totp.algorithm.name,
  41. if (iconPath != null) 'iconPath': iconPath,
  42. };
  43. @override
  44. bool operator ==(Object other) =>
  45. identical(this, other) ||
  46. other is LegacyAuthenticatorItem && id == other.id && totp == other.totp && iconPath == other.iconPath;
  47. @override
  48. int get hashCode => id.hashCode ^ totp.hashCode ^ (iconPath?.hashCode ?? 0);
  49. }