/** * * @param token * @param key * @param counter * @return -1 if not a match. A positive number means successful validation. This positive number is also the new value of the counter */ public int validateHOTP(String token, String key, int counter) { int newCounter = counter; for (newCounter = counter; newCounter <= counter + lookAheadWindow; newCounter++) { String candidate = generateHOTP(key, newCounter); if (candidate.equals(token)) { return newCounter + 1; } } return -1; }
public String generateHOTP(String key, int counter) { String steps = Integer.toHexString(counter).toUpperCase(); // Just get a 16 digit string while (steps.length() < 16) steps = "0" + steps; return generateOTP(key, steps, numberDigits, algorithm); }
byte[] msg = hexStr2Bytes(counter); hash = hmac_sha1(crypto, k, msg);
public static boolean validOTP(RealmModel realm, String token, String secret) { OTPPolicy policy = realm.getOTPPolicy(); if (policy.getType().equals(UserCredentialModel.TOTP)) { TimeBasedOTP validator = new TimeBasedOTP(policy.getAlgorithm(), policy.getDigits(), policy.getPeriod(), policy.getLookAheadWindow()); return validator.validateTOTP(token, secret.getBytes()); } else { HmacOTP validator = new HmacOTP(policy.getDigits(), policy.getAlgorithm(), policy.getLookAheadWindow()); int c = validator.validateHOTP(token, secret, policy.getInitialCounter()); return c > -1; } }
byte[] msg = hexStr2Bytes(counter); hash = hmac_sha1(crypto, k, msg);
public static boolean validHOTP(RealmModel realm, UserModel user, String otp) { UserCredentialValueModel passwordCred = null; OTPPolicy policy = realm.getOTPPolicy(); HmacOTP validator = new HmacOTP(policy.getDigits(), policy.getAlgorithm(), policy.getLookAheadWindow()); for (UserCredentialValueModel cred : user.getCredentialsDirectly()) { if (cred.getType().equals(UserCredentialModel.HOTP)) { int counter = validator.validateHOTP(otp, cred.getValue(), cred.getCounter()); if (counter < 0) return false; cred.setCounter(counter); user.updateCredentialDirectly(cred); return true; } } return false; }
public String generateHOTP(String key, int counter) { String steps = Integer.toHexString(counter).toUpperCase(); // Just get a 16 digit string while (steps.length() < 16) steps = "0" + steps; return generateOTP(key, steps, numberDigits, algorithm); }
/** * * @param token * @param key * @param counter * @return -1 if not a match. A positive number means successful validation. This positive number is also the new value of the counter */ public int validateHOTP(String token, String key, int counter) { int newCounter = counter; for (newCounter = counter; newCounter <= counter + lookAheadWindow; newCounter++) { String candidate = generateHOTP(key, newCounter); if (candidate.equals(token)) { return newCounter + 1; } } return -1; }