HTOP works now (I think)

This commit is contained in:
TheArrayser 2023-06-18 21:35:01 +02:00
parent 451b46e087
commit bb7c152cb9
2 changed files with 37 additions and 23 deletions

View File

@ -31,24 +31,36 @@ public class HOTP extends OTP {
@Override @Override
public String getPin() { 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; return null;
} }
public String generateOTP(int truncationOffset) throws NoSuchAlgorithmException, InvalidKeyException { public String generateOTP(int truncationOffset, byte[] message) throws NoSuchAlgorithmException, InvalidKeyException {
boolean addChecksum = this.hasChecksum(); boolean addChecksum = this.hasChecksum();
byte[] secret = this.getSecret().getBytes(StandardCharsets.US_ASCII); byte[] secret = this.secret;
int codeDigits = this.getDigits(); int codeDigits = this.getDigits();
long movingFactor = this.getCounter();
// put movingFactor value into text byte array // put movingFactor value into text byte array
String result = null;
int digits = addChecksum ? (codeDigits + 1) : codeDigits; int digits = addChecksum ? (codeDigits + 1) : codeDigits;
byte[] text = new byte[8]; byte[] text = message;
for (int i = text.length - 1; i >= 0; i--) {
text[i] = (byte) (movingFactor & 0xff);
movingFactor >>= 8;
}
// compute hmac hash // compute hmac hash
byte[] hash = this.getAlgorithm().hash(secret, text); byte[] hash = this.getAlgorithm().hash(secret, text);
@ -65,7 +77,7 @@ public class HOTP extends OTP {
if (addChecksum) { if (addChecksum) {
otp = (otp * 10) + HOTPChecksumProvider.calcChecksum(otp, codeDigits); otp = (otp * 10) + HOTPChecksumProvider.calcChecksum(otp, codeDigits);
} }
result = Integer.toString(otp); String result = Integer.toString(otp);
while (result.length() < digits) { while (result.length() < digits) {
result = "0" + result; result = "0" + result;
} }

View File

@ -52,25 +52,27 @@ public class TOTP extends HOTP {
} }
public String getPinAt(long time) throws InvalidKeyException, NoSuchAlgorithmException { public String getPinAt(long time) throws InvalidKeyException, NoSuchAlgorithmException {
int codeDigits = this.getDigits(); //int codeDigits = this.getDigits();
// Get the HEX in a Byte[] // Get the HEX in a Byte[]
byte[] msg = ByteBuffer.allocate(Long.BYTES).putLong(this.getCounterAt(time)).array(); byte[] msg = ByteBuffer.allocate(Long.BYTES).putLong(this.getCounterAt(time)).array();
byte[] k = this.secret; //byte[] k = this.secret;
byte[] hash = this.getAlgorithm().hash(k, msg); //byte[] hash = this.getAlgorithm().hash(k, msg);
// put selected bytes into result int // 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) //int binary = ((hash[offset] & 0x7f) << 24) | ((hash[offset + 1] & 0xff) << 16)
| ((hash[offset + 2] & 0xff) << 8) | (hash[offset + 3] & 0xff); // | ((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); //String result = Integer.toString(otp);
while (result.length() < codeDigits) { //while (result.length() < codeDigits) {
result = "0" + result; // result = "0" + result;
} //}
return result; //return result;
return this.generateOTP(-1, msg);
} }
} }