changeset 430:c7c57f8d7538 dispose

use cookie MD5
author sugi
date Sun, 03 Aug 2014 22:58:55 +0900
parents 1b32ea1263f3
children 0239c1633012
files src/main/java/alice/topology/HostMessage.java src/main/java/alice/topology/manager/IncomingHosts.java
diffstat 2 files changed, 30 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/alice/topology/HostMessage.java	Tue Jul 22 18:44:18 2014 +0900
+++ b/src/main/java/alice/topology/HostMessage.java	Sun Aug 03 22:58:55 2014 +0900
@@ -12,7 +12,7 @@
     
     public String absName;
     public String remoteAbsName;
-    public String cookie;
+    public String cookie; // MD5
 
     public HostMessage() { }
     public HostMessage(String name, int port) {
--- a/src/main/java/alice/topology/manager/IncomingHosts.java	Tue Jul 22 18:44:18 2014 +0900
+++ b/src/main/java/alice/topology/manager/IncomingHosts.java	Sun Aug 03 22:58:55 2014 +0900
@@ -1,5 +1,7 @@
 package alice.topology.manager;
 
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 import java.util.HashMap;
 import java.util.LinkedList;
 
@@ -85,12 +87,21 @@
         @SuppressWarnings("unchecked")
         LinkedList<HostMessage> nodes = this.nodes.asClass(LinkedList.class);
         boolean checkNewCookie = false;
-        String str = null;
+        String hash = null;
         while (!checkNewCookie){
-            str = RandomStringUtils.randomAscii(10);
+            // create random string
+            hash = RandomStringUtils.randomAscii(10);
+            try { // convert to MD5
+                MessageDigest md = MessageDigest.getInstance("MD5");
+                md.update(hash.getBytes());
+                hash = hashByteToMD5(md.digest());
+            } catch (NoSuchAlgorithmException e) {
+            }
+            
+            // checking  MD5 has already created 
             int count = 0;
             for (HostMessage node : nodes) {
-                if (str.equals(node.cookie)) break;
+                if (hash.equals(node.cookie)) break;
                 count++;
             }
             if (count == nodes.size())
@@ -98,11 +109,23 @@
         }
         HostMessage table = new HostMessage();
         table.absName = hostName;
-        table.cookie = str;
+        table.cookie = hash;
         nodes.add(table);
         ods.put(this.nodes.key, nodes);
-        System.out.println(str);
-        return str;
+        System.out.println(hash);
+        return hash;
+    }
+    
+    private String hashByteToMD5(byte[] hash){
+        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();
     }
 
 }