Package tilda.utils

Class EncryptionUtil


  • public class EncryptionUtil
    extends java.lang.Object
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static org.apache.logging.log4j.Logger LOG  
    • Constructor Summary

      Constructors 
      Constructor Description
      EncryptionUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String aes​(java.lang.String plaintext, java.lang.String aesKeyStr)  
      static java.lang.String bytesToHex​(byte[] digest)  
      static java.lang.String getToken​(int size)
      A short-hand version of getToken(int, boolean) with alphaNumOnly set to false
      static java.lang.String getToken​(int size, boolean alphaNumOnly)
      Generates a random string with "size" characters.
      static java.lang.String hash​(java.lang.String plaintext, java.lang.String salt)
      For text-base hashing, for example for a password to be saved in a database text field, this method uses the strongest available SHA function (i.e., at this time, SHA-512).
      static byte[] hash256​(java.lang.String plaintext, java.lang.String salt)
      Returns a byte array for a SHA-256 digest derived from plaintext.
      protected static byte[] hashByteArrray​(java.lang.String plainText, java.lang.String salt, java.lang.String shaAlgo)  
      protected static java.lang.String hashStr​(java.lang.String plaintext, java.lang.String salt, java.lang.String algo)  
      protected static java.lang.String hmac​(java.lang.String plaintext, java.lang.String key, java.lang.String algo)  
      static java.lang.String hmacSHA1​(java.lang.String plaintext, java.lang.String key)  
      static java.lang.String hmacSHA256​(java.lang.String plaintext, java.lang.String key)  
      • Methods inherited from class java.lang.Object

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

      • LOG

        protected static final org.apache.logging.log4j.Logger LOG
    • Constructor Detail

      • EncryptionUtil

        public EncryptionUtil()
    • Method Detail

      • hash

        public static java.lang.String hash​(java.lang.String plaintext,
                                            java.lang.String salt)
        For text-base hashing, for example for a password to be saved in a database text field, this method uses the strongest available SHA function (i.e., at this time, SHA-512). You are responsible for salting the plaintext for stronger pswd protection. A Base64 String is returned.
        Parameters:
        plaintext - The plaintext password.
        salt - The salt, which could be empty, or null (for no salt).
        Returns:
      • hash256

        public static byte[] hash256​(java.lang.String plaintext,
                                     java.lang.String salt)
        Returns a byte array for a SHA-256 digest derived from plaintext. This is meant primarily to be used as a key to an AES256 encryption algorithm.
        For text-base hashing, for example for a password to be saved in a database text field, make sure you use the plain #hash(String).
        Parameters:
        plaintext -
        Returns:
      • hashStr

        protected static java.lang.String hashStr​(java.lang.String plaintext,
                                                  java.lang.String salt,
                                                  java.lang.String algo)
      • hashByteArrray

        protected static byte[] hashByteArrray​(java.lang.String plainText,
                                               java.lang.String salt,
                                               java.lang.String shaAlgo)
      • getToken

        public static java.lang.String getToken​(int size)
        A short-hand version of getToken(int, boolean) with alphaNumOnly set to false
        Parameters:
        size - The size of the returned String. If size is < 8, then it will be changed to 8. If it's > 254, it will be changed to 254.
        Returns:
        A string of random characters of a minimum size of 8, and maximum size of 254.
      • getToken

        public static java.lang.String getToken​(int size,
                                                boolean alphaNumOnly)
        Generates a random string with "size" characters.
        Parameters:
        size - The size of the returned String. If size is < 8, then it will be changed to 8. If it's > 254, it will be changed to 254.
        alphaNumOnly - Whether the returned random string should only contain alphanumeric characters or not.
        Returns:
        A string of random characters of a minimum size of 8, and maximum size of 254.
      • bytesToHex

        public static java.lang.String bytesToHex​(byte[] digest)
      • aes

        public static java.lang.String aes​(java.lang.String plaintext,
                                           java.lang.String aesKeyStr)
      • hmacSHA1

        public static java.lang.String hmacSHA1​(java.lang.String plaintext,
                                                java.lang.String key)
                                         throws java.lang.Exception
        Throws:
        java.lang.Exception
      • hmacSHA256

        public static java.lang.String hmacSHA256​(java.lang.String plaintext,
                                                  java.lang.String key)
                                           throws java.lang.Exception
        Throws:
        java.lang.Exception
      • hmac

        protected static java.lang.String hmac​(java.lang.String plaintext,
                                               java.lang.String key,
                                               java.lang.String algo)
                                        throws java.security.NoSuchAlgorithmException,
                                               java.security.InvalidKeyException,
                                               java.io.UnsupportedEncodingException
        Throws:
        java.security.NoSuchAlgorithmException
        java.security.InvalidKeyException
        java.io.UnsupportedEncodingException