Remove ByteUtils
authorMathieu Baudier <mbaudier@argeo.org>
Sat, 22 Jan 2022 04:43:21 +0000 (05:43 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Sat, 22 Jan 2022 04:43:21 +0000 (05:43 +0100)
org.argeo.util/src/org/argeo/util/BytesUtils.java [deleted file]
org.argeo.util/src/org/argeo/util/DigestUtils.java
org.argeo.util/src/org/argeo/util/DirH.java
org.argeo.util/src/org/argeo/util/UuidUtils.java

diff --git a/org.argeo.util/src/org/argeo/util/BytesUtils.java b/org.argeo.util/src/org/argeo/util/BytesUtils.java
deleted file mode 100644 (file)
index c8fc129..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-package org.argeo.util;
-
-/** Utilities around byte arrays and byte buffers. */
-public class BytesUtils {
-       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 BytesUtils() {
-
-       }
-
-}
index e1a19160bd5a04afe23f834b6556e41bf53d91dd..38b4e7032a5cf35328b010771fe02a154f0c0d49 100644 (file)
@@ -1,7 +1,5 @@
 package org.argeo.util;
 
-import static org.argeo.util.BytesUtils.toHexString;
-
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
@@ -188,10 +186,17 @@ public class DigestUtils {
                }
        }
 
-       /** @deprecated Use {@link BytesUtils#toHexString(byte[])} instead */
-       @Deprecated
-       public static String encodeHexString(byte[] bytes) {
-               return BytesUtils.toHexString(bytes);
+       final private static char[] hexArray = "0123456789abcdef".toCharArray();
+
+       /** 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);
        }
 
 }
index ce7172a1294e69e092ce84aa112957a7312eeb44..013897d23644f09e49a9a5862651f90ce649da6a 100644 (file)
@@ -68,14 +68,14 @@ public class DirH {
        }
 
        public void print(PrintStream out) {
-               out.print(BytesUtils.toHexString(digest));
+               out.print(DigestUtils.toHexString(digest));
                if (dirName.length > 0) {
                        out.print(' ');
                        out.print(new String(dirName, charset));
                }
                out.print('\n');
                for (int i = 0; i < hashes.length; i++) {
-                       out.print(BytesUtils.toHexString(hashes[i]));
+                       out.print(DigestUtils.toHexString(hashes[i]));
                        out.print(' ');
                        out.print(new String(fileNames[i], charset));
                        out.print('\n');
index 88812d3826ab523a3c2e05085cd9e793ca59d63e..219aef41cedfc9267ba3935ab190f80bc7a442f0 100644 (file)
@@ -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() {
        }