1
1
package com .thealgorithms .ciphers ;
2
2
3
- import static org .junit .jupiter .api .Assertions .assertEquals ;
4
-
5
3
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 ;
11
4
12
5
public final class DiffieHellman {
13
-
6
+
14
7
// Private constructor to prevent instantiation of utility class
15
8
private DiffieHellman () {
16
9
throw new UnsupportedOperationException ("Utility class" );
@@ -27,38 +20,4 @@ public static BigInteger calculateSharedSecret(BigInteger otherPublicValue, BigI
27
20
// Returns b^x mod p or a^y mod p
28
21
return otherPublicValue .modPow (secret , prime );
29
22
}
30
-
31
- // *********** Unit Test Section **************
32
-
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
- }
48
-
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
- }
57
-
58
- // Test for shared secret calculation
59
- @ ParameterizedTest
60
- @ MethodSource ("provideSharedSecretData" )
61
- public void testCalculateSharedSecret (BigInteger otherPublicValue , BigInteger secret , BigInteger prime , BigInteger expected ) {
62
- assertEquals (expected , DiffieHellman .calculateSharedSecret (otherPublicValue , secret , prime ));
63
- }
64
23
}
0 commit comments