Mercurial > hg > Database > Alice
view 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 |
line wrap: on
line source
package alice.topology.manager; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.LinkedList; import alice.codesegment.MetaCodeSegment; import org.apache.commons.lang3.RandomStringUtils; import alice.codesegment.CodeSegment; import alice.datasegment.CommandType; import alice.datasegment.Receiver; /** * create host cookie */ public class CreateHash extends CodeSegment { // this CodeSegment change raw string to MD5 private Receiver info = ids.create(CommandType.TAKE); // order to create private Receiver info1 = ids.create(CommandType.TAKE); public CreateHash(){ info.setKey("orderHash"); info1.setKey("createdList"); } @Override public void run() { @SuppressWarnings("unchecked") LinkedList<String> createdRawList = this.info1.asClass(LinkedList.class); boolean checkNewStr = false; String raw = null; while (!checkNewStr){ raw = RandomStringUtils.randomAscii(10); // checking raw String has already created int count = 0; for (String str : createdRawList) { if (raw.equals(str)) break; count++; } if (count == createdRawList.size()) checkNewStr = true; } createdRawList.add(raw); ods.put(info1.key, createdRawList); try { // convert to MD5 String MD5 = convertMD5(raw); ods.put("MD5", MD5); } catch (NoSuchAlgorithmException e) { } new CreateHash(); } private String convertMD5(String raw) throws NoSuchAlgorithmException{ MessageDigest md = MessageDigest.getInstance("MD5"); md.update(raw.getBytes()); byte[] hash = md.digest(); StringBuilder builder = new StringBuilder(); for (int idx = 0; idx < hash.length; idx++) { if ((0xff & hash[idx]) < 0x10) { builder.append("0" + Integer.toHexString((0xff & hash[idx]))); } else { builder.append(Integer.toHexString((0xff & hash[idx]))); } } return builder.toString(); } }