diff --git a/src/com/cringe_studios/cringe_authenticator_library/impl/HOTP.java b/src/com/cringe_studios/cringe_authenticator_library/impl/HOTP.java index 5e13bc0..56a4bcd 100644 --- a/src/com/cringe_studios/cringe_authenticator_library/impl/HOTP.java +++ b/src/com/cringe_studios/cringe_authenticator_library/impl/HOTP.java @@ -31,24 +31,36 @@ public class HOTP extends OTP { @Override public String getPin() { - // TODO Auto-generated method stub + try { + long movingFactor = this.getCounter(); + + byte[] text = new byte[Long.BYTES]; + for (int i = text.length - 1; i >= 0; i--) { + text[i] = (byte) (movingFactor & 0xff); + movingFactor >>= 8; + } + + return generateOTP(-1, text); + } catch (InvalidKeyException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } return null; } - public String generateOTP(int truncationOffset) throws NoSuchAlgorithmException, InvalidKeyException { + public String generateOTP(int truncationOffset, byte[] message) throws NoSuchAlgorithmException, InvalidKeyException { boolean addChecksum = this.hasChecksum(); - byte[] secret = this.getSecret().getBytes(StandardCharsets.US_ASCII); + byte[] secret = this.secret; int codeDigits = this.getDigits(); - long movingFactor = this.getCounter(); // put movingFactor value into text byte array - String result = null; + int digits = addChecksum ? (codeDigits + 1) : codeDigits; - byte[] text = new byte[8]; - for (int i = text.length - 1; i >= 0; i--) { - text[i] = (byte) (movingFactor & 0xff); - movingFactor >>= 8; - } + byte[] text = message; + // compute hmac hash byte[] hash = this.getAlgorithm().hash(secret, text); @@ -65,7 +77,7 @@ public class HOTP extends OTP { if (addChecksum) { otp = (otp * 10) + HOTPChecksumProvider.calcChecksum(otp, codeDigits); } - result = Integer.toString(otp); + String result = Integer.toString(otp); while (result.length() < digits) { result = "0" + result; } diff --git a/src/com/cringe_studios/cringe_authenticator_library/impl/TOTP.java b/src/com/cringe_studios/cringe_authenticator_library/impl/TOTP.java index 9233287..e71aeee 100644 --- a/src/com/cringe_studios/cringe_authenticator_library/impl/TOTP.java +++ b/src/com/cringe_studios/cringe_authenticator_library/impl/TOTP.java @@ -52,25 +52,27 @@ public class TOTP extends HOTP { } public String getPinAt(long time) throws InvalidKeyException, NoSuchAlgorithmException { - int codeDigits = this.getDigits(); + //int codeDigits = this.getDigits(); // Get the HEX in a Byte[] byte[] msg = ByteBuffer.allocate(Long.BYTES).putLong(this.getCounterAt(time)).array(); - byte[] k = this.secret; - byte[] hash = this.getAlgorithm().hash(k, msg); + //byte[] k = this.secret; + //byte[] hash = this.getAlgorithm().hash(k, msg); // put selected bytes into result int - int offset = hash[hash.length - 1] & 0xf; + //int offset = hash[hash.length - 1] & 0xf; - int binary = ((hash[offset] & 0x7f) << 24) | ((hash[offset + 1] & 0xff) << 16) - | ((hash[offset + 2] & 0xff) << 8) | (hash[offset + 3] & 0xff); + //int binary = ((hash[offset] & 0x7f) << 24) | ((hash[offset + 1] & 0xff) << 16) + // | ((hash[offset + 2] & 0xff) << 8) | (hash[offset + 3] & 0xff); - int otp = binary % DIGITS_POWER[codeDigits]; + //int otp = binary % DIGITS_POWER[codeDigits]; - String result = Integer.toString(otp); - while (result.length() < codeDigits) { - result = "0" + result; - } - return result; + //String result = Integer.toString(otp); + //while (result.length() < codeDigits) { + // result = "0" + result; + //} + //return result; + + return this.generateOTP(-1, msg); } } \ No newline at end of file