Class BinaryUtil

java.lang.Object
org.rapidcontext.util.BinaryUtil

public final class BinaryUtil extends Object
A set of utility methods for handling binary data.
Version:
1.0
  • Method Details

    • hashMD5

      public static String hashMD5(String input) throws NoSuchAlgorithmException
      Calculates the MD5 digest hash on the UTF-8 encoding of an input string. The result will be returned as an hexadecimal string.
      Parameters:
      input - the input string
      Returns:
      the hexadecimal string with the MD5 hash
      Throws:
      NoSuchAlgorithmException - if the MD5 algorithm isn't available (should be RuntimeException)
    • hashSHA256

      public static String hashSHA256(String input) throws NoSuchAlgorithmException
      Calculates the SHA-256 digest hash on the UTF-8 encoding of an input string. The result will be returned as an hexadecimal string.
      Parameters:
      input - the input string
      Returns:
      the hexadecimal string with the SHA-256 hash
      Throws:
      NoSuchAlgorithmException - if the SHA-256 algorithm isn't available (should be RuntimeException)
    • hashSHA256

      public static String hashSHA256(InputStream input) throws NoSuchAlgorithmException, IOException
      Calculates the SHA-256 digest hash on the UTF-8 encoding of an input file. The result will be returned as an hexadecimal string.
      Parameters:
      input - the input stream to read
      Returns:
      the hexadecimal string with the SHA-256 hash
      Throws:
      NoSuchAlgorithmException - if the SHA-256 algorithm isn't available (should be RuntimeException)
      IOException - if the file couldn't be found or read
    • hashBytes

      public static byte[] hashBytes(String alg, byte[] data) throws NoSuchAlgorithmException
      Performs a digest hash on the specified byte array.
      Parameters:
      alg - the hash algorithm (e.g. "MD5" or "SHA-256")
      data - the data to hash
      Returns:
      the digest hash of the data
      Throws:
      NoSuchAlgorithmException - if the hash algorithm isn't available
    • hashBytes

      public static byte[] hashBytes(String alg, InputStream input) throws NoSuchAlgorithmException, IOException
      Performs a digest hash on the data from an input stream.
      Parameters:
      alg - the hash algorithm (e.g. "MD5" or "SHA-256")
      input - the input stream to read
      Returns:
      the digest hash of the data
      Throws:
      NoSuchAlgorithmException - if the hash algorithm isn't available
      IOException - if the input stream couldn't be read
    • encodeHexString

      public static String encodeHexString(byte[] data)
      Encodes a byte array to a string with hexadecimal numbers.
      Parameters:
      data - the byte array
      Returns:
      the hexadecimal string with the converted data
    • encodeBase64

      public static String encodeBase64(byte[] data)
      Encodes a byte array to a string with Base64 characters (websafe).
      Parameters:
      data - the byte array
      Returns:
      the Base64 string with the converted data
    • decodeBase64

      public static byte[] decodeBase64(String data)
      Decodes a Base64 string back to the original byte array.
      Parameters:
      data - the Base64-encoded string
      Returns:
      the decoded original byte array