-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.java
More file actions
48 lines (42 loc) · 1.23 KB
/
Util.java
File metadata and controls
48 lines (42 loc) · 1.23 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
package heaven.nchat.server;
import java.io.*;
import java.math.BigInteger;
import java.security.*;
import javax.crypto.SecretKey;
public class Util {
public static String getServerIdHash(String serverId, PublicKey publicKey, SecretKey secretKey)
{
try
{
return (new BigInteger(digestOperation("SHA-1", new byte[][] {serverId.getBytes("ISO_8859_1"), secretKey.getEncoded(), publicKey.getEncoded()}))).toString(16);
}
catch (UnsupportedEncodingException ex)
{
ex.printStackTrace();
return null;
}
}
/**
* Compute a message digest on arbitrary byte[] data
*/
private static byte[] digestOperation(String algorithm, byte[] ... arguments)
{
try
{
MessageDigest digest = MessageDigest.getInstance(algorithm);
byte[][] args = arguments;
int count = arguments.length;
for (int i = 0; i < count; ++i)
{
byte[] pArgs = args[i];
digest.update(pArgs);
}
return digest.digest();
}
catch (NoSuchAlgorithmException ex)
{
ex.printStackTrace();
return null;
}
}
}