Skip to content

Commit fcd6ce1

Browse files
authored
Refactor ChaCha20Test for clarity and readability
Refactored nonce and expected byte array initialization for clarity. Updated test methods to improve readability by reducing line breaks.
1 parent e0fe4e7 commit fcd6ce1

1 file changed

Lines changed: 22 additions & 50 deletions

File tree

‎src/test/java/com/thealgorithms/ciphers/ChaCha20Test.java‎

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,10 @@ void encryptingZeroesMatchesRfc8439BlockVector() {
2323
key[i] = (byte) i;
2424
}
2525

26-
byte[] nonce = {
27-
0, 0, 0, 9, 0, 0, 0, 0x4a, 0, 0, 0, 0
28-
};
29-
byte[] expected = {
30-
0x10, (byte) 0xf1, (byte) 0xe7, (byte) 0xe4, (byte) 0xd1, 0x3b, 0x59, 0x15,
31-
0x50, 0x0f, (byte) 0xdd, 0x1f, (byte) 0xa3, 0x20, 0x71, (byte) 0xc4,
32-
(byte) 0xc7, (byte) 0xd1, (byte) 0xf4, (byte) 0xc7, 0x33, (byte) 0xc0, 0x68, 0x03,
33-
0x04, 0x22, (byte) 0xaa, (byte) 0x9a, (byte) 0xc3, (byte) 0xd4, 0x6c, 0x4e,
34-
(byte) 0xd2, (byte) 0x82, 0x64, 0x46, 0x07, (byte) 0x9f, (byte) 0xaa, 0x09,
35-
0x14, (byte) 0xc2, (byte) 0xd7, 0x05, (byte) 0xd9, (byte) 0x8b, 0x02, (byte) 0xa2,
36-
(byte) 0xb5, 0x12, (byte) 0x9c, (byte) 0xd1, (byte) 0xde, 0x16, 0x4e, (byte) 0xb9,
37-
(byte) 0xcb, (byte) 0xd0, (byte) 0x83, (byte) 0xe8, (byte) 0xa2, 0x50, 0x3c, 0x4e
38-
};
26+
byte[] nonce = {0, 0, 0, 9, 0, 0, 0, 0x4a, 0, 0, 0, 0};
27+
byte[] expected = {0x10, (byte) 0xf1, (byte) 0xe7, (byte) 0xe4, (byte) 0xd1, 0x3b, 0x59, 0x15, 0x50, 0x0f, (byte) 0xdd, 0x1f, (byte) 0xa3, 0x20, 0x71, (byte) 0xc4, (byte) 0xc7, (byte) 0xd1, (byte) 0xf4, (byte) 0xc7, 0x33, (byte) 0xc0, 0x68, 0x03, 0x04, 0x22, (byte) 0xaa, (byte) 0x9a,
28+
(byte) 0xc3, (byte) 0xd4, 0x6c, 0x4e, (byte) 0xd2, (byte) 0x82, 0x64, 0x46, 0x07, (byte) 0x9f, (byte) 0xaa, 0x09, 0x14, (byte) 0xc2, (byte) 0xd7, 0x05, (byte) 0xd9, (byte) 0x8b, 0x02, (byte) 0xa2, (byte) 0xb5, 0x12, (byte) 0x9c, (byte) 0xd1, (byte) 0xde, 0x16, 0x4e, (byte) 0xb9,
29+
(byte) 0xcb, (byte) 0xd0, (byte) 0x83, (byte) 0xe8, (byte) 0xa2, 0x50, 0x3c, 0x4e};
3930

4031
byte[] actual = ChaCha20.encrypt(new byte[64], key, nonce, 1);
4132

@@ -59,20 +50,20 @@ void decryptRestoresInputAcrossMultipleBlocks() {
5950
}
6051

6152
@Test
62-
void handlesPartialBlocksAroundBlockBoundary() {
63-
for (int length : new int[] {1, 63, 65}) {
64-
byte[] input = new byte[length];
65-
for (int i = 0; i < input.length; i++) {
66-
input[i] = (byte) (i + 1);
53+
void handlesPartialBlocksAroundBlockBoundary() {
54+
for (int length : new int[] {1, 63, 65}) {
55+
byte[] input = new byte[length];
56+
for (int i = 0; i < input.length; i++) {
57+
input[i] = (byte) (i + 1);
58+
}
59+
60+
byte[] key = new byte[32];
61+
byte[] nonce = new byte[12];
62+
byte[] encrypted = ChaCha20.encrypt(input, key, nonce, 0);
63+
64+
assertArrayEquals(input, ChaCha20.decrypt(encrypted, key, nonce, 0));
6765
}
68-
69-
byte[] key = new byte[32];
70-
byte[] nonce = new byte[12];
71-
byte[] encrypted = ChaCha20.encrypt(input, key, nonce, 0);
72-
73-
assertArrayEquals(input, ChaCha20.decrypt(encrypted, key, nonce, 0));
7466
}
75-
}
7667

7768
@Test
7869
void emptyInputReturnsEmptyOutput() {
@@ -83,38 +74,19 @@ void emptyInputReturnsEmptyOutput() {
8374
void maximumCounterCanProcessOneBlock() {
8475
byte[] input = new byte[64];
8576

86-
assertArrayEquals(
87-
input,
88-
ChaCha20.decrypt(ChaCha20.encrypt(input, new byte[32], new byte[12], 0xFFFF_FFFFL),
89-
new byte[32], new byte[12], 0xFFFF_FFFFL)
90-
);
77+
assertArrayEquals(input, ChaCha20.decrypt(ChaCha20.encrypt(input, new byte[32], new byte[12], 0xFFFF_FFFFL), new byte[32], new byte[12], 0xFFFF_FFFFL));
9178
}
9279

9380
@Test
9481
void counterOverflowIsRejected() {
95-
assertThrows(
96-
IllegalArgumentException.class,
97-
() -> ChaCha20.encrypt(new byte[65], new byte[32], new byte[12], 0xFFFF_FFFFL)
98-
);
82+
assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(new byte[65], new byte[32], new byte[12], 0xFFFF_FFFFL));
9983
}
10084

10185
@Test
10286
void invalidKeyNonceAndCounterAreRejected() {
103-
assertThrows(
104-
IllegalArgumentException.class,
105-
() -> ChaCha20.encrypt(new byte[1], new byte[31], new byte[12], 0)
106-
);
107-
assertThrows(
108-
IllegalArgumentException.class,
109-
() -> ChaCha20.encrypt(new byte[1], new byte[32], new byte[11], 0)
110-
);
111-
assertThrows(
112-
IllegalArgumentException.class,
113-
() -> ChaCha20.encrypt(new byte[1], new byte[32], new byte[12], -1)
114-
);
115-
assertThrows(
116-
IllegalArgumentException.class,
117-
() -> ChaCha20.encrypt(new byte[1], new byte[32], new byte[12], 0x1_0000_0000L)
118-
);
87+
assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(new byte[1], new byte[31], new byte[12], 0));
88+
assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(new byte[1], new byte[32], new byte[11], 0));
89+
assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(new byte[1], new byte[32], new byte[12], -1));
90+
assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(new byte[1], new byte[32], new byte[12], 0x1_0000_0000L));
11991
}
12092
}

0 commit comments

Comments
 (0)