Class EncryptionKey

  • All Implemented Interfaces:
    Serializable

    public final class EncryptionKey
    extends Object
    implements Serializable

    Pointer to the an encryption key, or the encryption key itself. An encryption key can be seen as equivalent to a secret key, passphrase or password.

    Because this class is immutable, it does not implement IXMLConfigurable directly, but static methods saveToXML(XML, EncryptionKey) and loadFromXML(XML, EncryptionKey) can be used instead. The usage example below is for when used embedded in a parent configuration.

    XML configuration usage:

    
    <value>(The actual key or reference to it.)</value>
    <source>[key|file|environment|property]</source>
    <size>(Size in bits of encryption key. Default is 128.)</size>

    These XML configurable options can be nested in a parent tag of any name. The expected parent tag name is defined by the consuming classes.

    XML usage example:

    
    <sampleKey>
      <value>/path/to/my.key</value>
      <source>file</source>
    </sampleKey>

    The above example has the encryption key configuration is nested in a <passwordKey> tag. It uses a key stored in a file to decrypt a password for user credentials.

    Since:
    1.9.0
    Author:
    Pascal Essiembre
    See Also:
    EncryptionUtil, Credentials, Serialized Form
    • Constructor Detail

      • EncryptionKey

        public EncryptionKey​(String value,
                             EncryptionKey.Source source,
                             int size)
        Creates a new reference to an encryption key. The reference can either be the key itself, or a pointer to a file or environment variable containing the key (as defined by the supplied value type). The actual value can be any sort of string, and it is converted to an encryption key of length size using cryptographic algorithms. If the size is specified, it must be supported by your version of Java.
        Parameters:
        value - the encryption key
        size - the size in bits of the encryption key
        source - the type of value
      • EncryptionKey

        public EncryptionKey​(String value,
                             EncryptionKey.Source source)
        Creates a new reference to an encryption key. The reference can either be the key itself, or a pointer to a file or environment variable containing the key (as defined by the supplied value type).
        Parameters:
        value - the encryption key
        source - the type of value
      • EncryptionKey

        public EncryptionKey​(String value,
                             int size)
        Creates a new encryption key where the value is the actual key, and the number of key bits to generate is the size.
        Parameters:
        value - the encrption key
        size - the encryption key size in bits
      • EncryptionKey

        public EncryptionKey​(String value)
        Creates a new encryption key where the value is the actual key.
        Parameters:
        value - the encryption key
    • Method Detail

      • getValue

        public String getValue()
      • getSize

        public int getSize()
        Gets the size in bits of the encryption key. Default is 128.
        Returns:
        size in bits of the encryption key
        Since:
        1.15.0
      • resolve

        public String resolve()
        Locate the key according to its value type and return it. This method will always resolve the value each type it is invoked and never caches the key, unless the key value specified at construction time is the actual key.
        Returns:
        encryption key or null if the key does not exist for the specified type
      • loadFromXML

        public static EncryptionKey loadFromXML​(XML xml,
                                                EncryptionKey defaultKey)
        Gets an encryption key from an existing XML.
        Parameters:
        xml - the XML to get the key from
        defaultKey - default encryption key if it does not exist in XML
        Returns:
        encryption key
        Since:
        2.0.0
      • saveToXML

        public static void saveToXML​(XML xml,
                                     EncryptionKey key)
        Adds an encryption key to an existing XML.
        Parameters:
        xml - the XML to add the key to
        key - encryption key
        Since:
        2.0.0
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object