com.worklight.wlclient.api

Class SecurityUtils

  • java.lang.Object
    • com.worklight.wlclient.api.SecurityUtils


  • public class SecurityUtils
    extends java.lang.Object
    MobileFirst Platform class that provides security utilities.
    • Constructor Summary

      Constructors 
      Constructor and Description
      SecurityUtils() 
    • Method Summary

      Methods 
      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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SecurityUtils

        public SecurityUtils()
    • Method Detail

      • generateKey

        public static java.lang.String generateKey(java.lang.String password,
                                   java.lang.String salt,
                                   int iterations)
                                            throws com.worklight.wlclient.api.SecurityUtilsException
        Generate key of the given length with the given password and salt, using the given number of iterations to generate the key.
        Parameters:
        password - the password to generate the key
        salt - the salt that is used to generate the key
        iterations - the number of iterations to use to encrypt the key
        Returns:
        the key, as a hexadecimal encoded string
        Throws:
        com.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 zero
      • generateSecretKey

        public static javax.crypto.SecretKey generateSecretKey(java.lang.String password,
                                               java.lang.String salt,
                                               int iterations,
                                               int keyLength)
                                                        throws java.lang.Exception
        Throws:
        java.lang.Exception
      • generateKey

        public static java.lang.String generateKey(java.lang.String password,
                                   java.lang.String salt,
                                   int iterations,
                                   int keyLength)
                                            throws com.worklight.wlclient.api.SecurityUtilsException
        Generate key of the given length with the given password, salt, and key length, using the given number of iterations to generate the key.
        Parameters:
        password - the password to generate the key
        salt - the salt that is used to generate the key
        iterations - the number of iterations to use to encrypt the key
        keyLength - the length, in bytes, of the key
        Returns:
        the key, as a hexadecimal encoded string
        Throws:
        com.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 zero
      • encrypt

        public static JSONObject encrypt(java.lang.String key,
                         java.lang.String plainText)
                                  throws com.worklight.wlclient.api.SecurityUtilsException
        Encrypt the given text with the given key. The encrypted text is included in the JSONObject that is returned.
        Parameters:
        key - the key with which to encrypt the text (hexadecimal encoded string)
        plainText - the text to encrypt
        Returns:
        JSONObject that contains the result
        Throws:
        com.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 encrypting
      • decrypt

        public static java.lang.String decrypt(java.lang.String key,
                               JSONObject encryptedObject)
                                        throws com.worklight.wlclient.api.SecurityUtilsException
        Decrypt the given encrypted object with the given key. The encrypted object must be the output of the encrypt function.
        Parameters:
        key - the key that is used to encrypt the object (hexadecimal encoded string)
        encryptedObject - the object to be decrypted
        Returns:
        String the decrypted text, in UTF-8 encoding
        Throws:
        com.worklight.wlclient.api.SecurityUtilsException - if the key is null or empty, or if the encryptedObject is null or invalid, or was encrypted in another environment
      • generateLocalKey

        public static byte[] generateLocalKey(int numBytes)
      • getRandomString

        public static java.lang.String getRandomString(int byteLength)
        Generate a hexadecimal-encoded random byte string locally with the given byte length.
        Parameters:
        byteLength - the length of the string to be generated, in byte size
        Returns:
        hexadecimal-encoded string of the generated random string
      • encodeBytesAsHexString

        public static java.lang.String encodeBytesAsHexString(byte[] bytes)
      • getRandomStringFromServer

        public static void getRandomStringFromServer(int byteLength,
                                     Context context,
                                     WLRequestListener listener)
        Gets a random string from the server. The random string is a hexadecimal encoded string of a random byte array of the specified length. The string is passed back in the request listener's WLRequestListener#onSuccess(WLResponse) method as the response's text.
        Parameters:
        byteLength - the length in bytes of the generated random string (maximum 64 bytes)
        context - the Android context from which this method is being called
        listener - the listener that is called back when the request finishes


© Copyright IBM Corp. 2006, 2015. All Rights Reserved.