Skip to content

Commit a31936a

Browse files
authored
Update ChaCha20Test.java
1 parent 1165b21 commit a31936a

File tree

1 file changed

+36
-19
lines changed

1 file changed

+36
-19
lines changed

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

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,36 @@
66

77
public class ChaCha20Test {
88

9-
// RFC 8439 Section 2.4.2 Test Vector (114 bytes total, counter starts at 1)
9+
// RFC 8439 Section 2.4.2 test vector (114 bytes, counter starts at 1)
1010
private static final byte[] RFC8439_KEY =
11-
hexStringToByteArray("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f");
11+
hexStringToByteArray("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f");
1212
private static final byte[] RFC8439_NONCE =
13-
hexStringToByteArray("000000000000004a00000000");
13+
hexStringToByteArray("000000000000004a00000000");
1414

15+
// 114-byte plaintext from RFC 8439
1516
private static final byte[] RFC8439_PLAINTEXT_114 = hexStringToByteArray(
16-
"4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e");
17+
"4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a20"
18+
+ "4966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f722074686520"
19+
+ "6675747572652c2073756e73637265656e20776f756c642062652069742e");
1720

21+
// 114-byte ciphertext from RFC 8439
1822
private static final byte[] RFC8439_CIPHERTEXT_114 = hexStringToByteArray(
19-
"6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f58375fcd4af034bd16adec164f7a2bda3dc0343a99a46c6e6593372ed8b9970cdbd7d8f5d9d3e0e6c90b8ed397b6c96b6f2ed8c8f0a5c9e6a2e6b1d58d88c7f1e9c7b3cda85a1b");
23+
"6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f58"
24+
+ "375fcd4af034bd16adec164f7a2bda3dc0343a99a46c6e6593372ed8b9970cdbd7d8f5d9d3e0e6c90b8e"
25+
+ "d397b6c96b6f2ed8c8f0a5c9e6a2e6b1d58d88c7f1e9c7b3cda85a1b");
2026

2127
@Test
2228
public void testEncryptRFC8439Vector114Bytes() {
2329
byte[] ciphertext = ChaCha20.encrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_PLAINTEXT_114);
2430
assertArrayEquals(RFC8439_CIPHERTEXT_114, ciphertext,
25-
"Ciphertext must match RFC 8439 Section 2.4.2 test vector");
31+
"Ciphertext must match RFC 8439 Section 2.4.2 test vector");
2632
}
2733

2834
@Test
2935
public void testDecryptRFC8439Vector114Bytes() {
3036
byte[] plaintext = ChaCha20.decrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_CIPHERTEXT_114);
3137
assertArrayEquals(RFC8439_PLAINTEXT_114, plaintext,
32-
"Decrypted plaintext must match RFC 8439 Section 2.4.2 test vector");
38+
"Decrypted plaintext must match RFC 8439 Section 2.4.2 test vector");
3339
}
3440

3541
@Test
@@ -55,38 +61,49 @@ public void testEmptyPlaintext() {
5561
public void testInvalidKeySize() {
5662
byte[] nonce = new byte[12];
5763
byte[] data = {1, 2, 3};
58-
assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(new byte[31], nonce, data));
59-
assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(new byte[33], nonce, data));
64+
assertThrows(IllegalArgumentException.class,
65+
() -> ChaCha20.encrypt(new byte[31], nonce, data));
66+
assertThrows(IllegalArgumentException.class,
67+
() -> ChaCha20.encrypt(new byte[33], nonce, data));
6068
}
6169

6270
@Test
6371
public void testInvalidNonceSize() {
6472
byte[] key = new byte[32];
6573
byte[] data = {1, 2, 3};
66-
assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(key, new byte[11], data));
67-
assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(key, new byte[13], data));
74+
assertThrows(IllegalArgumentException.class,
75+
() -> ChaCha20.encrypt(key, new byte[11], data));
76+
assertThrows(IllegalArgumentException.class,
77+
() -> ChaCha20.encrypt(key, new byte[13], data));
6878
}
6979

7080
@Test
7181
public void testNullInputs() {
7282
byte[] key = new byte[32];
7383
byte[] nonce = new byte[12];
7484
byte[] data = {1, 2, 3};
75-
assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(null, nonce, data));
76-
assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(key, null, data));
77-
assertThrows(IllegalArgumentException.class, () -> ChaCha20.encrypt(key, nonce, null));
78-
assertThrows(IllegalArgumentException.class, () -> ChaCha20.decrypt(null, nonce, data));
79-
assertThrows(IllegalArgumentException.class, () -> ChaCha20.decrypt(key, null, data));
80-
assertThrows(IllegalArgumentException.class, () -> ChaCha20.decrypt(key, nonce, null));
85+
assertThrows(IllegalArgumentException.class,
86+
() -> ChaCha20.encrypt(null, nonce, data));
87+
assertThrows(IllegalArgumentException.class,
88+
() -> ChaCha20.encrypt(key, null, data));
89+
assertThrows(IllegalArgumentException.class,
90+
() -> ChaCha20.encrypt(key, nonce, null));
91+
assertThrows(IllegalArgumentException.class,
92+
() -> ChaCha20.decrypt(null, nonce, data));
93+
assertThrows(IllegalArgumentException.class,
94+
() -> ChaCha20.decrypt(key, null, data));
95+
assertThrows(IllegalArgumentException.class,
96+
() -> ChaCha20.decrypt(key, nonce, null));
8197
}
8298

8399
private static byte[] hexStringToByteArray(String s) {
84100
int len = s.length();
85-
if (len % 2 != 0) throw new IllegalArgumentException("Hex string must have even length");
101+
if (len % 2 != 0)
102+
throw new IllegalArgumentException("Hex string must have even length");
86103
byte[] data = new byte[len / 2];
87104
for (int i = 0; i < len; i += 2) {
88105
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
89-
+ Character.digit(s.charAt(i + 1), 16));
106+
+ Character.digit(s.charAt(i + 1), 16));
90107
}
91108
return data;
92109
}

0 commit comments

Comments
 (0)