Added OTP.java

This class wraps the TOTP and HOTP functions in a method
called generateOTP which takes an instance of KeyInfo.
pull/41/head
Impyy 9 years ago
parent d4007ab065
commit 6a6da66bbe

@ -6,12 +6,9 @@ import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.Serializable;
import me.impy.aegis.crypto.KeyInfo;
import me.impy.aegis.crypto.TOTP;
import me.impy.aegis.crypto.OTP;
public class MainActivity extends AppCompatActivity {
@ -42,10 +39,14 @@ public class MainActivity extends AppCompatActivity {
if (resultCode == RESULT_OK) {
KeyInfo info = (KeyInfo)data.getSerializableExtra("Keyinfo");
String nowTimeString = (Long.toHexString(System.currentTimeMillis() / 1000 / info.getPeriod()));
String totp = TOTP.generateTOTP(info.getSecret(), nowTimeString, info.getDigits(), info.getAlgorithm());
tvTotp.setText(totp);
String otp;
try {
otp = OTP.generateOTP(info);
} catch (Exception e) {
e.printStackTrace();
return;
}
tvTotp.setText(otp);
}
}
}

@ -54,12 +54,9 @@ public class ScannerActivity extends Activity implements ZXingScannerView.Result
try {
KeyInfo info = KeyInfo.FromURL(rawResult.getText());
//String nowTimeString = (Long.toHexString(System.currentTimeMillis() / 1000 / info.getPeriod()));
//Toast.makeText(this, TOTP.generateTOTP(info.getSecret(), nowTimeString, info.getDigits(), info.getAlgo()), Toast.LENGTH_LONG).show();
Intent resultIntent = new Intent();
resultIntent.putExtra("Keyinfo", info);
setResult(Activity.RESULT_OK, resultIntent);
finish();
} catch (Exception e) {

@ -0,0 +1,25 @@
package me.impy.aegis.crypto;
public class OTP {
private OTP() {
}
public static String generateOTP(KeyInfo info) throws Exception {
String otp;
switch (info.getType()) {
case "totp":
String time = Long.toHexString(System.currentTimeMillis() / 1000 / info.getPeriod());
otp = TOTP.generateTOTP(info.getSecret(), time, info.getDigits(), info.getAlgorithm());
break;
case "hotp":
otp = HOTP.generateOTP(info.getSecret(), info.getCounter(), info.getDigits(), false, -1);
break;
default:
// this should never happen
throw new Exception("unsupported type");
}
return otp;
}
}
Loading…
Cancel
Save