Site Loader

/** * Mereena Kurian *//** * Generates OTP using Hmac Alogrithm based on timestamp with 15seconds expiry */public class OTPGenerator extends BaseGenerator { private final long timeStepMillis; public static final String OTP_ALGORITHM_HMAC_SHA1 = “HmacSHA1”; public static final String OTP_ALGORITHM_HMAC_SHA256 = “HmacSHA256”; public static final String OTP_ALGORITHM_HMAC_SHA512 = “HmacSHA512”; public OTPGenerator() throws NoSuchAlgorithmException { this(15, TimeUnit.SECONDS); } public OTPGenerator(final long timeStep, final TimeUnit timeStepUnit) throws NoSuchAlgorithmException { this(timeStep, timeStepUnit, BaseGenerator.DEFAULT_PASSWORD_LENGTH); } public OTPGenerator(final long timeStep, final TimeUnit timeStepUnit, final int passwordLength) throws NoSuchAlgorithmException { this(timeStep, timeStepUnit, passwordLength, BaseGenerator.OTP_HMAC_ALGORITHM); } public OTPGenerator(final long timeStep, final TimeUnit timeStepUnit, final int passwordLength, final String algorithm) throws NoSuchAlgorithmException { super(passwordLength, algorithm); this.timeStepMillis = timeStepUnit.toMillis(timeStep); } public String generateOneTimePassword(final Key key, final Date timestamp) throws InvalidKeyException { long t = timestamp.getTime() – (timestamp.getTime() % 15000); //invoking OTP generation logic which is timebased int otp = super.generateOneTimePassword(key, t / this.timeStepMillis); //converting to 4digit pin if (otp < 9) { return "000" + String.valueOf(otp); } else if (otp < 99) { return "00" + String.valueOf(otp); } else if (otp < 999) { return "0" + String.valueOf(otp); } else { return String.valueOf(otp); } } //generates previousOTP as previous pin should be valid for 2-3seconds public String getPreviousOTP(final Key key, final long timestamp) throws InvalidKeyException { int otp = super.generateOneTimePassword(key, timestamp / this.timeStepMillis); //converting to 4digit pin if (otp < 9) { return "000" + String.valueOf(otp); } else if (otp < 99) { return "00" + String.valueOf(otp); } else if (otp < 999) { return "0" + String.valueOf(otp); } else { return String.valueOf(otp); } } public long getTimeStep(final TimeUnit timeUnit) { return timeUnit.convert(this.timeStepMillis, TimeUnit.MILLISECONDS); }}

Post Author: admin

x

Hi!
I'm Sonya!

Would you like to get a custom essay? How about receiving a customized one?

Check it out