TOTP constructor

TOTP(
  1. List<int> secret, {
  2. int digits = 6,
  3. String? label,
  4. String? issuer,
  5. Duration period = const Duration(seconds: 30),
  6. BlockHashBase<BlockHashSink> algo = sha1,
})

Creates an instance of the TOTP class with the specified parameters.

Parameters:

  • secret is the shared secret as a list of bytes for generating the OTP.
  • digits is the number of digits in the generated OTP (default is 6).
  • period is duration in seconds an OTP is valid for (default is 30).
  • algo is the block hash algorithm to use (default is sha1).
  • label is an optional string to identify the account or service the OTP is associated with.
  • issuer is an optional string to specify the entity issuing the OTP.

Implementation

TOTP(
  List<int> secret, {
  int digits = 6,
  String? label,
  String? issuer,
  this.period = const Duration(seconds: 30),
  BlockHashBase algo = sha1,
})  : _periodMS = period.inMilliseconds,
      super(
        secret,
        algo: algo,
        digits: digits,
        label: label,
        issuer: issuer,
        counter: Uint8List(8),
      ) {
  // setup stream controller
  Timer? timer;
  _controller.onCancel = () {
    timer?.cancel();
  };
  _controller.onListen = () {
    int d = _periodMS - (currentTime % _periodMS);
    timer = Timer(Duration(milliseconds: d), () {
      timer = Timer.periodic(period, (_) {
        _controller.sink.add(value());
      });
      _controller.sink.add(value());
    });
    _controller.sink.add(value());
  };
}