Skip to content

Commit

Permalink
Issue #20 Allow shorter or longer keys in profile options
Browse files Browse the repository at this point in the history
  • Loading branch information
kwart committed Sep 18, 2013
1 parent d0ea090 commit eaf58fe
Showing 1 changed file with 3 additions and 21 deletions.
24 changes: 3 additions & 21 deletions src/main/java/org/jboss/totp/TOTPMIDlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ public class TOTPMIDlet extends MIDlet implements CommandListener {
private static final char[] HEX_TABLE = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
'e', 'f' };

private static final int[] BASE32_LEN = { 0, 2, 4, 5, 7, 8 };

private static final String SHA1 = "SHA-1";
private static final String SHA256 = "SHA-256";
private static final String SHA512 = "SHA-512";
Expand Down Expand Up @@ -437,7 +435,7 @@ private String validateInput() {
str = "";
} else {
final StringBuffer sb = new StringBuffer();
str = str.toUpperCase().replace('0', 'O');
str = str.toUpperCase().replace('0', 'O').replace('1', 'L');
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (BASE32_CHARS.indexOf(ch) >= 0) {
Expand All @@ -447,12 +445,6 @@ private String validateInput() {
str = sb.toString();
}
tfSecret.setString(str);
final int keyLen = str.length();
int expectedBase32Len = getBase32Len(HMAC_BYTE_COUNT[algIdx]);
if (keyLen != 0 && keyLen != expectedBase32Len) {
warnings.append("Base32 encoded key for ").append(HMAC_ALGORITHMS[algIdx]).append(" must have ")
.append(expectedBase32Len).append(" characters.");
}

int step = 0;
try {
Expand All @@ -466,7 +458,7 @@ private String validateInput() {
warnings.append("\n");
warnings.append("Time step must be positive number.");
}
gauValidity.setMaxValue((keyLen > 0 && step > 1) ? step - 1 : INDEFINITE);
gauValidity.setMaxValue((str.length() > 0 && step > 1) ? step - 1 : INDEFINITE);

int digits = 0;
try {
Expand Down Expand Up @@ -660,7 +652,7 @@ private void loadSelectedProfile() {
final byte[] profileConfig = loadRecordFromStore(STORE_PROFILE_CONFIG, recordIds[profileIdx]);
ByteArrayInputStream bais = new ByteArrayInputStream(profileConfig);
DataInputStream dis = new DataInputStream(bais);
String base32EncodedSecret = null;
String base32EncodedSecret = "";
try {
tfProfile.setString(dis.readUTF());
byte[] key = new byte[dis.readByte()];
Expand Down Expand Up @@ -1038,16 +1030,6 @@ private byte[] generateNewKey() {
return result;
}

/**
* Returns lenght of Base32 encoded string for the given count of bytes.
*
* @param byteCount
* @return
*/
private static int getBase32Len(int byteCount) {
return byteCount / 5 * 8 + BASE32_LEN[byteCount % 5];
}

/**
* Zero-left-padding for integer values. If a length of given integer
* converted to string is smaller than len, then zeroes are filled on the
Expand Down

0 comments on commit eaf58fe

Please sign in to comment.