use of com.google.authenticator.blackberry.Base32String.DecodingException in project google-authenticator by google.
the class AuthenticatorScreen method computePin.
/**
* Computes the one-time PIN given the secret key.
*
* @param secret
* the secret key
* @return the PIN
* @throws GeneralSecurityException
* @throws DecodingException
* If the key string is improperly encoded.
*/
public static String computePin(String secret, Long counter) {
try {
final byte[] keyBytes = Base32String.decode(secret);
Mac mac = new HMac(new SHA1Digest());
mac.init(new KeyParameter(keyBytes));
PasscodeGenerator pcg = new PasscodeGenerator(mac);
if (counter == null) {
// time-based totp
return pcg.generateTimeoutCode();
} else {
// counter-based hotp
return pcg.generateResponseCode(counter.longValue());
}
} catch (RuntimeException e) {
return "General security exception";
} catch (DecodingException e) {
return "Decoding exception";
}
}
use of com.google.authenticator.blackberry.Base32String.DecodingException in project google-authenticator by google.
the class AuthenticatorScreen method parseSecret.
/**
* Parses a secret value from a URI. The format will be:
*
* <pre>
* https://www.google.com/accounts/KeyProv?user=username#secret
* OR
* totp://username@domain#secret
* otpauth://totp/user@example.com?secret=FFF...
* otpauth://hotp/user@example.com?secret=FFF...&counter=123
* </pre>
*
* @param uri The URI containing the secret key
*/
void parseSecret(Uri uri) {
String scheme = uri.getScheme().toLowerCase();
String path = uri.getPath();
String authority = uri.getAuthority();
String user = DEFAULT_USER;
String secret;
AccountDb.OtpType type = AccountDb.OtpType.TOTP;
// only interesting for HOTP
Integer counter = new Integer(0);
if (OTP_SCHEME.equals(scheme)) {
if (authority != null && authority.equals(TOTP)) {
type = AccountDb.OtpType.TOTP;
} else if (authority != null && authority.equals(HOTP)) {
type = AccountDb.OtpType.HOTP;
String counterParameter = uri.getQueryParameter(COUNTER_PARAM);
if (counterParameter != null) {
counter = Integer.valueOf(counterParameter);
}
}
if (path != null && path.length() > 1) {
// path is "/user", so remove leading /
user = path.substring(1);
}
secret = uri.getQueryParameter(SECRET_PARAM);
// TODO: remove TOTP scheme
} else if (TOTP.equals(scheme)) {
if (authority != null) {
user = authority;
}
secret = uri.getFragment();
} else {
// https://www.google.com... URI format
String userParam = uri.getQueryParameter(USER_PARAM);
if (userParam != null) {
user = userParam;
}
secret = uri.getFragment();
}
if (secret == null) {
// Secret key not found in URI
return;
}
// TODO: April 2010 - remove version parameter handling.
String version = uri.getQueryParameter(VERSION_PARAM);
if (version == null) {
// version is null for legacy URIs
try {
secret = Base32String.encode(Base32Legacy.decode(secret));
} catch (DecodingException e) {
// Error decoding legacy key from URI
e.printStackTrace();
}
}
if (!secret.equals(getSecret(user)) || counter != AccountDb.getCounter(user) || type != AccountDb.getType(user)) {
saveSecret(user, secret, null, type);
mStatusText.setText(sResources.getString(SECRET_SAVED));
}
}
Aggregations