import java.nio.ByteBuffer;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
-import java.util.ArrayList;
-import java.util.List;
import java.util.Locale;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
-import android.content.Context;
-import android.content.SharedPreferences;
import android.net.Uri;
import com.google.android.apps.authenticator.Base32String;
private long counter;
private int period;
- public static List<Token> getTokens(Context ctx) {
- SharedPreferences prefs = ctx.getSharedPreferences(Token.class.getName(), Context.MODE_PRIVATE);
-
- List<Token> tokens = new ArrayList<Token>();
- for (String key : prefs.getAll().keySet()) {
- try {
- tokens.add(new Token(prefs.getString(key, null)));
- } catch (TokenUriInvalidException e) {
- e.printStackTrace();
- }
- }
-
- return tokens;
- }
-
private Token(Uri uri) throws TokenUriInvalidException {
if (!uri.getScheme().equals("otpauth"))
throw new TokenUriInvalidException();
this(Uri.parse(uri));
}
- private String getId() {
+ public String getID() {
String id;
if (issuerInt != null && !issuerInt.equals(""))
id = issuerInt + ":" + label;
return id;
}
- public void remove(Context ctx) {
- SharedPreferences prefs = ctx.getSharedPreferences(Token.class.getName(), Context.MODE_PRIVATE);
- prefs.edit().remove(getId()).apply();
+ public String getIssuer() {
+ return issuerExt != null ? issuerExt : "";
}
- public void save(Context ctx) {
- SharedPreferences prefs = ctx.getSharedPreferences(Token.class.getName(), Context.MODE_PRIVATE);
- prefs.edit().putString(getId(), toString()).apply();
+ public String getLabel() {
+ return label != null ? label : "";
}
- public String getTitle() {
- String title = "";
- if (issuerExt != null && !issuerExt.equals(""))
- title += issuerExt + ": ";
- title += label;
- return title;
+ public String getCode() {
+ switch (type) {
+ case HOTP:
+ return getHOTP(counter);
+ case TOTP:
+ return getHOTP(System.currentTimeMillis() / 1000 / period);
+ }
+
+ return null;
}
- public String getCurrentTokenValue(Context ctx, boolean increment) {
- if (type == TokenType.HOTP) {
- if (increment) {
- try {
- return getHOTP(counter++);
- } finally {
- save(ctx);
- }
- } else {
- String placeholder = "";
- for (int i = 0; i < digits; i++)
- placeholder += "-";
-
- return placeholder;
- }
- }
+ public String getPlaceholder() {
+ StringBuilder sb = new StringBuilder(digits);
+ for (int i = 0; i < digits; i++)
+ sb.append('-');
+ return sb.toString();
+ }
- return getHOTP(System.currentTimeMillis() / 1000 / period);
+ public void increment() {
+ if (type == TokenType.HOTP)
+ counter++;
}
public Uri toUri() {