Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[auth] Add method to generate future codes

Neeraj Guptac47fcba5cc9d
diff --git a/auth/lib/utils/totp_util.dart b/auth/lib/utils/totp_util.dart
index f886215081..669f986cb5 100644
--- a/auth/lib/utils/totp_util.dart
+++ b/auth/lib/utils/totp_util.dart
@@ -52,6 +52,38 @@ String getNextTotp(Code code) {
);
}
+// generateFutureTotpCodes generates future TOTP codes based on the current time.
+// It returns the start time and a list of future codes.
+(int, List<String>) generateFutureTotpCodes(Code code, int count) {
+ final int startTime =
+ ((DateTime.now().millisecondsSinceEpoch ~/ 1000) ~/ code.period) *
+ code.period *
+ 1000;
+ final String secret = getSanitizedSecret(code.secret);
+ final List<String> codes = [];
+ if (code.type == Type.steam || code.issuer.toLowerCase() == 'steam') {
+ final SteamTOTP steamTotp = SteamTOTP(secret: code.secret);
+ for (int i = 1; i <= count; i++) {
+ int generatedTime = startTime + code.period * 1000 * i;
+ codes.add(steamTotp.generate(generatedTime ~/ 1000));
+ }
+ } else {
+ for (int i = 1; i <= count; i++) {
+ int generatedTime = startTime + code.period * 1000 * i;
+ final genCode = otp.OTP.generateTOTPCodeString(
+ secret,
+ generatedTime,
+ length: code.digits,
+ interval: code.period,
+ algorithm: _getAlgorithm(code),
+ isGoogle: true,
+ );
+ codes.add(genCode);
+ }
+ }
+ return (startTime, codes);
+}
+
otp.Algorithm _getAlgorithm(Code code) {
switch (code.algorithm) {
case Algorithm.sha256: