generateTOTP static method
Implementation
static String generateTOTP(String secret, {int timeStep = 30, int digits = 6}) {
final time = DateTime.now().millisecondsSinceEpoch ~/ 1000 ~/ timeStep;
final timeBuffer = _intToBuffer(time, 8); // Chuyển đổi thời gian thành buffer
final secretBuffer = _base32ToBuffer(secret); // Chuyển đổi Base32 secret thành buffer
// Tạo HMAC-SHA-1
final hmac = Hmac(sha1, secretBuffer); // HMAC với SHA-1
final hmacResult = hmac.convert(timeBuffer).bytes;
// Tính toán offset từ byte cuối của kết quả HMAC
final offset = hmacResult.last & 0x0F;
// Trích xuất 4 byte từ vị trí offset
final binary = ((hmacResult[offset] & 0x7F) << 24) |
((hmacResult[offset + 1] & 0xFF) << 16) |
((hmacResult[offset + 2] & 0xFF) << 8) |
(hmacResult[offset + 3] & 0xFF);
// Lấy mã OTP
final otp = (binary % pow(10, digits).toInt()).toString().padLeft(digits, '0');
return otp;
}