Package io.ably.lib.util
Class Crypto
- java.lang.Object
-
- io.ably.lib.util.Crypto
-
public class Crypto extends java.lang.ObjectUtility classes and interfaces for message payload encryption. This class supports AES/CBC/PKCS5 with a default key length of 256 bits but supporting other key lengths. Other algorithms and chaining modes are not supported directly, but supportable by extending/implementing the base classes and interfaces here. Secure random data for creation of Initialisation Vectors (IVs) and keys is obtained from the default system SecureRandom. Future extensions of this class might make the SecureRandom pluggable or at least seedable with client-provided entropy. Each message payload is encrypted with an IV in CBC mode, and the IV is concatenated with the resulting raw ciphertext to construct the "ciphertext" data passed to the recipient.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceCrypto.ChannelCipherInterface for a ChannelCipher instance that may be associated with a Channel.static classCrypto.CipherParamsA class encapsulating the client-specifiable parameters for the cipher.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringDEFAULT_ALGORITHMstatic intDEFAULT_BLOCKLENGTHstatic intDEFAULT_KEYLENGTH
-
Constructor Summary
Constructors Constructor Description Crypto()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static byte[]generateRandomKey()static byte[]generateRandomKey(int keyLength)static Crypto.ChannelCiphergetCipher(ChannelOptions opts)Internal; get a ChannelCipher instance based on the given ChannelOptionsstatic Crypto.CipherParamsgetDefaultParams()Obtain a default CipherParams.static Crypto.CipherParamsgetDefaultParams(byte[] key)Obtain a default CipherParams.static Crypto.CipherParamsgetDefaultParams(java.lang.String base64Key)Obtain a default CipherParams using Base64-encoded key.static Crypto.CipherParamsgetParams(java.lang.String algorithm, byte[] key)static Crypto.CipherParamsgetParams(java.lang.String algorithm, byte[] key, byte[] iv)static Crypto.CipherParamsgetParams(java.lang.String algorithm, int keyLength)static java.lang.StringgetRandomMessageId()
-
-
-
Field Detail
-
DEFAULT_ALGORITHM
public static final java.lang.String DEFAULT_ALGORITHM
- See Also:
- Constant Field Values
-
DEFAULT_KEYLENGTH
public static final int DEFAULT_KEYLENGTH
-
DEFAULT_BLOCKLENGTH
public static final int DEFAULT_BLOCKLENGTH
- See Also:
- Constant Field Values
-
-
Method Detail
-
getDefaultParams
public static Crypto.CipherParams getDefaultParams()
Obtain a default CipherParams. This uses default algorithm, mode and padding and key length. A key and IV are generated using the default system SecureRandom; the key may be obtained from the returned CipherParams for out-of-band distribution to other clients.- Returns:
- the CipherParams
-
getDefaultParams
public static Crypto.CipherParams getDefaultParams(byte[] key)
Obtain a default CipherParams. This uses default algorithm, mode and padding and initialises a key based on the given key data. The cipher key length is derived from the length of the given key data. An IV is generated using the default system SecureRandom. Use this method of constructing CipherParams if initialising a Channel with a client-provided key, or to obtain a system-generated key of a non-default key length.- Returns:
- the CipherParams
-
getDefaultParams
public static Crypto.CipherParams getDefaultParams(java.lang.String base64Key)
Obtain a default CipherParams using Base64-encoded key. Same as above, throws IllegalArgumentException if base64Key is invalid- Parameters:
base64Key-- Returns:
-
getParams
public static Crypto.CipherParams getParams(java.lang.String algorithm, int keyLength)
-
getParams
public static Crypto.CipherParams getParams(java.lang.String algorithm, byte[] key) throws java.security.NoSuchAlgorithmException
- Throws:
java.security.NoSuchAlgorithmException
-
getParams
public static Crypto.CipherParams getParams(java.lang.String algorithm, byte[] key, byte[] iv) throws java.security.NoSuchAlgorithmException
- Throws:
java.security.NoSuchAlgorithmException
-
generateRandomKey
public static byte[] generateRandomKey(int keyLength)
-
generateRandomKey
public static byte[] generateRandomKey()
-
getCipher
public static Crypto.ChannelCipher getCipher(ChannelOptions opts) throws AblyException
Internal; get a ChannelCipher instance based on the given ChannelOptions- Parameters:
opts-- Returns:
- Throws:
AblyException
-
getRandomMessageId
public static java.lang.String getRandomMessageId()
-
-