totp_item.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import 'base32.dart' show Base32;
  2. import 'totp_algorithm.dart' show OtpHashAlgorithm, Totp;
  3. import 'otp_uri.dart' show OtpUri;
  4. /// Represents a TOTP item and associated information.
  5. ///
  6. /// Has properties of accountName, issuer, secret, digits, period and algorithm,
  7. /// as well as an id which is randomly assigned on generation.
  8. class TotpItem {
  9. TotpItem(this.secret,
  10. [this.digits = 6,
  11. this.period = 30,
  12. this.algorithm = OtpHashAlgorithm.sha1,
  13. this.issuer = "",
  14. this.accountName = ""])
  15. : assert(Base32.isBase32(secret) && secret != ''),
  16. assert(digits == 6 || digits == 8),
  17. assert(period > 0);
  18. /// Account name
  19. final String accountName;
  20. /// Issuer
  21. final String issuer;
  22. /// Secret key (in base32)
  23. final String secret;
  24. /// Number of digits (of code)
  25. final int digits;
  26. /// Time period
  27. final int period;
  28. /// Algorithm (sha1/sha256/sha512)
  29. final OtpHashAlgorithm algorithm;
  30. /// Creates a new TOTP item.
  31. static TotpItem newTotpItem(String secret,
  32. [int digits = 6,
  33. int period = 60,
  34. OtpHashAlgorithm algorithm = OtpHashAlgorithm.sha1,
  35. String issuer = "",
  36. String accountName = ""]) {
  37. return TotpItem(secret, digits, period, algorithm, issuer, accountName);
  38. }
  39. /// Parses a TOTP key URI and returns a TOTPItem.
  40. static TotpItem fromUri(String uri) {
  41. return OtpUri.fromUri(uri);
  42. }
  43. /// Generates a formatted TOTP value for the given [time].
  44. String getPrettyCode(int time) {
  45. return Totp.prettyValue(
  46. Totp.generateCode(time, secret, digits, period, algorithm));
  47. }
  48. /// Generates a TOTP value for the given [time].
  49. String getCode(int time) {
  50. return Totp.generateCode(time, secret, digits, period, algorithm);
  51. }
  52. /// Returns a placeholder representation of the generated code.
  53. String get placeholder {
  54. if (digits == 8) {
  55. return '···· ····';
  56. }
  57. return '··· ···';
  58. }
  59. @override
  60. bool operator ==(Object other) =>
  61. identical(this, other) ||
  62. other is TotpItem &&
  63. accountName == other.accountName &&
  64. issuer == other.issuer &&
  65. secret == other.secret &&
  66. digits == other.digits &&
  67. period == other.period &&
  68. algorithm == other.algorithm;
  69. @override
  70. int get hashCode =>
  71. accountName.hashCode ^
  72. issuer.hashCode ^
  73. period.hashCode ^
  74. digits.hashCode ^
  75. algorithm.hashCode ^
  76. secret.hashCode;
  77. /// Decode item from JSON.
  78. TotpItem.fromJSON(Map<String, dynamic> json)
  79. : accountName = json['accountName'],
  80. issuer = json['issuer'],
  81. period = json['period'],
  82. digits = json['digits'],
  83. algorithm = OtpHashAlgorithm.values.byName(json['algorithm']),
  84. secret = json['secret'];
  85. /// Encode item to JSON.
  86. Map<String, dynamic> toJSON() => {
  87. 'accountName': accountName,
  88. 'issuer': issuer,
  89. 'secret': secret,
  90. 'digits': digits,
  91. 'period': period,
  92. 'algorithm': algorithm.name
  93. };
  94. }