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 StringcalculateHash(MessageDigest d, byte[] bytes)Calculate an SHA-256 Hash String from the passed in byte[].static StringcalculateMD5Hash(byte[] bytes)Calculate an MD5 Hash String from the passed in byte[].static StringcalculateMD5Hash(FileChannel ch)static StringcalculateSHA1Hash(byte[] bytes)Calculate an MD5 Hash String from the passed in byte[].static StringcalculateSHA256Hash(byte[] bytes)Calculate an SHA-256 Hash String from the passed in byte[].static StringcalculateSHA512Hash(byte[] bytes)Calculate an SHA-512 Hash String from the passed in byte[].static CiphercreateAesCipher(String key, int mode)static CiphercreateAesCipher(Key key, int mode)Creates a Cipher from the passed in key, using the passed in mode.static CiphercreateAesDecryptionCipher(String key)static CiphercreateAesEncryptionCipher(String key)static byte[]createCipherBytes(String key, int bitsNeeded)static Stringdecrypt(String key, String hexStr)Get unencrypted String from encrypted hex Stringstatic byte[]decryptBytes(String key, String hexStr)Get unencrypted byte[] from encrypted hex Stringstatic Stringencrypt(String key, String content)Get hex String of content String encrypted.static StringencryptBytes(String key, byte[] content)static StringfastMD5(File file)Super-fast MD5 calculation from entire file.static MessageDigestgetDigest(String digest)static MessageDigestgetMD5Digest()static MessageDigestgetSHA1Digest()static MessageDigestgetSHA256Digest()static MessageDigestgetSHA512Digest()
-
-
-
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[].
-
-