Package com.cedarsoftware.util
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 Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
calculateHash(MessageDigest d, byte[] bytes)
Calculate an SHA-256 Hash String from the passed in byte[].static String
calculateMD5Hash(byte[] bytes)
Calculate an MD5 Hash String from the passed in byte[].static String
calculateMD5Hash(FileChannel ch)
static String
calculateSHA1Hash(byte[] bytes)
Calculate an MD5 Hash String from the passed in byte[].static String
calculateSHA256Hash(byte[] bytes)
Calculate an SHA-256 Hash String from the passed in byte[].static String
calculateSHA512Hash(byte[] bytes)
Calculate an SHA-512 Hash String from the passed in byte[].static Cipher
createAesCipher(String key, int mode)
static Cipher
createAesCipher(Key key, int mode)
Creates a Cipher from the passed in key, using the passed in mode.static Cipher
createAesDecryptionCipher(String key)
static Cipher
createAesEncryptionCipher(String key)
static byte[]
createCipherBytes(String key, int bitsNeeded)
static String
decrypt(String key, String hexStr)
Get unencrypted String from encrypted hex Stringstatic byte[]
decryptBytes(String key, String hexStr)
Get unencrypted byte[] from encrypted hex Stringstatic String
encrypt(String key, String content)
Get hex String of content String encrypted.static String
encryptBytes(String key, byte[] content)
static String
fastMD5(File file)
Super-fast MD5 calculation from entire file.static MessageDigest
getDigest(String digest)
static MessageDigest
getMD5Digest()
static MessageDigest
getSHA1Digest()
static MessageDigest
getSHA256Digest()
static MessageDigest
getSHA512Digest()
-
-
-
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(FileChannel ch) 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[].
-
getSHA1Digest
public static MessageDigest getSHA1Digest()
-
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)
-
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
- SecretKeySpecmode
- 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.
-
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[].
-
-