Mercurial > hg > Database > Alice
annotate src/main/java/alice/topology/manager/CreateHash.java @ 602:8a9fd716c335 dispose
change topology class extends CodeSegment from MetaCodeSegment, Chat minor fix
author | Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 03 May 2016 20:09:45 +0900 |
parents | 3284428f525e |
children |
rev | line source |
---|---|
431 | 1 package alice.topology.manager; |
2 | |
3 import java.security.MessageDigest; | |
4 import java.security.NoSuchAlgorithmException; | |
5 import java.util.LinkedList; | |
6 | |
599
3284428f525e
add MetaCodeSegment & MetaDSM
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents:
584
diff
changeset
|
7 import alice.codesegment.MetaCodeSegment; |
431 | 8 import org.apache.commons.lang3.RandomStringUtils; |
9 | |
10 import alice.codesegment.CodeSegment; | |
11 import alice.datasegment.CommandType; | |
12 import alice.datasegment.Receiver; | |
13 | |
584
8f765e3eed24
add Topology Manager Number to Topology Node Key
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents:
466
diff
changeset
|
14 /** |
8f765e3eed24
add Topology Manager Number to Topology Node Key
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents:
466
diff
changeset
|
15 * create host cookie |
8f765e3eed24
add Topology Manager Number to Topology Node Key
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents:
466
diff
changeset
|
16 */ |
602
8a9fd716c335
change topology class extends CodeSegment from MetaCodeSegment, Chat minor fix
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents:
599
diff
changeset
|
17 public class CreateHash extends CodeSegment { |
431 | 18 // this CodeSegment change raw string to MD5 |
466 | 19 |
431 | 20 private Receiver info = ids.create(CommandType.TAKE); // order to create |
466 | 21 private Receiver info1 = ids.create(CommandType.TAKE); |
22 | |
431 | 23 public CreateHash(){ |
24 info.setKey("orderHash"); | |
25 info1.setKey("createdList"); | |
26 } | |
466 | 27 |
431 | 28 @Override |
29 public void run() { | |
30 @SuppressWarnings("unchecked") | |
31 LinkedList<String> createdRawList = this.info1.asClass(LinkedList.class); | |
32 boolean checkNewStr = false; | |
33 String raw = null; | |
466 | 34 |
431 | 35 while (!checkNewStr){ |
36 raw = RandomStringUtils.randomAscii(10); | |
466 | 37 // checking raw String has already created |
431 | 38 int count = 0; |
39 for (String str : createdRawList) { | |
40 if (raw.equals(str)) break; | |
41 count++; | |
42 } | |
43 if (count == createdRawList.size()) | |
44 checkNewStr = true; | |
45 } | |
46 createdRawList.add(raw); | |
47 ods.put(info1.key, createdRawList); | |
466 | 48 |
431 | 49 try { // convert to MD5 |
50 String MD5 = convertMD5(raw); | |
51 | |
52 ods.put("MD5", MD5); | |
53 } catch (NoSuchAlgorithmException e) { | |
54 } | |
55 new CreateHash(); | |
56 } | |
466 | 57 |
431 | 58 private String convertMD5(String raw) throws NoSuchAlgorithmException{ |
59 MessageDigest md = MessageDigest.getInstance("MD5"); | |
60 md.update(raw.getBytes()); | |
61 byte[] hash = md.digest(); | |
62 StringBuilder builder = new StringBuilder(); | |
63 for (int idx = 0; idx < hash.length; idx++) { | |
64 if ((0xff & hash[idx]) < 0x10) { | |
65 builder.append("0" + Integer.toHexString((0xff & hash[idx]))); | |
66 } else { | |
67 builder.append(Integer.toHexString((0xff & hash[idx]))); | |
68 } | |
69 } | |
70 return builder.toString(); | |
71 } | |
466 | 72 |
431 | 73 } |