431
|
1 package alice.topology.manager;
|
|
2
|
|
3 import java.security.MessageDigest;
|
|
4 import java.security.NoSuchAlgorithmException;
|
|
5 import java.util.LinkedList;
|
|
6
|
|
7 import org.apache.commons.lang3.RandomStringUtils;
|
|
8
|
|
9 import alice.codesegment.CodeSegment;
|
|
10 import alice.datasegment.CommandType;
|
|
11 import alice.datasegment.Receiver;
|
|
12
|
|
13 public class CreateHash extends CodeSegment{
|
|
14 // this CodeSegment change raw string to MD5
|
|
15
|
|
16 private Receiver info = ids.create(CommandType.TAKE); // order to create
|
|
17 private Receiver info1 = ids.create(CommandType.TAKE);
|
|
18
|
|
19 public CreateHash(){
|
|
20 info.setKey("orderHash");
|
|
21 info1.setKey("createdList");
|
|
22 }
|
|
23
|
|
24 @Override
|
|
25 public void run() {
|
435
|
26 System.out.println("created");
|
431
|
27 @SuppressWarnings("unchecked")
|
|
28 LinkedList<String> createdRawList = this.info1.asClass(LinkedList.class);
|
|
29 boolean checkNewStr = false;
|
|
30 String raw = null;
|
|
31
|
|
32 while (!checkNewStr){
|
|
33 raw = RandomStringUtils.randomAscii(10);
|
|
34 // checking raw String has already created
|
|
35 int count = 0;
|
|
36 for (String str : createdRawList) {
|
|
37 if (raw.equals(str)) break;
|
|
38 count++;
|
|
39 }
|
|
40 if (count == createdRawList.size())
|
|
41 checkNewStr = true;
|
|
42 }
|
|
43 createdRawList.add(raw);
|
|
44 ods.put(info1.key, createdRawList);
|
|
45
|
|
46 try { // convert to MD5
|
|
47 String MD5 = convertMD5(raw);
|
|
48
|
|
49 ods.put("MD5", MD5);
|
|
50 } catch (NoSuchAlgorithmException e) {
|
|
51 }
|
|
52 System.out.println("put hash");
|
|
53 new CreateHash();
|
|
54 }
|
|
55
|
|
56 private String convertMD5(String raw) throws NoSuchAlgorithmException{
|
|
57 MessageDigest md = MessageDigest.getInstance("MD5");
|
|
58 md.update(raw.getBytes());
|
|
59 byte[] hash = md.digest();
|
|
60 StringBuilder builder = new StringBuilder();
|
|
61 for (int idx = 0; idx < hash.length; idx++) {
|
|
62 if ((0xff & hash[idx]) < 0x10) {
|
|
63 builder.append("0" + Integer.toHexString((0xff & hash[idx])));
|
|
64 } else {
|
|
65 builder.append(Integer.toHexString((0xff & hash[idx])));
|
|
66 }
|
|
67 }
|
|
68 return builder.toString();
|
|
69 }
|
|
70
|
|
71 }
|