Class SecurityUtil


  • public final class SecurityUtil
    extends java.lang.Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static char[] bytesToChars​(byte[] bytes)
      Converts bytes to chars without using any external functions that might allocate additional buffers for the potentially sensitive data.
      static java.lang.String bytesToHex​(byte[] bytes, boolean toLowerCase)
      Computes the Hex encoded input.
      static byte[] charsToBytes​(char[] chars)
      Converts chars to bytes without using any external functions that might allocate additional buffers for the potentially sensitive data.
      static void clear​(byte[] bytes)
      Clears an array of potentially sensitive bytes
      static void clear​(char[] chars)
      Clears an array of potentially sensitive chars
      static java.lang.String computeBase64SHA1Hash​(byte[] bytes)
      Computes the base 64 encoded SHA1 hash of the input.
      static java.lang.String computeBase64SHA1Hash​(char[] input)
      Computes the base 64 encoded SHA1 hash of the input.
      static java.lang.String computeHexSHA1Hash​(byte[] bytes, boolean toLowerCase)
      Computes the Hex encoded SHA1 hash of the input.
      static java.security.PublicKey createPublicKey​(byte[] publicKeyBytes)
      Create a public key from encoded byte array.
      static byte[] decrypt​(GuardedByteArray guardedByteArray)
      Decrypts the value of a GuardedByteArray.
      static java.lang.String decrypt​(GuardedString guardedString)
      Decrypts the value of a GuardedString.
      static java.security.KeyPair generateKeyPair()
      Generating an EC key pair using the P-521 curve.
      static java.lang.Class<?> loadClass​(java.lang.String className, java.lang.Class<?> callingClass)
      Load a class with a given name.
      static boolean verifyBase64SHA1Hash​(char[] input, java.lang.String hash)
      Verifies the base 64-encoded SHA1 hash of the input.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • charsToBytes

        public static byte[] charsToBytes​(char[] chars)
        Converts chars to bytes without using any external functions that might allocate additional buffers for the potentially sensitive data. This guarantees the caller that they only need to cleanup the input and result.
        Parameters:
        chars - The chars
        Returns:
        The bytes
      • bytesToChars

        public static char[] bytesToChars​(byte[] bytes)
        Converts bytes to chars without using any external functions that might allocate additional buffers for the potentially sensitive data. This guarantees the caller that they only need to cleanup the input and result.
        Parameters:
        bytes - The bytes (to convert into characters).
        Returns:
        The characters (converted from the specified bytes).
      • clear

        public static void clear​(byte[] bytes)
        Clears an array of potentially sensitive bytes
        Parameters:
        bytes - The bytes. May be null.
      • clear

        public static void clear​(char[] chars)
        Clears an array of potentially sensitive chars
        Parameters:
        chars - The characters. May be null.
      • computeBase64SHA1Hash

        public static java.lang.String computeBase64SHA1Hash​(char[] input)
        Computes the base 64 encoded SHA1 hash of the input.
        Parameters:
        input - The input chars
        Returns:
        the hash
      • computeBase64SHA1Hash

        public static java.lang.String computeBase64SHA1Hash​(byte[] bytes)
        Computes the base 64 encoded SHA1 hash of the input.
        Parameters:
        bytes - The input bytes.
        Returns:
        the hash (computed from the input bytes).
      • computeHexSHA1Hash

        public static java.lang.String computeHexSHA1Hash​(byte[] bytes,
                                                          boolean toLowerCase)
        Computes the Hex encoded SHA1 hash of the input.
        Parameters:
        bytes - The input bytes.
        toLowerCase - true converts to lowercase or false to uppercase
        Returns:
        the hash (computed from the input bytes).
        Since:
        1.5
      • bytesToHex

        public static java.lang.String bytesToHex​(byte[] bytes,
                                                  boolean toLowerCase)
        Computes the Hex encoded input.
        Parameters:
        bytes - The input bytes to convert to Hex characters
        toLowerCase - true converts to lowercase or false to uppercase
        Returns:
        A String containing hexadecimal characters
        Since:
        1.5
      • verifyBase64SHA1Hash

        public static boolean verifyBase64SHA1Hash​(char[] input,
                                                   java.lang.String hash)
        Verifies the base 64-encoded SHA1 hash of the input.
        Parameters:
        input - The input chars
        hash - The expected hash
        Returns:
        true if the hash of the input characters matches the expected hash.
      • decrypt

        public static java.lang.String decrypt​(GuardedString guardedString)
        Decrypts the value of a GuardedString.
        Parameters:
        guardedString - the guarded string value.
        Returns:
        the clear string value.
        Since:
        1.4
      • decrypt

        public static byte[] decrypt​(GuardedByteArray guardedByteArray)
        Decrypts the value of a GuardedByteArray.
        Parameters:
        guardedByteArray - the guarded byte array value.
        Returns:
        the clear byte array value.
        Since:
        1.4
      • createPublicKey

        public static java.security.PublicKey createPublicKey​(byte[] publicKeyBytes)
        Create a public key from encoded byte array.
        Parameters:
        publicKeyBytes - encoded X509 key specification
        Returns:
        public key from byte array
      • generateKeyPair

        public static java.security.KeyPair generateKeyPair()
        Generating an EC key pair using the P-521 curve.
        Returns:
        new generated KeyPair
      • loadClass

        public static java.lang.Class<?> loadClass​(java.lang.String className,
                                                   java.lang.Class<?> callingClass)
                                            throws java.lang.ClassNotFoundException
        Load a class with a given name.

        It will try to load the class in the following order:

        • From Thread.currentThread().getContextClassLoader()
        • Using the basic Class.forName()
        • From SecurityUtil.class.getClassLoader()
        • From the callingClass.getClassLoader()
        Parameters:
        className - The name of the class to load
        callingClass - The Class object of the calling object
        Throws:
        java.lang.ClassNotFoundException - If the class cannot be found anywhere.