Added Time correction setting to OTP.java
This commit is contained in:
parent
ffd5350cb6
commit
75680b2e93
@ -14,6 +14,7 @@ public abstract class OTP {
|
||||
protected long counter;
|
||||
protected long period;
|
||||
protected boolean checksum;
|
||||
static protected long timeOffset;
|
||||
|
||||
protected OTP(OTPType nType, String nSecret, OTPAlgorithm nAlgorithm, int nDigits, long nCounter, long nPeriodInSeconds, boolean nChecksum) {
|
||||
this.secret = Base32.decode(nSecret);
|
||||
@ -81,4 +82,14 @@ public abstract class OTP {
|
||||
public boolean hasChecksum() {
|
||||
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;
|
||||
}
|
||||
}
|
@ -37,7 +37,7 @@ public class TOTP extends HOTP {
|
||||
|
||||
@Override
|
||||
public long getCounter() {
|
||||
return getCounterAt(Instant.now().getEpochSecond());
|
||||
return getCounterAt(Instant.now().getEpochSecond() - super.getTimeCorrection());
|
||||
}
|
||||
|
||||
private long getCounterAt(long unixSecond) {
|
||||
|
Loading…
Reference in New Issue
Block a user