|
| 1 | +package com.thealgorithms.ciphers; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
1 | 5 | import java.math.BigInteger;
|
| 6 | +import java.util.stream.Stream; |
| 7 | + |
| 8 | +import org.junit.jupiter.params.ParameterizedTest; |
| 9 | +import org.junit.jupiter.params.provider.Arguments; |
| 10 | +import org.junit.jupiter.params.provider.MethodSource; |
2 | 11 |
|
3 | 12 | public final class DiffieHellman {
|
| 13 | + |
| 14 | + // Private constructor to prevent instantiation of utility class |
4 | 15 | private DiffieHellman() {
|
5 | 16 | throw new UnsupportedOperationException("Utility class");
|
6 | 17 | }
|
7 | 18 |
|
| 19 | + // Method to calculate public value (g^x mod p) |
8 | 20 | public static BigInteger calculatePublicValue(BigInteger base, BigInteger secret, BigInteger prime) {
|
| 21 | + // Returns g^x mod p |
9 | 22 | return base.modPow(secret, prime);
|
10 | 23 | }
|
11 | 24 |
|
| 25 | + // Method to calculate the shared secret key (otherPublic^secret mod p) |
12 | 26 | public static BigInteger calculateSharedSecret(BigInteger otherPublicValue, BigInteger secret, BigInteger prime) {
|
| 27 | + // Returns b^x mod p or a^y mod p |
13 | 28 | return otherPublicValue.modPow(secret, prime);
|
14 | 29 | }
|
15 |
| -} |
16 | 30 |
|
17 |
| -// DiffieHellmanTest.java - JUnit Tests |
18 |
| -import org.junit.jupiter.api.DisplayName; |
19 |
| -import org.junit.jupiter.params.ParameterizedTest; |
20 |
| -import org.junit.jupiter.params.provider.CsvSource; |
21 |
| -import static org.junit.jupiter.api.Assertions.*; |
| 31 | + // *********** Unit Test Section ************** |
22 | 32 |
|
23 |
| -import java.math.BigInteger; |
| 33 | + // Method to provide test data for public key calculation |
| 34 | + private static Stream<Arguments> providePublicKeyData() { |
| 35 | + return Stream.of( |
| 36 | + // base, secret, prime, expected public value |
| 37 | + Arguments.of(new BigInteger("5"), new BigInteger("6"), new BigInteger("23"), new BigInteger("8")), |
| 38 | + Arguments.of(new BigInteger("2"), new BigInteger("5"), new BigInteger("13"), new BigInteger("6")) |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + // Test for public key calculation |
| 43 | + @ParameterizedTest |
| 44 | + @MethodSource("providePublicKeyData") |
| 45 | + public void testCalculatePublicValue(BigInteger base, BigInteger secret, BigInteger prime, BigInteger expected) { |
| 46 | + assertEquals(expected, DiffieHellman.calculatePublicValue(base, secret, prime)); |
| 47 | + } |
24 | 48 |
|
25 |
| -public class DiffieHellmanTest { |
| 49 | + // Method to provide test data for shared secret calculation |
| 50 | + private static Stream<Arguments> provideSharedSecretData() { |
| 51 | + return Stream.of( |
| 52 | + // otherPublic, secret, prime, expected shared secret |
| 53 | + Arguments.of(new BigInteger("8"), new BigInteger("6"), new BigInteger("23"), new BigInteger("2")), |
| 54 | + Arguments.of(new BigInteger("6"), new BigInteger("5"), new BigInteger("13"), new BigInteger("12")) |
| 55 | + ); |
| 56 | + } |
26 | 57 |
|
| 58 | + // Test for shared secret calculation |
27 | 59 | @ParameterizedTest
|
28 |
| - @DisplayName("Diffie-Hellman Key Exchange Test") |
29 |
| - @CsvSource({ |
30 |
| - "23, 5, 6, 15", |
31 |
| - "97, 7, 12, 23", |
32 |
| - "61, 2, 9, 19" |
33 |
| - }) |
34 |
| - void testDiffieHellman(String nStr, String gStr, String xStr, String yStr) { |
35 |
| - BigInteger n = new BigInteger(nStr); |
36 |
| - BigInteger g = new BigInteger(gStr); |
37 |
| - BigInteger x = new BigInteger(xStr); |
38 |
| - BigInteger y = new BigInteger(yStr); |
39 |
| - |
40 |
| - BigInteger a = DiffieHellman.calculatePublicValue(g, x, n); |
41 |
| - BigInteger b = DiffieHellman.calculatePublicValue(g, y, n); |
42 |
| - |
43 |
| - BigInteger k1 = DiffieHellman.calculateSharedSecret(b, x, n); |
44 |
| - BigInteger k2 = DiffieHellman.calculateSharedSecret(a, y, n); |
45 |
| - |
46 |
| - assertEquals(k1, k2, "Shared secret keys do not match for inputs n=" + nStr + ", g=" + gStr); |
| 60 | + @MethodSource("provideSharedSecretData") |
| 61 | + public void testCalculateSharedSecret(BigInteger otherPublicValue, BigInteger secret, BigInteger prime, BigInteger expected) { |
| 62 | + assertEquals(expected, DiffieHellman.calculateSharedSecret(otherPublicValue, secret, prime)); |
47 | 63 | }
|
48 | 64 | }
|
0 commit comments