Package com.macasaet.fernet
Class Key
java.lang.Object
com.macasaet.fernet.Key
public class Key
extends java.lang.Object
A Fernet shared secret key.
Copyright © 2017 Carlos Macasaet.
- Author:
- Carlos Macasaet
-
Constructor Summary
-
Method Summary
Modifier and Type Method Description protected byte[]decrypt(byte[] cipherText, javax.crypto.spec.IvParameterSpec initializationVector)Decrypt the payload of a Fernet token.byte[]encrypt(byte[] payload, javax.crypto.spec.IvParameterSpec initializationVector)Encrypt a payload to embed in a Fernet tokenbooleanequals(java.lang.Object obj)static KeygenerateKey()Generate a random keystatic KeygenerateKey(java.security.SecureRandom random)Generate a random keyprotected java.lang.StringgetCipherTransformation()protected java.util.Base64.EncodergetEncoder()protected java.lang.StringgetEncryptionAlgorithm()protected byte[]getEncryptionKey()Warning: Modifying the returned byte array will write through to this object.protected javax.crypto.spec.SecretKeySpecgetEncryptionKeySpec()protected java.lang.StringgetSigningAlgorithm()protected byte[]getSigningKey()Warning: Modifying the returned byte array will write through to this object.protected java.security.KeygetSigningKeySpec()protected intgetTokenPrefixBytes()inthashCode()java.lang.Stringserialise()byte[]sign(byte version, java.time.Instant timestamp, javax.crypto.spec.IvParameterSpec initializationVector, byte[] cipherText)Generate an HMAC SHA-256 signature from the components of a Fernet token.protected byte[]sign(byte version, java.time.Instant timestamp, javax.crypto.spec.IvParameterSpec initializationVector, byte[] cipherText, java.io.ByteArrayOutputStream byteStream)voidwriteTo(java.io.OutputStream outputStream)Write the raw bytes of this key to the specified output stream.
-
Constructor Details
-
Key
public Key(byte[] signingKey, byte[] encryptionKey)Create a Key from individual components.- Parameters:
signingKey- a 128-bit (16 byte) key for signing tokens.encryptionKey- a 128-bit (16 byte) key for encrypting and decrypting token contents.
-
Key
public Key(byte[] concatenatedKeys)Create a Key from a payload containing the signing and encryption key.- Parameters:
concatenatedKeys- an array of 32 bytes of which the first 16 is the signing key and the last 16 is the encryption/decryption key
-
Key
public Key(java.lang.String string)- Parameters:
string- a Base 64 URL string in the format Signing-key (128 bits) || Encryption-key (128 bits)
-
-
Method Details
-
generateKey
Generate a random key- Returns:
- a new shared secret key
-
generateKey
Generate a random key- Parameters:
random- source of entropy- Returns:
- a new shared secret key
-
sign
public byte[] sign(byte version, java.time.Instant timestamp, javax.crypto.spec.IvParameterSpec initializationVector, byte[] cipherText)Generate an HMAC SHA-256 signature from the components of a Fernet token.- Parameters:
version- the Fernet version numbertimestamp- the seconds after the epoch that the token was generatedinitializationVector- the encryption and decryption initialization vectorcipherText- the encrypted content of the token- Returns:
- the HMAC signature
-
encrypt
public byte[] encrypt(byte[] payload, javax.crypto.spec.IvParameterSpec initializationVector)Encrypt a payload to embed in a Fernet token- Parameters:
payload- the raw bytes of the data to store in a tokeninitializationVector- random bytes from a high-entropy source to initialise the AES cipher- Returns:
- the AES-encrypted payload. The length will always be a multiple of 16 (128 bits).
- See Also:
decrypt(byte[], IvParameterSpec)
-
decrypt
protected byte[] decrypt(byte[] cipherText, javax.crypto.spec.IvParameterSpec initializationVector)Decrypt the payload of a Fernet token.
Warning: Do not call this unless the cipher text has first been verified. Attempting to decrypt a cipher text that has been tampered with will leak whether or not the padding is correct and this can be used to decrypt stolen cipher text.
- Parameters:
cipherText- the verified padded encrypted payload of a token. The length must be a multiple of 16 (128 bits).initializationVector- the random bytes used in the AES encryption of the token- Returns:
- the decrypted payload
- See Also:
encrypt(byte[], IvParameterSpec)
-
serialise
public java.lang.String serialise()- Returns:
- the Base 64 URL representation of this Fernet key
-
writeTo
public void writeTo(java.io.OutputStream outputStream) throws java.io.IOExceptionWrite the raw bytes of this key to the specified output stream.- Parameters:
outputStream- the target- Throws:
java.io.IOException- if the underlying I/O device cannot be written to
-
hashCode
public int hashCode()- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)- Overrides:
equalsin classjava.lang.Object
-
sign
protected byte[] sign(byte version, java.time.Instant timestamp, javax.crypto.spec.IvParameterSpec initializationVector, byte[] cipherText, java.io.ByteArrayOutputStream byteStream) throws java.io.IOException- Throws:
java.io.IOException
-
getSigningKeySpec
protected java.security.Key getSigningKeySpec()- Returns:
- an HMAC SHA-256 key for signing the token
-
getEncryptionKeySpec
protected javax.crypto.spec.SecretKeySpec getEncryptionKeySpec()- Returns:
- the AES key for encrypting and decrypting the token payload
-
getSigningKey
protected byte[] getSigningKey()Warning: Modifying the returned byte array will write through to this object.- Returns:
- the raw underlying signing key bytes
-
getEncryptionKey
protected byte[] getEncryptionKey()Warning: Modifying the returned byte array will write through to this object.- Returns:
- the raw underlying encryption key bytes
-
getTokenPrefixBytes
protected int getTokenPrefixBytes() -
getSigningAlgorithm
protected java.lang.String getSigningAlgorithm() -
getEncryptionAlgorithm
protected java.lang.String getEncryptionAlgorithm() -
getEncoder
protected java.util.Base64.Encoder getEncoder() -
getCipherTransformation
protected java.lang.String getCipherTransformation()
-