Class EncryptionKey
- java.lang.Object
-
- com.norconex.commons.lang.encrypt.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 methodssaveToXML(XML, EncryptionKey)
andloadFromXML(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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
EncryptionKey.Source
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_KEY_SIZE
-
Constructor Summary
Constructors Constructor Description EncryptionKey(String value)
Creates a new encryption key where the value is the actual key.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.EncryptionKey(String value, EncryptionKey.Source source)
Creates a new reference to an encryption key.EncryptionKey(String value, EncryptionKey.Source source, int size)
Creates a new reference to an encryption key.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(Object o)
int
getSize()
Gets the size in bits of the encryption key.EncryptionKey.Source
getSource()
String
getValue()
int
hashCode()
static EncryptionKey
loadFromXML(XML xml, EncryptionKey defaultKey)
Gets an encryption key from an existing XML.String
resolve()
Locate the key according to its value type and return it.static void
saveToXML(XML xml, EncryptionKey key)
Adds an encryption key to an existing XML.String
toString()
-
-
-
Field Detail
-
DEFAULT_KEY_SIZE
public static final int DEFAULT_KEY_SIZE
- See Also:
- Constant Field Values
-
-
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 keysize
- the size in bits of the encryption keysource
- 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 keysource
- 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 keysize
- 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()
-
getSource
public EncryptionKey.Source getSource()
-
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 fromdefaultKey
- 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 tokey
- encryption key- Since:
- 2.0.0
-
-