mirror of
https://github.com/beemdevelopment/Aegis.git
synced 2025-04-19 21:39:18 +00:00
Added OTP.java
This class wraps the TOTP and HOTP functions in a method called generateOTP which takes an instance of KeyInfo.
This commit is contained in:
parent
d4007ab065
commit
6a6da66bbe
3 changed files with 35 additions and 12 deletions
|
@ -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) {
|
||||
|
|
25
app/src/main/java/me/impy/aegis/crypto/OTP.java
Normal file
25
app/src/main/java/me/impy/aegis/crypto/OTP.java
Normal file
|
@ -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…
Add table
Reference in a new issue