X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.util%2Fsrc%2Forg%2Fargeo%2Futil%2FUuidUtils.java;h=219aef41cedfc9267ba3935ab190f80bc7a442f0;hb=212a43ed44ffc186b69e838d65a1421fd5e1f3a5;hp=88812d3826ab523a3c2e05085cd9e793ca59d63e;hpb=e5a22cdc7d0f4918f2740c626e1ab6384bd5ee44;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.util/src/org/argeo/util/UuidUtils.java b/org.argeo.util/src/org/argeo/util/UuidUtils.java index 88812d382..219aef41c 100644 --- a/org.argeo.util/src/org/argeo/util/UuidUtils.java +++ b/org.argeo.util/src/org/argeo/util/UuidUtils.java @@ -8,6 +8,8 @@ import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.time.Duration; import java.time.LocalDateTime; @@ -314,6 +316,9 @@ public class UuidUtils { * NAME BASED (version 3 and 5) */ + public final static String MD5 = "MD5"; + public final static String SHA1 = "SHA1"; + public static UUID nameUUIDv5(UUID namespace, String name) { Objects.requireNonNull(namespace, "Namespace cannot be null"); Objects.requireNonNull(name, "Name cannot be null"); @@ -343,6 +348,18 @@ public class UuidUtils { return UUID.nameUUIDFromBytes(arr); } + static byte[] sha1(byte[]... bytes) { + try { + MessageDigest digest = MessageDigest.getInstance(SHA1); + for (byte[] arr : bytes) + digest.update(arr); + byte[] checksum = digest.digest(); + return checksum; + } catch (NoSuchAlgorithmException e) { + throw new UnsupportedOperationException("SHA1 is not avalaible", e); + } + } + /* * RANDOM v4 */ @@ -388,14 +405,14 @@ public class UuidUtils { Objects.requireNonNull(uuid, "UUID cannot be null"); long msb = uuid.getMostSignificantBits(); long lsb = uuid.getLeastSignificantBits(); - return BytesUtils.toBytes(msb, lsb); + return toBytes(msb, lsb); } public static void copyBytes(UUID uuid, byte[] arr, int offset) { Objects.requireNonNull(uuid, "UUID cannot be null"); long msb = uuid.getMostSignificantBits(); long lsb = uuid.getLeastSignificantBits(); - BytesUtils.copyBytes(msb, lsb, arr, offset); + copyBytes(msb, lsb, arr, offset); } /** @@ -463,11 +480,7 @@ public class UuidUtils { /** To a 32 characters hex string without '-'. */ public static String toCompact(UUID uuid) { - return BytesUtils.toHexString(toBytes(uuid)); - } - - public static String firstBlock(UUID uuid) { - return uuid.toString().substring(0, 8); + return toHexString(toBytes(uuid)); } public static boolean isRandom(UUID uuid) { @@ -490,6 +503,37 @@ public class UuidUtils { return uuid.version() == 3 || uuid.version() == 5; } + final private static char[] hexArray = "0123456789abcdef".toCharArray(); + + /** Convert two longs to a byte array with length 16. */ + public static byte[] toBytes(long long1, long long2) { + byte[] result = new byte[16]; + for (int i = 0; i < 8; i++) + result[i] = (byte) ((long1 >> ((7 - i) * 8)) & 0xff); + for (int i = 8; i < 16; i++) + result[i] = (byte) ((long2 >> ((15 - i) * 8)) & 0xff); + return result; + } + + public static void copyBytes(long long1, long long2, byte[] arr, int offset) { + assert arr.length >= 16 + offset; + for (int i = offset; i < 8 + offset; i++) + arr[i] = (byte) ((long1 >> ((7 - i) * 8)) & 0xff); + for (int i = 8 + offset; i < 16 + offset; i++) + arr[i] = (byte) ((long2 >> ((15 - i) * 8)) & 0xff); + } + + /** Converts a byte array to an hex String. */ + public static String toHexString(byte[] bytes) { + char[] hexChars = new char[bytes.length * 2]; + for (int j = 0; j < bytes.length; j++) { + int v = bytes[j] & 0xFF; + hexChars[j * 2] = hexArray[v >>> 4]; + hexChars[j * 2 + 1] = hexArray[v & 0x0F]; + } + return new String(hexChars); + } + /** Singleton. */ private UuidUtils() { }