Skip to content

Commit c39aa6f

Browse files
committed
Provide direct access to table as an array instead of a String lookup to
avoid Java-level boundary checks.
1 parent 2e0213a commit c39aa6f

3 files changed

Lines changed: 13 additions & 8 deletions

File tree

src/main/java/org/apache/commons/codec/digest/B64.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
import java.util.concurrent.ThreadLocalRandom;
2020

2121
/**
22-
* Base64 like method to convert binary bytes into ASCII chars.
23-
*
22+
* Base64-like method to convert binary bytes into ASCII chars.
23+
* <p>
2424
* TODO: Can Base64 be reused?
25-
*
25+
* </p>
2626
* <p>
2727
* This class is immutable and thread-safe.
2828
* </p>
@@ -35,7 +35,12 @@ class B64 {
3535
/**
3636
* Table with characters for Base64 transformation.
3737
*/
38-
static final String B64T = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
38+
static final String B64T_STRING = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
39+
40+
/**
41+
* Table with characters for Base64 transformation.
42+
*/
43+
static final char[] B64T_ARRAY = B64T_STRING.toCharArray();
3944

4045
/**
4146
* Base64 like conversion of bytes to ASCII chars.
@@ -58,7 +63,7 @@ static void b64from24bit(final byte b2, final byte b1, final byte b0, final int
5863
// It's effectively a "for" loop but kept to resemble the original C code.
5964
int n = outLen;
6065
while (n-- > 0) {
61-
buffer.append(B64T.charAt(w & 0x3f));
66+
buffer.append(B64T_ARRAY[w & 0x3f]);
6267
w >>= 6;
6368
}
6469
}
@@ -76,7 +81,7 @@ static String getRandomSalt(final int num) {
7681
final StringBuilder saltString = new StringBuilder(num);
7782
final ThreadLocalRandom current = ThreadLocalRandom.current();
7883
for (int i = 1; i <= num; i++) {
79-
saltString.append(B64T.charAt(current.nextInt(B64T.length())));
84+
saltString.append(B64T_ARRAY[current.nextInt(B64T_ARRAY.length)]);
8085
}
8186
return saltString.toString();
8287
}

src/main/java/org/apache/commons/codec/digest/UnixCrypt.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public static String crypt(final byte[] original, String salt) {
207207
final int numSaltChars = SALT_CHARS.length;
208208
salt = "" + SALT_CHARS[randomGenerator.nextInt(numSaltChars)] +
209209
SALT_CHARS[randomGenerator.nextInt(numSaltChars)];
210-
} else if (!salt.matches("^[" + B64.B64T + "]{2,}$")) {
210+
} else if (!salt.matches("^[" + B64.B64T_STRING + "]{2,}$")) {
211211
throw new IllegalArgumentException("Invalid salt value: " + salt);
212212
}
213213

src/test/java/org/apache/commons/codec/digest/B64Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class B64Test {
2626
@Test
2727
public void testB64T() {
2828
assertNotNull(new B64()); // for the 100% code coverage :)
29-
assertEquals(64, B64.B64T.length());
29+
assertEquals(64, B64.B64T_ARRAY.length);
3030
}
3131

3232
@Test

0 commit comments

Comments
 (0)