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
FieldsModifier and TypeFieldDescriptionstatic final intThe byte length of an initialisation vector -
Method Summary
Modifier and TypeMethodDescriptionfinal byte[]decrypt(byte[] data) Decrypts the provided datafinal StringDecrypts the provided datafinal voiddestroy()Destroys this Whiteflag cipher by clearing the encryption keyfinal byte[]encrypt(byte[] data) Encrypts the provided datafinal StringEncrypts the provided datastatic final WfCipherfromKey(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 booleanDetermine if this Whiteflag cipher has been destroyed.final booleanisSet()Checks if the cipher has been fully set up for encryption or decryption.final WfCiphersetContext(byte[] context) Sets the context to bind the encryption key; in Whiteflag this is usually the blockchain address of the message originatorfinal WfCiphersetContext(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 WfCiphersetInitVector(byte[] initialisationVector) Sets the initialisation vector.final WfCiphersetInitVector(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:
destroyin 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:
isDestroyedin 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
-