Class EncryptionUtilities


  • public class EncryptionUtilities
    extends Object
    Useful encryption utilities that simplify tasks like getting an encrypted String return value (or MD5 hash String) for String or Stream input.
    Author:
    John DeRegnaucourt ([email protected])
    Copyright (c) Cedar Software LLC

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
    • Method Detail

      • fastMD5

        public static String fastMD5​(File file)
        Super-fast MD5 calculation from entire file. Uses FileChannel and direct ByteBuffer (internal JVM memory).
        Parameters:
        file - File that from which to compute the MD5
        Returns:
        String MD5 value.
      • calculateMD5Hash

        public static String calculateMD5Hash​(byte[] bytes)
        Calculate an MD5 Hash String from the passed in byte[].
        Parameters:
        bytes - byte[] for which to obtain the MD5 hash.
        Returns:
        String of hex digits representing MD5 hash.
      • calculateSHA1Hash

        public static String calculateSHA1Hash​(byte[] bytes)
        Calculate an MD5 Hash String from the passed in byte[].
      • calculateSHA256Hash

        public static String calculateSHA256Hash​(byte[] bytes)
        Calculate an SHA-256 Hash String from the passed in byte[].
      • getSHA256Digest

        public static MessageDigest getSHA256Digest()
      • calculateSHA512Hash

        public static String calculateSHA512Hash​(byte[] bytes)
        Calculate an SHA-512 Hash String from the passed in byte[].
      • getSHA512Digest

        public static MessageDigest getSHA512Digest()
      • createCipherBytes

        public static byte[] createCipherBytes​(String key,
                                               int bitsNeeded)
      • createAesCipher

        public static Cipher createAesCipher​(Key key,
                                             int mode)
                                      throws Exception
        Creates a Cipher from the passed in key, using the passed in mode.
        Parameters:
        key - SecretKeySpec
        mode - Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE
        Returns:
        Cipher instance created with the passed in key and mode.
        Throws:
        Exception
      • encrypt

        public static String encrypt​(String key,
                                     String content)
        Get hex String of content String encrypted.
      • encryptBytes

        public static String encryptBytes​(String key,
                                          byte[] content)
      • decrypt

        public static String decrypt​(String key,
                                     String hexStr)
        Get unencrypted String from encrypted hex String
      • decryptBytes

        public static byte[] decryptBytes​(String key,
                                          String hexStr)
        Get unencrypted byte[] from encrypted hex String
      • calculateHash

        public static String calculateHash​(MessageDigest d,
                                           byte[] bytes)
        Calculate an SHA-256 Hash String from the passed in byte[].