public class SecurityUtils
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
CYPHER_TEXT_LABEL |
static java.lang.String |
ENCRYPTION_SOURCE_LABEL |
static java.lang.String |
IV_LABEL |
static java.lang.String |
VERSION_LABEL |
Constructor and Description |
---|
SecurityUtils() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
decrypt(java.lang.String key,
JSONObject encryptedObject)
Decrypt the given encrypted object with the given key.
|
static java.lang.String |
encodeBytesAsHexString(byte[] bytes) |
static JSONObject |
encrypt(java.lang.String key,
java.lang.String plainText)
Encrypt the given text with the given key.
|
static java.lang.String |
generateKey(java.lang.String password,
java.lang.String salt,
int iterations)
Generate key of the given length with the given password and salt, using the given number of iterations to generate the key.
|
static java.lang.String |
generateKey(java.lang.String password,
java.lang.String salt,
int iterations,
int keyLength)
Generate key of the given length with the given password, salt, and key length, using the given number of iterations to generate the key.
|
static byte[] |
generateLocalKey(int numBytes) |
static javax.crypto.SecretKey |
generateSecretKey(java.lang.String password,
java.lang.String salt,
int iterations,
int keyLength) |
static java.lang.String |
getRandomString(int byteLength)
Generate a hexadecimal-encoded random byte string locally with the given byte length.
|
static void |
getRandomStringFromServer(int byteLength,
Context context,
WLRequestListener listener)
Gets a random string from the server.
|
public static final java.lang.String CYPHER_TEXT_LABEL
public static final java.lang.String IV_LABEL
public static final java.lang.String ENCRYPTION_SOURCE_LABEL
public static final java.lang.String VERSION_LABEL
public static java.lang.String generateKey(java.lang.String password, java.lang.String salt, int iterations) throws com.worklight.wlclient.api.SecurityUtilsException
password
- the password to generate the keysalt
- the salt that is used to generate the keyiterations
- the number of iterations to use to encrypt the keycom.worklight.wlclient.api.SecurityUtilsException
- when there is a problem generating the key, or when the given password or salt is empty, or iterations is negative or zeropublic static javax.crypto.SecretKey generateSecretKey(java.lang.String password, java.lang.String salt, int iterations, int keyLength) throws java.lang.Exception
java.lang.Exception
public static java.lang.String generateKey(java.lang.String password, java.lang.String salt, int iterations, int keyLength) throws com.worklight.wlclient.api.SecurityUtilsException
password
- the password to generate the keysalt
- the salt that is used to generate the keyiterations
- the number of iterations to use to encrypt the keykeyLength
- the length, in bytes, of the keycom.worklight.wlclient.api.SecurityUtilsException
- when there is a problem generating the key, or when the given password or salt is empty, or iterations or key length is negative or zeropublic static JSONObject encrypt(java.lang.String key, java.lang.String plainText) throws com.worklight.wlclient.api.SecurityUtilsException
key
- the key with which to encrypt the text (hexadecimal encoded string)plainText
- the text to encryptcom.worklight.wlclient.api.SecurityUtilsException
- if the key or plainText is empty or null, or if the key is invalid and there is a problem while encryptingpublic static java.lang.String decrypt(java.lang.String key, JSONObject encryptedObject) throws com.worklight.wlclient.api.SecurityUtilsException
key
- the key that is used to encrypt the object (hexadecimal encoded string)encryptedObject
- the object to be decryptedcom.worklight.wlclient.api.SecurityUtilsException
- if the key is null or empty, or if the encryptedObject is null or invalid, or was encrypted in another environmentpublic static byte[] generateLocalKey(int numBytes)
public static java.lang.String getRandomString(int byteLength)
byteLength
- the length of the string to be generated, in byte sizepublic static java.lang.String encodeBytesAsHexString(byte[] bytes)
public static void getRandomStringFromServer(int byteLength, Context context, WLRequestListener listener)
WLRequestListener.onSuccess(WLResponse)
method as the response's text.byteLength
- the length in bytes of the generated random string (maximum 64 bytes)context
- the Android context from which this method is being calledlistener
- the listener that is called back when the request finishes