updates for 0.9.4
[debian/openrocket] / src / net / sf / openrocket / util / UniqueID.java
index 1cc082e816bf4ba1aaca592afe6703e76255856c..e7b56d4ac7dbef057d29ccfaa7b59c6a69c369ce 100644 (file)
@@ -1,8 +1,12 @@
 package net.sf.openrocket.util;
 
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 import java.util.UUID;
 import java.util.concurrent.atomic.AtomicInteger;
 
+import net.sf.openrocket.gui.main.ExceptionHandler;
+
 public class UniqueID {
        
        private static AtomicInteger nextId = new AtomicInteger(1);
@@ -30,4 +34,34 @@ public class UniqueID {
                return UUID.randomUUID().toString();
        }
        
+       
+       /**
+        * Return a hashed unique ID that contains no information whatsoever of the
+        * originating computer.
+        * 
+        * @return      a unique identifier string that contains no information about the computer.
+        */
+       public static String generateHashedID() {
+               String id = UUID.randomUUID().toString();
+               
+               try {
+                       MessageDigest algorithm = MessageDigest.getInstance("MD5");
+                       algorithm.reset();
+                       algorithm.update(id.getBytes());
+                       byte[] digest = algorithm.digest();
+                       
+                       StringBuilder sb = new StringBuilder();
+                       for (byte b: digest) {
+                               sb.append(String.format("%02X", 0xFF & b));
+                       }
+                       id = sb.toString();
+                       
+               } catch (NoSuchAlgorithmException e) {
+                       ExceptionHandler.handleErrorCondition(e);
+                       id = "" + id.hashCode();
+               }
+               
+               return id;
+       }
+       
 }