|
1 |
| -import static org.junit.jupiter.api.Assertions.assertEquals; |
| 1 | +package com.thealgorithms.ciphers; |
2 | 2 |
|
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import static org.junit.jupiter.api.Assertions.*; |
3 | 5 | import java.math.BigInteger;
|
4 |
| -import java.util.stream.Stream; |
5 | 6 |
|
6 |
| -import org.junit.jupiter.params.ParameterizedTest; |
7 |
| -import org.junit.jupiter.params.provider.Arguments; |
8 |
| -import org.junit.jupiter.params.provider.MethodSource; |
| 7 | +class DiffieHellmanTest { |
9 | 8 |
|
10 |
| -public class DiffieHellmanTest { |
| 9 | + @Test |
| 10 | + void testDiffieHellmanSharedKey() { |
| 11 | + BigInteger p = new BigInteger("23"); |
| 12 | + BigInteger g = new BigInteger("5"); |
| 13 | + BigInteger a = new BigInteger("6"); // Private key for Alice |
| 14 | + BigInteger b = new BigInteger("15"); // Private key for Bob |
11 | 15 |
|
12 |
| - // Method to provide test data for public key calculation |
13 |
| - private static Stream<Arguments> providePublicKeyData() { |
14 |
| - return Stream.of( |
15 |
| - // base, secret, prime, expected public value |
16 |
| - Arguments.of(new BigInteger("5"), new BigInteger("6"), new BigInteger("23"), new BigInteger("8")), |
17 |
| - Arguments.of(new BigInteger("2"), new BigInteger("5"), new BigInteger("13"), new BigInteger("6")) |
18 |
| - ); |
19 |
| - } |
| 16 | + DiffieHellman alice = new DiffieHellman(p, g, a); |
| 17 | + DiffieHellman bob = new DiffieHellman(p, g, b); |
| 18 | + |
| 19 | + BigInteger A = alice.getPublicKey(); |
| 20 | + BigInteger B = bob.getPublicKey(); |
20 | 21 |
|
21 |
| - // Test for public key calculation |
22 |
| - @ParameterizedTest |
23 |
| - @MethodSource("providePublicKey |
| 22 | + BigInteger aliceSharedKey = alice.computeSharedKey(B); |
| 23 | + BigInteger bobSharedKey = bob.computeSharedKey(A); |
| 24 | + |
| 25 | + assertEquals(aliceSharedKey, bobSharedKey, "Shared keys do not match!"); |
| 26 | + } |
| 27 | +} |
0 commit comments