Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switching to Base64.NO_WRAP for Bas64.encodeToString #1321

Merged
merged 2 commits into from
Dec 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,35 +206,13 @@ public static String hash(String data, String key) {
byte [] sig = sha.doFinal(dataBytes);

// Encodes with Base64.
String hash = Base64.encodeToString(sig, Base64.DEFAULT);

/*
* Android 4.3 has a bug where a newline character is appended
* at the end of the base64 encoded string. We remove this newline
* character to prevent a mismatch between the stored hash
* and computed hash.
*/
hash = removeNewLine(hash);
return hash;
return Base64.encodeToString(sig, Base64.NO_WRAP);
} catch (Exception ex) {
Log.w(TAG, "Error during hashing", ex);
return null;
}
}

/**
* Removes a trailing newline character from the hash.
*
* @param hash Hash.
* @return Hash with trailing newline character removed.
*/
public static String removeNewLine(String hash) {
if (hash != null && hash.endsWith("\n")) {
return hash.substring(0, hash.lastIndexOf("\n"));
}
return hash;
}

private static byte[] generateInitVector() throws NoSuchAlgorithmException, NoSuchProviderException {
final SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
byte[] iv = new byte[16];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public int addFailedPasscodeAttempt() {
public boolean check(Context ctx, String passcode) {
SharedPreferences sp = ctx.getSharedPreferences(PASSCODE_PREF_NAME, Context.MODE_PRIVATE);
String hashedPasscode = sp.getString(KEY_PASSCODE, null);
hashedPasscode = Encryptor.removeNewLine(hashedPasscode);
hashedPasscode = removeNewLine(hashedPasscode);
if (hashedPasscode != null) {
return hashedPasscode.equals(hashForVerification(passcode));
}
Expand All @@ -318,6 +318,20 @@ public boolean check(Context ctx, String passcode) {
return true;
}

/**
* Removes a trailing newline character from the hash.
*
* @param hash Hash.
* @return Hash with trailing newline character removed.
*/
private String removeNewLine(String hash) {
int length = hash == null ? 0 : hash.length();
if (length > 0 && hash.endsWith("\n")) {
return hash.substring(0, length - 1);
}
return hash;
}

/**
* Store the given passcode (hashed) in private preference
* @param ctx
Expand Down