Skip to content

Commit 0d48971

Browse files
committed
Fixes HEX conversion for secret.
* Corrects the handling of uppercase letters by adding the necessary offset. * Handles lowercase letters as well * Adds tests for both cases.
1 parent e1edf2e commit 0d48971

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

src/codeu/chat/common/Secret.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ public static byte[] parse(String stringSecret) {
3030
final int offset = stringSecret.length() % 2;
3131

3232
for (int i = 0; i < stringSecret.length(); i++) {
33-
expanded[offset + i] = (byte)toHex(stringSecret.charAt(i));
33+
expanded[offset + i] = (byte) toHex(stringSecret.charAt(i));
3434
}
3535

3636
final byte[] compressed = new byte[expanded.length / 2];
3737

3838
for (int i = 0; i < compressed.length; i++) {
39-
compressed[i] = (byte)((expanded[2 * i] << 4) | expanded[2 * i + 1]);
39+
compressed[i] = (byte) ((expanded[2 * i] << 4) | expanded[2 * i + 1]);
4040
}
4141

4242
return compressed;
@@ -46,7 +46,7 @@ private static final int toHex(char c) {
4646

4747
// If an invalid value was given, it will be treated as 0.
4848

49-
switch(c) {
49+
switch (c) {
5050
case '0':
5151
case '1':
5252
case '2':
@@ -59,13 +59,21 @@ private static final int toHex(char c) {
5959
case '9':
6060
return c - '0';
6161

62+
case 'a':
63+
case 'b':
64+
case 'c':
65+
case 'd':
66+
case 'e':
67+
case 'f':
68+
return c - 'a' + 10;
69+
6270
case 'A':
6371
case 'B':
6472
case 'C':
6573
case 'D':
6674
case 'E':
6775
case 'F':
68-
return c - 'A';
76+
return c - 'A' + 10;
6977

7078
default:
7179
return 0;

test/codeu/chat/common/SecretTest.java

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,54 @@
1414

1515
package codeu.chat.common;
1616

17-
import java.util.Arrays;
18-
1917
import static org.junit.Assert.*;
18+
19+
import java.util.Arrays;
2020
import org.junit.Test;
21-
import org.junit.BeforeClass;
22-
import org.junit.AfterClass;
2321

2422
public final class SecretTest {
2523

2624
@Test
2725
public void testParseEvenLength() {
2826

2927
final String input = "2345";
30-
final byte[] expected = { 0x23, 0x45 };
28+
final byte[] expected = {0x23, 0x45};
3129

3230
final byte[] actual = Secret.parse(input);
3331

3432
assertNotNull(actual);
3533
assertTrue(Arrays.equals(expected, actual));
34+
}
3635

36+
@Test
37+
public void testParseEvenLengthWithUppercaseLetters() {
38+
39+
final String input = "ABCDEF";
40+
final byte[] expected = {(byte) 0xAB, (byte) 0xCD, (byte) 0xEF};
41+
42+
final byte[] actual = Secret.parse(input);
43+
44+
assertNotNull(actual);
45+
assertTrue(Arrays.equals(expected, actual));
46+
}
47+
48+
@Test
49+
public void testParseEvenLengthWithLowercaseLetters() {
50+
51+
final String input = "abcdef";
52+
final byte[] expected = {(byte) 0xAB, (byte) 0xCD, (byte) 0xEF};
53+
54+
final byte[] actual = Secret.parse(input);
55+
56+
assertNotNull(actual);
57+
assertTrue(Arrays.equals(expected, actual));
3758
}
3859

3960
@Test
4061
public void testParseEvenLengthWithLeadingZero() {
4162

4263
final String input = "012345";
43-
final byte[] expected = { 0x01, 0x23, 0x45 };
64+
final byte[] expected = {0x01, 0x23, 0x45};
4465

4566
final byte[] actual = Secret.parse(input);
4667

@@ -52,7 +73,7 @@ public void testParseEvenLengthWithLeadingZero() {
5273
public void testParseEvenLengthWithLeadingDoubleZero() {
5374

5475
final String input = "00123456";
55-
final byte[] expected = { 0x00, 0x12, 0x34, 0x56 };
76+
final byte[] expected = {0x00, 0x12, 0x34, 0x56};
5677

5778
final byte[] actual = Secret.parse(input);
5879

@@ -64,7 +85,7 @@ public void testParseEvenLengthWithLeadingDoubleZero() {
6485
public void testParseOddLength() {
6586

6687
final String input = "12345";
67-
final byte[] expected = { 0x01, 0x23, 0x45 };
88+
final byte[] expected = {0x01, 0x23, 0x45};
6889

6990
final byte[] actual = Secret.parse(input);
7091

@@ -76,7 +97,7 @@ public void testParseOddLength() {
7697
public void testParseOddLengthWithLeadingZero() {
7798

7899
final String input = "0123456";
79-
final byte[] expected = { 0x00, 0x12, 0x34, 0x56 };
100+
final byte[] expected = {0x00, 0x12, 0x34, 0x56};
80101

81102
final byte[] actual = Secret.parse(input);
82103

@@ -88,7 +109,7 @@ public void testParseOddLengthWithLeadingZero() {
88109
public void testParseOddLengthWithLeadingDoubleZero() {
89110

90111
final String input = "001234567";
91-
final byte[] expected = { 0x00, 0x01, 0x23, 0x45, 0x67 };
112+
final byte[] expected = {0x00, 0x01, 0x23, 0x45, 0x67};
92113

93114
final byte[] actual = Secret.parse(input);
94115

0 commit comments

Comments
 (0)