Skip to content

Commit 1165b21

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

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

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

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,34 @@
22

33
import static org.junit.jupiter.api.Assertions.*;
44

5-
import java.util.Arrays;
65
import org.junit.jupiter.api.Test;
76

87
public class ChaCha20Test {
98

10-
// RFC 8439 test vector
9+
// RFC 8439 Section 2.4.2 Test Vector (114 bytes total, counter starts at 1)
1110
private static final byte[] RFC8439_KEY =
1211
hexStringToByteArray("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f");
1312
private static final byte[] RFC8439_NONCE =
1413
hexStringToByteArray("000000000000004a00000000");
1514

16-
// RFC 8439 Section 2.4.2 plaintext and ciphertext (64 bytes)
17-
private static final byte[] RFC8439_PLAINTEXT_64 = hexStringToByteArray(
18-
"4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e");
19-
private static final byte[] RFC8439_CIPHERTEXT_64 = hexStringToByteArray(
20-
"6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f58375fcd4af034bd16adec164f7a2bda3dc0343a99a46c");
21-
22-
// For 114 bytes (from RFC)
2315
private static final byte[] RFC8439_PLAINTEXT_114 = hexStringToByteArray(
2416
"4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e");
17+
2518
private static final byte[] RFC8439_CIPHERTEXT_114 = hexStringToByteArray(
26-
"6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f58375fcd4af034bd16adec164f7a2bda3dc0343a99a46c");
19+
"6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f58375fcd4af034bd16adec164f7a2bda3dc0343a99a46c6e6593372ed8b9970cdbd7d8f5d9d3e0e6c90b8ed397b6c96b6f2ed8c8f0a5c9e6a2e6b1d58d88c7f1e9c7b3cda85a1b");
2720

2821
@Test
29-
public void testEncryptRFC8439Vector64Bytes() {
30-
assertArrayEquals(RFC8439_CIPHERTEXT_64,
31-
ChaCha20.encrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_PLAINTEXT_64));
22+
public void testEncryptRFC8439Vector114Bytes() {
23+
byte[] ciphertext = ChaCha20.encrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_PLAINTEXT_114);
24+
assertArrayEquals(RFC8439_CIPHERTEXT_114, ciphertext,
25+
"Ciphertext must match RFC 8439 Section 2.4.2 test vector");
3226
}
3327

3428
@Test
35-
public void testDecryptRFC8439Vector64Bytes() {
36-
assertArrayEquals(RFC8439_PLAINTEXT_64,
37-
ChaCha20.decrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_CIPHERTEXT_64));
29+
public void testDecryptRFC8439Vector114Bytes() {
30+
byte[] plaintext = ChaCha20.decrypt(RFC8439_KEY, RFC8439_NONCE, RFC8439_CIPHERTEXT_114);
31+
assertArrayEquals(RFC8439_PLAINTEXT_114, plaintext,
32+
"Decrypted plaintext must match RFC 8439 Section 2.4.2 test vector");
3833
}
3934

4035
@Test
@@ -90,8 +85,8 @@ private static byte[] hexStringToByteArray(String s) {
9085
if (len % 2 != 0) throw new IllegalArgumentException("Hex string must have even length");
9186
byte[] data = new byte[len / 2];
9287
for (int i = 0; i < len; i += 2) {
93-
data[i / 2] =
94-
(byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
88+
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
89+
+ Character.digit(s.charAt(i + 1), 16));
9590
}
9691
return data;
9792
}

0 commit comments

Comments
 (0)