Skip to content

Commit

Permalink
Expose method to get string value to TOTP #28
Browse files Browse the repository at this point in the history
  • Loading branch information
dipu-bd committed Jul 11, 2024
1 parent 1726aea commit 07848d0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
- `shake128generator`
- `shake256generator`
- Generalize the random seed list generation to support web
- Expose methods to get padded string value from TOTP:
- `valueString`
- `streamString`

# 1.19.1

Expand Down
3 changes: 1 addition & 2 deletions example/otpauth_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,8 @@ void main(List<String> args) {
for (int i = 0; i < uris.length; ++i) {
print(uris[i]);
var totp = parse(uris[i]) as TOTP;
totp.stream.listen((e) {
totp.streamString.listen((otp) {
if (i == 0) print("\nTime: ${DateTime.now()}\n ${'-' * 40}");
var otp = e.toString().padLeft(totp.digits, '0');
var left = otp.substring(0, totp.digits ~/ 2);
var right = otp.substring(totp.digits ~/ 2);
var joined = '$left $right';
Expand Down
6 changes: 5 additions & 1 deletion lib/src/core/otpauth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ abstract class OTPAuth {
this.issuer,
}) : assert(digits >= 4 && digits <= 15);

/// Generates the OTP value
/// Generates the next OTP value
int value();

/// Returns the next OTP value as string
@pragma('vm:prefer-inline')
String valueString() => value().toString().padLeft(digits, '0');
}
7 changes: 6 additions & 1 deletion lib/src/totp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,14 @@ class TOTP extends HOTP {
};
}

/// A broadcast stream that reports new OTP on every [period] interval
/// A broadcast stream that reports new OTP value on every [period] interval
Stream<int> get stream => _controller.stream;

/// A broadcast stream that reports new OTP value as string on every [period]
/// interval
Stream<String> get streamString =>
_controller.stream.map((e) => e.toString().padLeft(digits, '0'));

/// The current time in milliseconds since EPOCH with adjusted delta shift
int get currentTime => DateTime.now().millisecondsSinceEpoch + _timeDelta;

Expand Down

0 comments on commit 07848d0

Please sign in to comment.