Class DigestUtils


  • public final class DigestUtils
    extends Object
    Utility class for computing cryptographic hashes.
    Author:
    Curtis Rueden, Johannes Schindelin
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static String base64​(byte[] bytes)
      Converts the given byte array to a base64 string.
      static byte[] best​(byte[] bytes)
      Gets the given byte array's best available hash.
      static byte[] best​(String s)
      Gets the given string's best available hash.
      static String bestBase64​(byte[] bytes)
      Gets the base64 string of the given byte array's best available hash.
      static String bestBase64​(String text)
      Gets the base64 string of the given string's best available hash.
      static String bestHex​(byte[] bytes)
      Gets the hex string of the given byte array's best available hash.
      static String bestHex​(String text)
      Gets the hex string of the given string's best available hash.
      static byte[] bytes​(int i)
      Converts the given integer into a byte array.
      static byte[] bytes​(String s)
      Converts the given string to a byte array.
      static byte[] digest​(String algorithm, byte[] bytes)
      Gets the given byte array's hash value according to the specified algorithm.
      static byte[] hash​(byte[] bytes)
      Gets the hash code of the given byte array, as a byte array.
      static byte[] hash​(String s)
      Gets the Java hash code of the given string, as a byte array.
      static String hex​(byte[] bytes)
      Converts the given byte array to a hexidecimal string.
      static byte[] md5​(byte[] bytes)
      Gets the given byte array's MD5 checksum, or null if unavailable.
      static byte[] sha1​(byte[] bytes)
      Gets the given byte array's SHA-1 checksum, or null if unavailable.
      static String string​(byte[] bytes)
      Converts the given byte array to a string.
    • Method Detail

      • string

        public static String string​(byte[] bytes)
        Converts the given byte array to a string. UTF-8 encoding is used if available, with the platform's default encoding as a fallback.
      • bytes

        public static byte[] bytes​(String s)
        Converts the given string to a byte array. UTF-8 encoding is used if available, with the platform's default encoding as a fallback.
      • bytes

        public static byte[] bytes​(int i)
        Converts the given integer into a byte array.
      • hex

        public static String hex​(byte[] bytes)
        Converts the given byte array to a hexidecimal string.
      • base64

        public static String base64​(byte[] bytes)
        Converts the given byte array to a base64 string.
      • hash

        public static byte[] hash​(String s)
        Gets the Java hash code of the given string, as a byte array.
        See Also:
        String.hashCode()
      • hash

        public static byte[] hash​(byte[] bytes)
        Gets the hash code of the given byte array, as a byte array.
        See Also:
        String.hashCode(), string(byte[])
      • sha1

        public static byte[] sha1​(byte[] bytes)
        Gets the given byte array's SHA-1 checksum, or null if unavailable.
      • md5

        public static byte[] md5​(byte[] bytes)
        Gets the given byte array's MD5 checksum, or null if unavailable.
      • digest

        public static byte[] digest​(String algorithm,
                                    byte[] bytes)
        Gets the given byte array's hash value according to the specified algorithm.
        Parameters:
        algorithm - The algorithm to use when generating the hash value.
        bytes - The byte array for which to compute the hash value.
        Returns:
        The computed hash value, or null if the digest algorithm is not available.
        See Also:
        MessageDigest
      • best

        public static byte[] best​(byte[] bytes)
        Gets the given byte array's best available hash. Tries SHA-1 first, then MD5, then Java hash code.
        See Also:
        sha1(byte[]), md5(byte[]), hash(byte[])
      • bestHex

        public static String bestHex​(byte[] bytes)
        Gets the hex string of the given byte array's best available hash.
        See Also:
        best(byte[]), hex(byte[])
      • bestBase64

        public static String bestBase64​(byte[] bytes)
        Gets the base64 string of the given byte array's best available hash.
        See Also:
        best(byte[]), base64(byte[])