Class WfCipher
java.lang.Object
org.whiteflagprotocol.java.crypto.WfCipher
- All Implemented Interfaces:
Destroyable
Whiteflag cipher class
This class represents a Whiteflag cipher. Instances of this are used to encrypt and decrypt a Whiteflag messages. This class needs to be instantiated with a Whiteflag encryption key instance.
- Since:
- 1.1
- Whiteflag version:
- v1-draft.6
- Whiteflag standard x-reference:
- 5.2.3 Key and Token Derivation
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
The byte length of an initialisation vector -
Method Summary
Modifier and TypeMethodDescriptionfinal byte[]
decrypt
(byte[] data) Decrypts the provided datafinal String
Decrypts the provided datafinal void
destroy()
Destroys this Whiteflag cipher by clearing the encryption keyfinal byte[]
encrypt
(byte[] data) Encrypts the provided datafinal String
Encrypts the provided datastatic final WfCipher
fromKey
(WfEncryptionKey key) Creates a new Whiteflag cipher instance from a Whiteflag encryption keyprotected final byte[]
Gets the current context to which the encryption key is boundfinal byte[]
Gets the current initialisation vectorfinal boolean
Determine if this Whiteflag cipher has been destroyed.final boolean
isSet()
Checks if the cipher has been fully set up for encryption or decryption.final WfCipher
setContext
(byte[] context) Sets the context to bind the encryption key; in Whiteflag this is usually the blockchain address of the message originatorfinal WfCipher
setContext
(String context) Sets the context to bind the encryption key; in Whiteflag this is usually the blockchain address of the message originatorfinal byte[]
Generates a new random initialisation vector.final WfCipher
setInitVector
(byte[] initialisationVector) Sets the initialisation vector.final WfCipher
setInitVector
(String initialisationVector) Sets the initialisation vector.
-
Field Details
-
IVBYTELENGTH
public static final int IVBYTELENGTHThe byte length of an initialisation vector- See Also:
-
-
Method Details
-
fromKey
Creates a new Whiteflag cipher instance from a Whiteflag encryption key- Parameters:
key
- the encryption key- Returns:
- a new Whiteflag cipher instance
- Throws:
IllegalArgumentException
- if the encryption key is invalidWfCryptoException
- if the cipher could not be created
-
destroy
Destroys this Whiteflag cipher by clearing the encryption key- Specified by:
destroy
in interfaceDestroyable
- Throws:
DestroyFailedException
- if the destroy operation failsIllegalStateException
- if the encryption key has already been destroyed
-
isDestroyed
public final boolean isDestroyed()Determine if this Whiteflag cipher has been destroyed.- Specified by:
isDestroyed
in interfaceDestroyable
- Returns:
- TRUE if destroyed, else FALSE
-
setContext
Sets the context to bind the encryption key; in Whiteflag this is usually the blockchain address of the message originator- Parameters:
context
- a hexadecimal string with the context specific information required to derive the correct key- Returns:
- this Whiteflag cipher object
-
setContext
Sets the context to bind the encryption key; in Whiteflag this is usually the blockchain address of the message originator- Parameters:
context
- a byte array with the context specific information required to derive the correct key- Returns:
- this Whiteflag cipher object
-
setInitVector
Generates a new random initialisation vector. This should be used when encrypting a new message.- Returns:
- a byte array with the random initialisation vector
- Throws:
WfCryptoException
- if a random in initialization vector could not be generated
-
setInitVector
Sets the initialisation vector. This should only be used to decrypt a message.- Parameters:
initialisationVector
- a hexadecimal string with the initialisation vector- Returns:
- this Whiteflag cipher object
-
setInitVector
Sets the initialisation vector. This should only be used to decrypt a message.- Parameters:
initialisationVector
- a byte array with the initialisation vector- Returns:
- this Whiteflag cipher object
-
getInitVector
public final byte[] getInitVector()Gets the current initialisation vector- Returns:
- a byte array with the initialisation vector
-
isSet
public final boolean isSet()Checks if the cipher has been fully set up for encryption or decryption.- Returns:
- TRUE if cipher has been fully set up, else FALSE
-
encrypt
Encrypts the provided data- Parameters:
data
- a hexadecimal string with the data to be encrypted- Returns:
- a hexadecimal string with the encrypted data
- Throws:
WfCryptoException
- if data could not be encrypted
-
encrypt
Encrypts the provided data- Parameters:
data
- a byte array the data to be encrypted- Returns:
- a byte array with the encrypted data
- Throws:
IllegalStateException
- if this cipher has not been fully set up or keys have been destroyedWfCryptoException
- if data could not be encrypted
-
decrypt
Decrypts the provided data- Parameters:
data
- a hexadecimal string with the data to be decrypted- Returns:
- a hexadecimal string with the decrypted data
- Throws:
WfCryptoException
- if data could not be decrypted
-
decrypt
Decrypts the provided data- Parameters:
data
- a byte array the data to be decrypted- Returns:
- a byte array with the decrypted data
- Throws:
IllegalStateException
- if this cipher has not been fully set up or keys have been destroyedWfCryptoException
- if data could not be decrypted
-
getContext
protected final byte[] getContext()Gets the current context to which the encryption key is bound- Returns:
- a byte array with the context, typically the blockchain address of the message originator
-