Class KeyFactory

java.lang.Object
com.gigaspaces.security.encoding.KeyFactory

public class KeyFactory extends Object
A convenient factory for handling secret keys.

Keys can be randomly generated using generateKey(String) or using one of the other generate key methods and supplying a key. The SecretKey can then be stored as a resource using storeKey(SecretKey, File), and loaded using the loadKey(String).

 Example usage: Generate a key and store it into a file.

  SecretKey key = KeyFactory.generateKey("AES");
  File file = new File("my-secret.key");
  AesKeyFactory.storeKey(key, file);
  ...
  AesKeyFactory.loadKey(file.getAbsolutePath());
 
Since:
7.0.1
Author:
Moran Avigdor
  • Constructor Details

    • KeyFactory

      public KeyFactory()
  • Method Details

    • generateKey

      public static SecretKey generateKey(String algorithm) throws EncodingException
      Generate a 128 bit key using the key generator of the algorithm provided.
      Parameters:
      algorithm - algorithm to use.
      Returns:
      a secret key.
      Throws:
      EncodingException - If any exception occurred while generating a key.
    • generateKey

      public static SecretKey generateKey(String key, String algorithm) throws EncodingException
      Generate a key based on the provided secret.
      Parameters:
      key - a string to convert using UTF-8 (at least 8 bytes).
      algorithm - algorithm to use
      Returns:
      a secret key
      Throws:
      EncodingException - If any exception occurred while generating a key.
    • generateKey

      public static SecretKey generateKey(byte[] key, String algorithm) throws EncodingException
      Generate a key based on a secret key from the given byte array.
      Parameters:
      key - a key to use (at least 8 bytes).
      algorithm - algorithm to use
      Returns:
      a secret key
      Throws:
      EncodingException - If any exception occurred while generating a key.
    • loadKey

      public static SecretKey loadKey(String resourceName)
      Loads the SecretKey file from a resource located in the classpath or jar.
      Parameters:
      resourceName - The full resource name. Can't be null.
      Returns:
      the secret key stored in the specified resource.
    • storeKey

      public static void storeKey(SecretKey key, File file)
      Stores the secret key to the provided file.
      Parameters:
      key - a secret key.
      file - a file to write to.