1 package net.sf.openrocket.util;
3 import java.security.MessageDigest;
4 import java.security.NoSuchAlgorithmException;
6 import java.util.concurrent.atomic.AtomicInteger;
8 import net.sf.openrocket.gui.main.ExceptionHandler;
10 public class UniqueID {
12 private static AtomicInteger nextId = new AtomicInteger(1);
15 * Return a positive integer ID unique during this program execution.
16 * The values are taken as sequential numbers, and will re-occur in
17 * later executions of the program.
19 * This method is thread-safe and fast.
21 * @return a positive integer ID unique in this program execution.
23 public static int next() {
24 return nextId.getAndIncrement();
29 * Return a new universally unique ID string.
31 * @return a unique identifier string.
33 public static String uuid() {
34 return UUID.randomUUID().toString();
39 * Return a hashed unique ID that contains no information whatsoever of the
40 * originating computer.
42 * @return a unique identifier string that contains no information about the computer.
44 public static String generateHashedID() {
45 String id = UUID.randomUUID().toString();
48 MessageDigest algorithm = MessageDigest.getInstance("MD5");
50 algorithm.update(id.getBytes());
51 byte[] digest = algorithm.digest();
53 StringBuilder sb = new StringBuilder();
54 for (byte b: digest) {
55 sb.append(String.format("%02X", 0xFF & b));
59 } catch (NoSuchAlgorithmException e) {
60 ExceptionHandler.handleErrorCondition(e);
61 id = "" + id.hashCode();