Mercurial > hg > Database > Christie
diff src/main/java/christie/topology/manager/CreateHash.java @ 43:ea9657dc8311
add CreateHash SearchHostName TopologyManager
author | akahori |
---|---|
date | Thu, 02 Aug 2018 11:41:29 +0900 |
parents | |
children | cfd79a71f9cd |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/topology/manager/CreateHash.java Thu Aug 02 11:41:29 2018 +0900 @@ -0,0 +1,73 @@ +package christie.topology.manager; + + +import christie.annotation.Take; +import christie.codegear.CodeGear; + +import christie.codegear.CodeGearManager; +import org.apache.commons.lang3.RandomStringUtils; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.LinkedList; + +public class CreateHash extends CodeGear{ + // this CodeSegment change raw string to MD5 + + // Question: orderHashどこで使われているかわかんない + //@Take + //orderHash; + + @Take + LinkedList<String> createdList; + + + public CreateHash(){ } + + @Override + protected void run(CodeGearManager cgm) { + + boolean checkNewStr = false; + String raw = null; + + while (!checkNewStr){ + raw = RandomStringUtils.randomAscii(10); + // checking raw String has already created + int count = 0; + for (String str : createdList) { + if (raw.equals(str)) break; + count++; + } + + if (count == createdList.size()) checkNewStr = true; + } + createdList.add(raw); + getLocalDGM().put("createdList", createdList); + + try { // convert to MD5 + String MD5 = convertMD5(raw); + + getLocalDGM().put("MD5", MD5); + } catch (NoSuchAlgorithmException e) { + System.out.println("cannot convert MD5"); + + } + cgm.setup(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 (byte aHash : hash) { + if ((0xff & aHash) < 0x10) { + builder.append("0" + Integer.toHexString((0xff & aHash))); + } else { + builder.append(Integer.toHexString((0xff & aHash))); + } + } + return builder.toString(); + } + +}