-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSRP_Utility.java
More file actions
67 lines (59 loc) · 2.38 KB
/
SRP_Utility.java
File metadata and controls
67 lines (59 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class SRP_Utility {
public static final BigInteger N = new BigInteger(
"E9E3C8013CF43E080378D682F58D784EB6A9D7A0A2E9A17D8F0A1C9B8BB9B149", 16); // N a large prime (N = 2q + 1
// where q: prime)
public static final BigInteger g = BigInteger.valueOf(2); // g: generator
/**
* Computes multiplier paramter: k
*
* @return BigInteger k
* @throws NoSuchAlgorithmException
*/
public static BigInteger computeK() throws NoSuchAlgorithmException {
byte[] n_bytes = N.toByteArray();
byte[] g_bytes = g.toByteArray();
byte[] temp = new byte[n_bytes.length + g_bytes.length];
System.arraycopy(n_bytes, 0, temp, 0, n_bytes.length);
System.arraycopy(g_bytes, 0, temp, n_bytes.length, g_bytes.length);
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(temp);
return new BigInteger(1, hash);
}
/**
* Computes SHA-256 Hash for given string input
*
* @param input String random string
* @return BigInteger hash of string input
* @throws NoSuchAlgorithmException
*/
public static BigInteger hash(String input) throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] bytes = digest.digest(input.getBytes());
return new BigInteger(1, bytes);
}
/**
* Computes Exponent and Mods it with given modulus value
*
* @param base BigInteger used as base
* @param exponent BigInteger used as exponent
* @param modulus BigInteger used as modulus
* @return
*/
public static BigInteger modularExponent(BigInteger base, BigInteger exponent, BigInteger modulus) {
return base.modPow(exponent, modulus);
}
/**
* Generates and return a random BigInteger mod N
*
* @param max BigInteger used for modulo with randomly generated number
* @return BigInteger random number
*/
public static BigInteger generateRandomBigInteger(BigInteger max) {
SecureRandom random = new SecureRandom();
return new BigInteger(max.bitLength(), random).mod(max);
}
}