public class CipherUtility extends Object
Modifier and Type | Field and Description |
---|---|
static int |
BUFFER_SIZE |
private static int |
DEFAULT_MAX_ALLOWED_KEY_LENGTH |
private static Pattern |
KEY_LENGTH_PATTERN |
private static Map<String,Integer> |
MAX_PASSWORD_LENGTH_BY_ALGORITHM |
Constructor and Description |
---|
CipherUtility() |
Modifier and Type | Method and Description |
---|---|
static int |
calculateCipherTextLength(int ptLength,
int saltLength)
Returns the calculated cipher text length given the plaintext length and salt length, if any.
|
static String |
encodeBase64NoPadding(byte[] bytes) |
static byte[] |
extractRawSalt(byte[] fullSalt,
KeyDerivationFunction kdf)
Returns the raw salt from the provided "full salt" which could be KDF-specific.
|
static int |
findSequence(byte[] haystack,
byte[] needle)
Returns the array index of
haystack if needle is found within it. |
private static int |
getDefaultKeyLengthForCipher(String cipher) |
static int |
getMaximumPasswordLengthForAlgorithmOnLimitedStrengthCrypto(EncryptionMethod encryptionMethod) |
static int |
getSaltLengthForAlgorithm(String algorithm)
Returns the salt length for various PBE algorithms.
|
static String |
getTimestampString()
Returns the current timestamp in a default format.
|
static List<Integer> |
getValidKeyLengthsForAlgorithm(String algorithm)
Returns a list of valid key lengths in bits for this algorithm.
|
static boolean |
isUnlimitedStrengthCryptoSupported() |
static boolean |
isValidKeyLength(int keyLength,
String cipher)
Returns true if the provided key length is a valid key length for the provided cipher family.
|
static boolean |
isValidKeyLengthForAlgorithm(int keyLength,
String algorithm)
Returns true if the provided key length is a valid key length for the provided algorithm.
|
private static int |
parseActualKeyLengthFromAlgorithm(String algorithm) |
static String |
parseCipherFromAlgorithm(String algorithm)
Returns the cipher algorithm from the full algorithm name.
|
static int |
parseKeyLengthFromAlgorithm(String algorithm)
Returns the cipher key length from the full algorithm name.
|
static boolean |
passwordLengthIsValidForAlgorithmOnLimitedStrengthCrypto(int passwordLength,
EncryptionMethod encryptionMethod) |
static void |
processStreams(Cipher cipher,
InputStream in,
OutputStream out) |
static byte[] |
readBytesFromInputStream(InputStream in,
String label,
int limit,
byte[] delimiter) |
static ByteCountingInputStream |
wrapStreamForCounting(InputStream inputStream) |
static ByteCountingOutputStream |
wrapStreamForCounting(OutputStream outputStream) |
static void |
writeBytesToOutputStream(OutputStream out,
byte[] value,
String label,
byte[] delimiter) |
public static final int BUFFER_SIZE
private static final Pattern KEY_LENGTH_PATTERN
private static final Map<String,Integer> MAX_PASSWORD_LENGTH_BY_ALGORITHM
private static final int DEFAULT_MAX_ALLOWED_KEY_LENGTH
public static String parseCipherFromAlgorithm(String algorithm)
algorithm
- the full algorithm namepublic static int parseKeyLengthFromAlgorithm(String algorithm)
algorithm
- the full algorithm nameprivate static int parseActualKeyLengthFromAlgorithm(String algorithm)
public static boolean isValidKeyLength(int keyLength, String cipher)
AES/CBC/PKCS7Padding
but not PBEWITHMD5AND128BITAES-CBC-OPENSSL
. However, this method will return true
for both because it only gets the cipher
family, AES
.
64, AES -> false
[128, 192, 256], AES -> truekeyLength
- the key length in bitscipher
- the cipher familypublic static boolean isValidKeyLengthForAlgorithm(int keyLength, String algorithm)
AES/CBC/PKCS7Padding
but not PBEWITHMD5AND128BITAES-CBC-OPENSSL
.
64, AES/CBC/PKCS7Padding -> false
[128, 192, 256], AES/CBC/PKCS7Padding -> true
128, PBEWITHMD5AND128BITAES-CBC-OPENSSL -> true
[192, 256], PBEWITHMD5AND128BITAES-CBC-OPENSSL -> falsekeyLength
- the key length in bitsalgorithm
- the specific algorithmpublic static List<Integer> getValidKeyLengthsForAlgorithm(String algorithm)
algorithm
- the name of the algorithmprivate static int getDefaultKeyLengthForCipher(String cipher)
public static void processStreams(Cipher cipher, InputStream in, OutputStream out)
public static byte[] readBytesFromInputStream(InputStream in, String label, int limit, byte[] delimiter) throws IOException, ProcessException
IOException
ProcessException
public static void writeBytesToOutputStream(OutputStream out, byte[] value, String label, byte[] delimiter) throws IOException
IOException
public static String encodeBase64NoPadding(byte[] bytes)
public static boolean passwordLengthIsValidForAlgorithmOnLimitedStrengthCrypto(int passwordLength, EncryptionMethod encryptionMethod)
public static int getMaximumPasswordLengthForAlgorithmOnLimitedStrengthCrypto(EncryptionMethod encryptionMethod)
public static boolean isUnlimitedStrengthCryptoSupported()
public static int getSaltLengthForAlgorithm(String algorithm)
algorithm
- the EncryptionMethod.getAlgorithm()
public static String getTimestampString()
public static ByteCountingInputStream wrapStreamForCounting(InputStream inputStream)
public static ByteCountingOutputStream wrapStreamForCounting(OutputStream outputStream)
public static int calculateCipherTextLength(int ptLength, int saltLength)
8
) is included as well.ptLength
- the plaintext lengthsaltLength
- the salt lengthpublic static int findSequence(byte[] haystack, byte[] needle)
haystack
if needle
is found within it. This is a sequence scanner.haystack
- the search space byte[]needle
- the sequence to findpublic static byte[] extractRawSalt(byte[] fullSalt, KeyDerivationFunction kdf)
$argon2id$v=19$m=4096,t=3,p=1$abcdefABCDEF0123456789
Bcrypt -> $2a$10$abcdefABCDEF0123456789
Scrypt -> $s0$e0801$abcdefABCDEF0123456789
If the KDF does not have a custom encoding for the salt, the provided "full salt" is returned intact.fullSalt
- the KDF-formatted saltkdf
- the KDF usedCopyright © 2022 Apache NiFi Project. All rights reserved.