Class CryptoUtil

java.lang.Object
overit.geocall.util.CryptoUtil

public class CryptoUtil extends Object
This class contains a whole series of utility methods for encryption.
  • Field Details

    • clock

      protected static Clock clock
    • passwordPadding

      protected boolean passwordPadding
      Normalizes the password's length to adapt it to the crypt algorithm.
    • b64Return

      protected boolean b64Return
      Introduces the carriage return every 76 characters to match the base64 coding standard.
  • Constructor Details

    • CryptoUtil

      public CryptoUtil()
  • Method Details

    • setCompression

      public void setCompression(boolean compression)
      Sets the value of the attribute that defines if is active the gzip compression.
      Parameters:
      compression - true to activate the compression, false (default value) otherwise.
    • setB64Return

      public void setB64Return(boolean b64Return)
      Sets the value of the attribute that defines if is introduced the carriage return every 76 character to match te requirements of the base 64 coding standard.
      Parameters:
      b64Return - true (default value) to activate the carriage return, false otherwise.
    • setPasswordPadding

      public void setPasswordPadding(boolean pp)
      Sets the value of the attribute that defines if the password length will be normalized to adapt it to the crypt algorithm.
      Parameters:
      pp - true (default value) to activate the normalization, false otherwise.
    • blowfishKeyGenerate

      public static String blowfishKeyGenerate(int keySize) throws CryptoException
      Generates the key for the Blowfish cipher algorithm.
      Parameters:
      keySize - The key size (from 32 to 448)
      Returns:
      The key or null if there is an exception during the key creation.
      Throws:
      CryptoException - exception thrown in case of error occurred during the blowfish key generation.
    • blowfishEncrypt

      public static String blowfishEncrypt(String key, String value) throws CryptoException
      Encrypts the value using the Blowfish cipher algorithm and the key passed as parameter.
      Parameters:
      key - The key used to encrypt.
      value - The value to encrypt.
      Returns:
      The String that represents the encrypted value.
      Throws:
      CryptoException - exception thrown in case of error occurred during the encryption.
    • blowfishDecrypt

      public static String blowfishDecrypt(String key, String value) throws CryptoException
      Decrypts the value using the Blowfish algorithm and the key passed as parameter.
      Parameters:
      key - The key used to decrypt.
      value - The value to decrypt.
      Returns:
      The String that represents the decrypted value.
      Throws:
      CryptoException - exception thrown in case of error occurred during the decryption.
    • blowfishEncrypt

      public String blowfishEncrypt(String key, int keyLength, String value) throws CryptoException
      Encrypts the value using the Blowfish algorithm and the key passed as parameter. If required (see setPasswordPadding(boolean)) it normalizes the key to the dimension passed as parameter.
      Parameters:
      key - The key used to encrypt.
      keyLength - the to-be-derived key length (it can be from 32 to 448).
      value - The value to encrypt.
      Returns:
      The String that represents the encrypted value.
      Throws:
      CryptoException - exception thrown in case of error occurred during the encryption.
    • blowfishDecrypt

      public String blowfishDecrypt(String key, int keyLength, String value) throws CryptoException
      Decrypts the value using the Blowfish algorithm and the key passed as parameter. If required (see setPasswordPadding(boolean)) it normalizes the key to the dimension passed as parameter.
      Parameters:
      key - The key used to decrypt.
      keyLength - the to-be-derived key length (it can be from 32 to 448).
      value - The value to decrypt.
      Returns:
      The String that represents the decrypted value.
      Throws:
      CryptoException - exception thrown in case of error occurred during the decryption.
    • tripleDesKeyGenerate

      public static String tripleDesKeyGenerate(int keySize) throws CryptoException
      Generates the key for the Triple DES cipher algorithm.
      Parameters:
      keySize - the key size. This is an algorithm-specific metric, specified in number of bits. (it can be 112 or 168).
      Returns:
      The key once generated.
      Throws:
      CryptoException - exception thrown in case of error occurred during the Triple DES key generation.
    • tripleDesEncrypt

      public static String tripleDesEncrypt(String key, String value) throws CryptoException
      Encrypts the value using the Triple DES cipher algorithm and the key passed as parameter.
      Parameters:
      key - The key used to encrypt.
      value - The value to encrypt.
      Returns:
      The String that represents the encrypted value.
      Throws:
      CryptoException - exception thrown in case of error occurred during the encryption.
    • tripleDesDecrypt

      public static String tripleDesDecrypt(String key, String value) throws CryptoException
      Decrypts the value using the Triple DES cipher algorithm and the key passed as parameter.
      Parameters:
      key - The key used to decrypt.
      value - The value to decrypt.
      Returns:
      The String that represents the decrypted value.
      Throws:
      CryptoException - exception thrown in case of error occurred during the decryption.
    • tripleDESEncrypt

      public String tripleDESEncrypt(String key, String value) throws CryptoException
      Encrypts the value using the Triple DES cipher algorithm and the key passed as parameter.
      Parameters:
      key - The key used to encrypt.
      value - The value to encrypt.
      Returns:
      The String that represents the encrypted value.
      Throws:
      CryptoException - exception thrown in case of error occurred during the encryption.
    • tripleDESDecrypt

      public String tripleDESDecrypt(String key, String value) throws CryptoException
      Decrypts the value using the Triple DES cipher algorithm and the key passed as parameter.
      Parameters:
      key - The key used to decrypt.
      value - The value to decrypt.
      Returns:
      The String that represents the decrypted value.
      Throws:
      CryptoException - exception thrown in case of error occurred during the decryption.
    • aesKeyGenerate

      public static String aesKeyGenerate(int keySize) throws CryptoException
      Generates the key for the Advanced Encryption Standard cipher algorithm.
      Parameters:
      keySize - the key size. This is an algorithm-specific metric, specified in number of bits. (it can be 128, 192 or 256).
      Returns:
      The key once generated.
      Throws:
      CryptoException - exception thrown in case of error occurred during the Advanced Encryption Standard key generation.
    • aesEncrypt

      public static String aesEncrypt(String key, String value) throws CryptoException
      Encrypts the value using the Advanced Encryption Standard cipher algorithm and the key passed as parameter.
      Parameters:
      key - The key used to encrypt.
      value - The value to encrypt.
      Returns:
      The String that represents the encrypted value.
      Throws:
      CryptoException - exception thrown in case of error occurred during the encryption.
    • aesDecrypt

      public static String aesDecrypt(String key, String value) throws CryptoException
      Decrypts the value using the Advanced Encryption Standard cipher algorithm and the key passed as parameter.
      Parameters:
      key - The key used to decrypt.
      value - The value to decrypt.
      Returns:
      The String that represents the decrypted value.
      Throws:
      CryptoException - exception thrown in case of error occurred during the decryption.
    • aesEncrypt

      public String aesEncrypt(String key, int keyLength, String value) throws CryptoException
      Encrypts the value using the Advanced Encryption Standard algorithm and the key passed as parameter. If required (see setPasswordPadding(boolean)) it normalizes the key to the dimension passed as parameter.
      Parameters:
      key - The key used to encrypt.
      keyLength - The to-be-derived key length (it can be 128, 192 or 256).
      value - The value to encrypt.
      Returns:
      The String that represents the encrypted value.
      Throws:
      CryptoException - exception thrown in case of error occurred during the encryption.
    • aesDecrypt

      public String aesDecrypt(String key, int keyLength, String value) throws CryptoException
      Decrypts the value using the Advanced Encryption Standard algorithm and the key passed as parameter. If required (see setPasswordPadding(boolean)) it normalizes the key to the dimension passed as parameter.
      Parameters:
      key - The key used to decrypt.
      keyLength - The to-be-derived key length (it can be 128, 192 or 256).
      value - The value to decrypt.
      Returns:
      The String that represents the decrypted value.
      Throws:
      CryptoException - exception thrown in case of error occurred during the decryption.
    • aesCBCEncrypt

      public String aesCBCEncrypt(String key, int keyLength, String value) throws CryptoException
      Encrypts the value using the "AES/CBC/PKCS5Padding" transform and the key passed as parameter. If required (see setPasswordPadding(boolean)) it normalizes the key to the dimension passed as parameter.
      Parameters:
      key - The key used to encrypt.
      keyLength - The to-be-derived key length (it can be 128, 192 or 256).
      value - The value to encrypt.
      Returns:
      The String that represents the encrypted value.
      Throws:
      CryptoException - exception thrown in case of error occurred during the encryption.
    • aesCBCDecrypt

      public String aesCBCDecrypt(String key, int keyLength, String value) throws CryptoException
      Decrypts the value using the "AES/CBC/PKCS5Padding" transform and the key passed as parameter. If required (see setPasswordPadding(boolean)) it normalizes the key to the dimension passed as parameter.
      Parameters:
      key - The key used to decrypt.
      keyLength - The to-be-derived key length (it can be 128, 192 or 256).
      value - The value to decrypt.
      Returns:
      The String that represents the decrypted value.
      Throws:
      CryptoException - exception thrown in case of error occurred during the decryption.
    • aesECBEncrypt

      public String aesECBEncrypt(String key, int keyLength, String value) throws CryptoException
      Encrypts the value using the "AES/ECB/PKCS5Padding" transform and the key passed as parameter. If required (see setPasswordPadding(boolean)) it normalizes the key to the dimension passed as parameter.
      Parameters:
      key - The key used to encrypt.
      keyLength - The to-be-derived key length (it can be 128, 192 or 256).
      value - The value to encrypt.
      Returns:
      The String that represents the encrypted value.
      Throws:
      CryptoException - exception thrown in case of error occurred during the decryption.
    • aesECBDecrypt

      public String aesECBDecrypt(String key, int keyLength, String value) throws CryptoException
      Decrypts the value using the "AES/ECB,/PKCS5Padding" transform and the key passed as parameter. If required (see setPasswordPadding(boolean)) it normalizes the key to the dimension passed as parameter.
      Parameters:
      key - The key used to decrypt.
      keyLength - The to-be-derived key length (it can be 128, 192 or 256).
      value - The value to decrypt.
      Returns:
      The String that represents the decrypted value.
      Throws:
      CryptoException - exception thrown in case of error occurred during the decryption.
    • encrypt

      protected String encrypt(String algorithm, String transform, String key, String value, boolean useIV) throws CryptoException
      Throws:
      CryptoException
    • decrypt

      protected String decrypt(String algorithm, String transform, String key, String value, boolean useIV) throws CryptoException
      Throws:
      CryptoException
    • encrypt

      protected String encrypt(String algorithm, String key, String value) throws CryptoException
      Returns a String about the provided value once encrypted, using the provided parameters.

      WARNING: Legacy interface, better not use: lets you pass algorithm (e.g. "AES") to Cipher.getInstance(), so - per JSA specification - actual transform "algorithm/mode/padding" is automatically chosen as the default valid for the provider (may vary across platforms or implementations).

      Parameters:
      algorithm - String about the encryption algorithm to use.
      key - String about the encryption key.
      value - String about the value to encrypt.
      Returns:
      a String about the provided value once encrypted, using the provided parameters.
      Throws:
      CryptoException - exception thrown in case of error occurred during the encryption.
    • decrypt

      protected String decrypt(String algorithm, String key, String value) throws CryptoException
      Returns a String about the provided value once decrypted, using the provided parameters.

      WARNING: Legacy interface, better not use: lets you pass algorithm (e.g. "AES") to Cipher.getInstance(), so - per JSA specification - actual transform "algorithm/mode/padding" is automatically chosen as the default valid for the provider (may vary across platforms or implementations).

      Parameters:
      algorithm - String about the decryption algorithm to use.
      key - String about the decryption key.
      value - String about the value to decrypt.
      Returns:
      a String about the provided value once decrypted, using the provided parameters.
      Throws:
      CryptoException - exception thrown in case of error occurred during the decryption.
    • hash

      public static String hash(DigestAlgorithm digestAlgorithm, String salt, String message) throws CryptoException
      Returns a string that corresponds to the hashed message.
      Parameters:
      digestAlgorithm - The digestAlgorithm to use. It can be HEX_MD5, HEX_SHA1, HEX_SHA256, HEX_SHA512, B64_MD5, B64_SHA1, B64_SHA256, B64_SHA512, OB64_SHA1, OB64_SHA256, OB64_SHA512
      salt - The random string used as an additional input by the hashing mechanism.
      message - The message.
      Returns:
      The hashed message.
      Throws:
      CryptoException - exception thrown in case of unexpected digestAlgorithm.
    • generateToken

      public static String generateToken(Identity id) throws CryptoException
      Generates a new authentication token for the successfully logged user.
      Parameters:
      id - The identity of the user.
      Returns:
      A new authentication token for the successfully logged user.
      Throws:
      CryptoException - exception thrown in case of error occurred during the hash calculation.