Added Time correction setting to OTP.java

This commit is contained in:
TheArrayser 2023-06-25 22:14:12 +02:00
parent ffd5350cb6
commit 75680b2e93
2 changed files with 12 additions and 1 deletions

View File

@ -14,6 +14,7 @@ public abstract class OTP {
protected long counter; protected long counter;
protected long period; protected long period;
protected boolean checksum; protected boolean checksum;
static protected long timeOffset;
protected OTP(OTPType nType, String nSecret, OTPAlgorithm nAlgorithm, int nDigits, long nCounter, long nPeriodInSeconds, boolean nChecksum) { protected OTP(OTPType nType, String nSecret, OTPAlgorithm nAlgorithm, int nDigits, long nCounter, long nPeriodInSeconds, boolean nChecksum) {
this.secret = Base32.decode(nSecret); this.secret = Base32.decode(nSecret);
@ -81,4 +82,14 @@ public abstract class OTP {
public boolean hasChecksum() { public boolean hasChecksum() {
return checksum; return checksum;
} }
// Seconds towards positive infinity means a positive correction into the future (seconds = 2 -> 5 becomes 7)
// Seconds towards negative infinity means a negative correction into the past (seconds = -2 -> 5 becomes 3)
public static void setTimeCorrection(long seconds) {
timeOffset = seconds;
}
public static long getTimeCorrection() {
return timeOffset;
}
} }

View File

@ -37,7 +37,7 @@ public class TOTP extends HOTP {
@Override @Override
public long getCounter() { public long getCounter() {
return getCounterAt(Instant.now().getEpochSecond()); return getCounterAt(Instant.now().getEpochSecond() - super.getTimeCorrection());
} }
private long getCounterAt(long unixSecond) { private long getCounterAt(long unixSecond) {