annotate src/main/java/alice/topology/manager/CreateHash.java @ 435:41bb86275910 dispose

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