Rename TOTPHelper to TokenCalculator

This commit is contained in:
Jakob Nixdorf 2017-07-25 16:34:21 +02:00
parent 5aa5a40b4e
commit 0457c5b276
No known key found for this signature in database
GPG key ID: BE99BF86574A7DBC
4 changed files with 23 additions and 29 deletions

View file

@ -50,7 +50,7 @@ import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import static org.shadowice.flocke.andotp.TOTPHelper.TOTP_DEFAULT_PERIOD;
import static org.shadowice.flocke.andotp.TokenCalculator.TOTP_DEFAULT_PERIOD;
public class ApplicationTest extends ApplicationTestCase<Application> {
@ -60,12 +60,12 @@ public class ApplicationTest extends ApplicationTestCase<Application> {
public void testTOTPHelper(){
byte[] b = "12345678901234567890".getBytes();
assertEquals(94287082, TOTPHelper.generate(b, TOTP_DEFAULT_PERIOD, 59l, 8));
assertEquals(7081804, TOTPHelper.generate(b, TOTP_DEFAULT_PERIOD, 1111111109l, 8));
assertEquals(14050471, TOTPHelper.generate(b, TOTP_DEFAULT_PERIOD, 1111111111l, 8));
assertEquals(89005924, TOTPHelper.generate(b, TOTP_DEFAULT_PERIOD, 1234567890l, 8));
assertEquals(69279037, TOTPHelper.generate(b, TOTP_DEFAULT_PERIOD, 2000000000l, 8));
assertEquals(65353130, TOTPHelper.generate(b, TOTP_DEFAULT_PERIOD, 20000000000l, 8));
assertEquals(94287082, TokenCalculator.TOTP(b, TOTP_DEFAULT_PERIOD, 59l, 8));
assertEquals(7081804, TokenCalculator.TOTP(b, TOTP_DEFAULT_PERIOD, 1111111109l, 8));
assertEquals(14050471, TokenCalculator.TOTP(b, TOTP_DEFAULT_PERIOD, 1111111111l, 8));
assertEquals(89005924, TokenCalculator.TOTP(b, TOTP_DEFAULT_PERIOD, 1234567890l, 8));
assertEquals(69279037, TokenCalculator.TOTP(b, TOTP_DEFAULT_PERIOD, 2000000000l, 8));
assertEquals(65353130, TokenCalculator.TOTP(b, TOTP_DEFAULT_PERIOD, 20000000000l, 8));
}

View file

@ -32,8 +32,6 @@ import org.json.JSONObject;
import java.net.URL;
import java.util.Arrays;
import static org.shadowice.flocke.andotp.TOTPHelper.TOTP_DEFAULT_PERIOD;
public class Entry {
public enum OTPType { TOTP, HOTP }
@ -47,7 +45,7 @@ public class Entry {
private OTPType type = OTPType.TOTP;
private byte[] secret;
private String label;
private int period = TOTPHelper.TOTP_DEFAULT_PERIOD;
private int period = TokenCalculator.TOTP_DEFAULT_PERIOD;
private String currentOTP;
private long last_update = 0;
@ -91,7 +89,7 @@ public class Entry {
if (period != null) {
this.period = Integer.parseInt(period);
} else {
this.period = TOTP_DEFAULT_PERIOD;
this.period = TokenCalculator.TOTP_DEFAULT_PERIOD;
}
}
@ -149,24 +147,20 @@ public class Entry {
public void setPeriod(int period) { this.period = period; }
public boolean hasNonDefaultPeriod() {
return this.period != TOTP_DEFAULT_PERIOD;
return this.period != TokenCalculator.TOTP_DEFAULT_PERIOD;
}
public String getCurrentOTP() {
return currentOTP;
}
public void setCurrentOTP(String currentOTP) {
this.currentOTP = currentOTP;
}
public boolean updateOTP() {
long time = System.currentTimeMillis() / 1000;
long counter = time / this.getPeriod();
if (counter > this.last_update) {
this.setCurrentOTP(TOTPHelper.generate(this.getSecret(), this.getPeriod()));
this.last_update = counter;
if (counter > last_update) {
currentOTP = TokenCalculator.TOTP(secret, period);
last_update = counter;
return true;
} else {

View file

@ -91,7 +91,7 @@ public class MainActivity extends AppCompatActivity {
final EditText periodInput = (EditText) inputView.findViewById(R.id.manual_period);
typeInput.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, Entry.OTPType.values()));
periodInput.setText(Integer.toString(TOTPHelper.TOTP_DEFAULT_PERIOD));
periodInput.setText(Integer.toString(TokenCalculator.TOTP_DEFAULT_PERIOD));
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.dialog_title_manual_entry)
@ -106,7 +106,7 @@ public class MainActivity extends AppCompatActivity {
int period = Integer.parseInt(periodInput.getText().toString());
Entry e = new Entry(type, secret, period, label);
e.setCurrentOTP(TOTPHelper.generate(e.getSecret(), e.getPeriod()));
e.updateOTP();
adapter.addEntry(e);
adapter.saveEntries();
} else if (type == Entry.OTPType.HOTP) {
@ -216,7 +216,7 @@ public class MainActivity extends AppCompatActivity {
{
@Override
public void run() {
int progress = (int) (TOTPHelper.TOTP_DEFAULT_PERIOD - (System.currentTimeMillis() / 1000) % TOTPHelper.TOTP_DEFAULT_PERIOD) ;
int progress = (int) (TokenCalculator.TOTP_DEFAULT_PERIOD - (System.currentTimeMillis() / 1000) % TokenCalculator.TOTP_DEFAULT_PERIOD) ;
progressBar.setProgress(progress*100);
ObjectAnimator animation = ObjectAnimator.ofInt(progressBar, "progress", (progress-1)*100);
@ -269,7 +269,7 @@ public class MainActivity extends AppCompatActivity {
if(result.getContents() != null) {
try {
Entry e = new Entry(result.getContents());
e.setCurrentOTP(TOTPHelper.generate(e.getSecret(), e.getPeriod()));
e.updateOTP();
adapter.addEntry(e);
adapter.saveEntries();
} catch (Exception e) {

View file

@ -27,20 +27,20 @@ import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class TOTPHelper {
public class TokenCalculator {
public static final int TOTP_DEFAULT_PERIOD = 30;
public static final String SHA1 = "HmacSHA1";
public static String generate(byte[] secret) {
return String.format("%06d", generate(secret, TOTP_DEFAULT_PERIOD, System.currentTimeMillis() / 1000, 6));
public static String TOTP(byte[] secret) {
return String.format("%06d", TOTP(secret, TOTP_DEFAULT_PERIOD, System.currentTimeMillis() / 1000, 6));
}
public static String generate(byte[] secret, int period) {
return String.format("%06d", generate(secret, period, System.currentTimeMillis() / 1000, 6));
public static String TOTP(byte[] secret, int period) {
return String.format("%06d", TOTP(secret, period, System.currentTimeMillis() / 1000, 6));
}
public static int generate(byte[] key, int period, long t, int digits)
public static int TOTP(byte[] key, int period, long t, int digits)
{
int r = 0;
try {