1 package spok;
2
3 import java.security.MessageDigest;
4 import java.security.NoSuchAlgorithmException;
5 import java.security.SecureRandom;
6
7
8
9
10
11
12
13 public class GenerateId {
14 private static String spectrumID;
15
16
17
18
19
20
21
22 public static String GenerateId() {
23 try {
24
25
26
27 SecureRandom prng = SecureRandom.getInstance("SHA1PRNG");
28
29
30 String randomNum = new Integer(prng.nextInt()).toString();
31
32
33 MessageDigest sha = MessageDigest.getInstance("SHA-1");
34 byte[] result = sha.digest(randomNum.getBytes());
35
36 spectrumID = hexEncode(result);
37 spectrumID = "sid_" + spectrumID;
38 } catch (NoSuchAlgorithmException ex) {
39 System.err.println(ex);
40 }
41 return spectrumID;
42 }
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 private static String hexEncode(byte[] aInput) {
58 StringBuffer result = new StringBuffer();
59 char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
60 'a', 'b', 'c', 'd', 'e', 'f' };
61 for (int idx = 0; idx < aInput.length; ++idx) {
62 byte b = aInput[idx];
63 result.append(digits[(b & 0xf0) >> 4]);
64 result.append(digits[b & 0x0f]);
65 }
66 return result.toString();
67 }
68 }