make method

String make({
  1. int counter = 0,
  2. int digits = 6,
})

Make a OTP code.

@param counter The counter value. @param digits The OTP code length.

Implementation

String make({
  int counter = 0,
  int digits = 6,
}) {
  /// Get the counter bytes hmac sha1 bytes;
  final bytes = hmac.convert(_int_to_8_bytes(counter)).bytes;

  /// Get the offset of the last byte;
  final offset = bytes.elementAt(bytes.length - 1) & 0xf;
  final value = (bytes.elementAt(offset) & 0x7f) << 24 |
      (bytes.elementAt(offset + 1) & 0xff) << 16 |
      (bytes.elementAt(offset + 2) & 0xff) << 8 |
      (bytes.elementAt(offset + 3) & 0xff);

  /// get the value string
  final str = (value % pow(10, digits)).toString();

  return str.padLeft(digits, '0');
}