Class EncryptionUtilities

java.lang.Object
com.cedarsoftware.util.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 Details

    • 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.
    • fastSHA1

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

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

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

      public static String calculateFileHash(FileChannel ch, MessageDigest d) throws IOException
      Throws:
      IOException
    • 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.
    • getDigest

      public static MessageDigest getDigest(String digest)
    • getMD5Digest

      public static MessageDigest getMD5Digest()
    • calculateSHA1Hash

      public static String calculateSHA1Hash(byte[] bytes)
      Calculate an MD5 Hash String from the passed in byte[].
      Parameters:
      bytes - byte[] of bytes for which to compute the SHA1
      Returns:
      the SHA-1 as a String of HEX digits
    • getSHA1Digest

      public static MessageDigest getSHA1Digest()
    • calculateSHA256Hash

      public static String calculateSHA256Hash(byte[] bytes)
      Calculate an SHA-256 Hash String from the passed in byte[].
      Parameters:
      bytes - byte[] for which to compute the SHA-2 (SHA-256)
      Returns:
      the SHA-2 as a String of HEX digits
    • getSHA256Digest

      public static MessageDigest getSHA256Digest()
    • calculateSHA512Hash

      public static String calculateSHA512Hash(byte[] bytes)
      Calculate an SHA-512 Hash String from the passed in byte[].
      Parameters:
      bytes - byte[] for which to compute the SHA-3 (SHA-512)
      Returns:
      the SHA-3 as a String of HEX digits
    • getSHA512Digest

      public static MessageDigest getSHA512Digest()
    • createCipherBytes

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

      public static Cipher createAesEncryptionCipher(String key) throws Exception
      Throws:
      Exception
    • createAesDecryptionCipher

      public static Cipher createAesDecryptionCipher(String key) throws Exception
      Throws:
      Exception
    • createAesCipher

      public static Cipher createAesCipher(String key, int mode) throws Exception
      Throws:
      Exception
    • 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 - if the requested Cipher instance does not exist.
    • encrypt

      public static String encrypt(String key, String content)
      Get hex String of content String encrypted.
      Parameters:
      key - String value of the encryption key (passphrase)
      content - String value of the content to be encrypted using the passed in encryption key
      Returns:
      String of the encrypted content (HEX characters), using AES-128
    • 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
      Parameters:
      key - String encryption key that was used to encryption the passed in hexStr of characters.
      hexStr - String encrypted bytes (as a HEX string)
      Returns:
      String of original content, decrypted using the passed in encryption/decryption key against the passed in hex String.
    • decryptBytes

      public static byte[] decryptBytes(String key, String hexStr)
      Get unencrypted byte[] from encrypted hex String
      Parameters:
      key - String encryption/decryption key
      hexStr - String of HEX bytes that were encrypted with an encryption key
      Returns:
      byte[] of original bytes (if the same key to encrypt the bytes was passed to decrypt the bytes).
    • calculateHash

      public static String calculateHash(MessageDigest d, byte[] bytes)
      Calculate a hash String from the passed in byte[].
      Parameters:
      d - MessageDigest to update with the passed in bytes
      bytes - byte[] of bytes to hash
      Returns:
      String hash of the passed in MessageDigest, after being updated with the passed in bytes, as a HEX string.