Class BinaryUtil

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

public final class BinaryUtil extends Object
A set of utility methods for handling binary data.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    The currently supported hash algorithms.
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    Decodes a Base64 string back to the original byte array.
    static String
    encodeBase64(byte[] data)
    Encodes a byte array to a string with Base64 characters (websafe).
    static String
    encodeHexString(byte[] data)
    Encodes a byte array to a string with hexadecimal numbers.
    static byte[]
    hashBytes(String alg, byte[] data)
    Calculates a digest hash on the specified byte array.
    static byte[]
    Calculates a digest hash on the data from an input stream.
    static String
    hashMD5(String input)
    Calculates the MD5 digest hash on the UTF-8 encoding of an input string.
    static String
    Calculates the SHA-256 digest hash on the bytes of an input file.
    static String
    Calculates the SHA-256 digest hash on the UTF-8 encoding of an input string.
    static String
    Calculates the SHA3-256 digest hash on the bytes of an input file.
    static String
    Calculates the SHA3-256 digest hash on the UTF-8 encoding of an input string.
    static byte[]
    hmacSHA256(byte[] secret, byte[] data)
    Calculates the HMAC-SHA256 hash of the specified data using the provided secret key.
    static byte[]
    hmacSHA256(String secret, String data)
    Calculates the HMAC-SHA256 hash of the specified data using the provided secret key.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 bytes 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
      See Also:
    • hashSHA3

      public static String hashSHA3(String input) throws NoSuchAlgorithmException
      Calculates the SHA3-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 SHA3-256 hash
      Throws:
      NoSuchAlgorithmException - if the SHA3-256 algorithm isn't available (should be RuntimeException)
      See Also:
    • hashSHA3

      public static String hashSHA3(InputStream input) throws NoSuchAlgorithmException, IOException
      Calculates the SHA3-256 digest hash on the bytes 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
      See Also:
    • hashBytes

      public static byte[] hashBytes(String alg, byte[] data) throws NoSuchAlgorithmException
      Calculates a digest hash on the specified byte array.
      Parameters:
      alg - the supported hash algorithm
      data - the data to hash
      Returns:
      the digest hash of the data
      Throws:
      NoSuchAlgorithmException - if the hash algorithm isn't available
      See Also:
    • hashBytes

      public static byte[] hashBytes(String alg, InputStream input) throws NoSuchAlgorithmException, IOException
      Calculates a digest hash on the data from an input stream.
      Parameters:
      alg - the supported hash algorithm
      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
      See Also:
    • hmacSHA256

      public static byte[] hmacSHA256(String secret, String data) throws SecurityException
      Calculates the HMAC-SHA256 hash of the specified data using the provided secret key.
      Parameters:
      secret - the secret key
      data - the data to hash
      Returns:
      the HMAC-SHA256 hash
      Throws:
      SecurityException - if the mac calculation failed
    • hmacSHA256

      public static byte[] hmacSHA256(byte[] secret, byte[] data) throws SecurityException
      Calculates the HMAC-SHA256 hash of the specified data using the provided secret key.
      Parameters:
      secret - the secret key
      data - the data to hash
      Returns:
      the HMAC-SHA256 hash
      Throws:
      SecurityException - if the mac calculation failed
    • 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