Class EncryptionUtilities
java.lang.Object
com.cedarsoftware.util.EncryptionUtilities
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
License
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
Modifier and TypeMethodDescriptionstatic String
static String
calculateHash
(MessageDigest d, byte[] bytes) Calculate a hash String from the passed in byte[].static String
calculateMD5Hash
(byte[] bytes) Calculate an MD5 Hash String from the passed in byte[].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
static Cipher
static byte[]
createCipherBytes
(String key, int bitsNeeded) static String
Get unencrypted String from encrypted hex Stringstatic byte[]
decryptBytes
(String key, String hexStr) Get unencrypted byte[] from encrypted hex Stringstatic String
Get hex String of content String encrypted.static String
encryptBytes
(String key, byte[] content) static String
Super-fast MD5 calculation from entire file.static String
Super-fast SHA-1 calculation from entire file.static String
fastSHA256
(File file) Super-fast SHA-256 calculation from entire file.static String
fastSHA512
(File file) Super-fast SHA-512 calculation from entire file.static MessageDigest
static MessageDigest
static MessageDigest
static MessageDigest
static MessageDigest
-
Method Details
-
fastMD5
-
fastSHA1
-
fastSHA256
-
fastSHA512
-
calculateFileHash
- Throws:
IOException
-
calculateMD5Hash
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
-
getMD5Digest
-
calculateSHA1Hash
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
-
calculateSHA256Hash
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
-
calculateSHA512Hash
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
-
createCipherBytes
-
createAesEncryptionCipher
-
createAesDecryptionCipher
-
createAesCipher
-
createAesCipher
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
- if the requested Cipher instance does not exist.
-
encrypt
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
-
decrypt
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
Get unencrypted byte[] from encrypted hex String- Parameters:
key
- String encryption/decryption keyhexStr
- 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
Calculate a hash String from the passed in byte[].- Parameters:
d
- MessageDigest to update with the passed in bytesbytes
- 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.
-