public class BcryptCipherProvider extends RandomIVPBECipherProvider
Modifier and Type | Field and Description |
---|---|
private static Pattern |
BCRYPT_SALT_FORMAT |
private static String |
BCRYPT_SALT_FORMAT_MSG |
private static int |
DEFAULT_SALT_LENGTH |
private static int |
DEFAULT_WORK_FACTOR
This can be calculated automatically using the code
BcryptCipherProviderGroovyTest#calculateMinimumWorkFactor or manually updated by a maintainer |
private static org.slf4j.Logger |
logger |
private int |
workFactor |
IV_DELIMITER, MAX_IV_LIMIT, MAX_SALT_LIMIT, SALT_DELIMITER
Constructor and Description |
---|
BcryptCipherProvider()
Instantiates a Bcrypt cipher provider with the default work factor 12 (2^12 key expansion rounds).
|
BcryptCipherProvider(int workFactor)
Instantiates a Bcrypt cipher provider with the specified work factor w (2^w key expansion rounds).
|
Modifier and Type | Method and Description |
---|---|
private Cipher |
createCipherAndHandleExceptions(EncryptionMethod encryptionMethod,
String password,
byte[] salt,
byte[] iv,
int keyLength,
boolean encryptMode,
boolean useLegacyKeyDerivation) |
private SecretKey |
deriveKey(String password,
int keyLength,
String algorithm,
String provider,
byte[] rawSalt,
int workFactor,
boolean useLegacyKeyDerivation) |
static byte[] |
extractRawSalt(String fullSalt)
Returns the raw salt as a
byte[] extracted from the Bcrypt formatted salt byte[]. |
static String |
formatSaltForBcrypt(byte[] salt,
int workFactor) |
byte[] |
generateSalt()
Returns the full salt in a
byte[] for this cipher provider (i.e. |
Cipher |
getCipher(EncryptionMethod encryptionMethod,
String password,
byte[] salt,
byte[] iv,
int keyLength,
boolean encryptMode)
Returns an initialized cipher for the specified algorithm.
|
Cipher |
getCipher(EncryptionMethod encryptionMethod,
String password,
byte[] salt,
int keyLength,
boolean encryptMode)
Returns an initialized cipher for the specified algorithm.
|
int |
getDefaultSaltLength()
Returns the default salt length for this implementation.
|
protected Cipher |
getInitializedCipher(EncryptionMethod encryptionMethod,
String password,
byte[] salt,
byte[] iv,
int keyLength,
boolean encryptMode,
boolean useLegacyKeyDerivation) |
Cipher |
getLegacyDecryptCipher(EncryptionMethod encryptionMethod,
String password,
byte[] salt,
byte[] iv,
int keyLength)
Returns a
Cipher instance in Cipher.DECRYPT_MODE configured with the provided inputs and using the
legacy key derivation process for Bcrypt where the complete Bcrypt hash output (including algorithm, work
factor, and salt) was used as the input to the key stretching SHA-512 digest function. |
(package private) org.slf4j.Logger |
getLogger() |
protected int |
getWorkFactor() |
static boolean |
isBcryptFormattedSalt(String salt)
Returns
true if the salt string is a valid Bcrypt salt string ($2a$10$abcdefghi..{22} ). |
private int |
parseSalt(String bcryptSalt,
byte[] rawSalt) |
readIV, readSalt, writeIV, writeSalt
private static final org.slf4j.Logger logger
private final int workFactor
private static final int DEFAULT_WORK_FACTOR
BcryptCipherProviderGroovyTest#calculateMinimumWorkFactor
or manually updated by a maintainerprivate static final int DEFAULT_SALT_LENGTH
private static final Pattern BCRYPT_SALT_FORMAT
private static final String BCRYPT_SALT_FORMAT_MSG
public BcryptCipherProvider()
public BcryptCipherProvider(int workFactor)
workFactor
- the (log) number of key expansion rounds [4..30]org.slf4j.Logger getLogger()
getLogger
in class RandomIVPBECipherProvider
public Cipher getCipher(EncryptionMethod encryptionMethod, String password, byte[] salt, byte[] iv, int keyLength, boolean encryptMode) throws Exception
getCipher
in class RandomIVPBECipherProvider
encryptionMethod
- the EncryptionMethod
password
- the secret inputsalt
- the complete salt (e.g. "$2a$10$gUVbkVzp79H8YaCOsCVZNu".getBytes(StandardCharsets.UTF_8)
)iv
- the IVkeyLength
- the desired key length in bitsencryptMode
- true for encrypt, false for decryptException
- if there is a problem initializing the cipherprivate Cipher createCipherAndHandleExceptions(EncryptionMethod encryptionMethod, String password, byte[] salt, byte[] iv, int keyLength, boolean encryptMode, boolean useLegacyKeyDerivation)
public Cipher getCipher(EncryptionMethod encryptionMethod, String password, byte[] salt, int keyLength, boolean encryptMode) throws Exception
The IV can be retrieved by the calling method using Cipher.getIV()
.
encryptionMethod
- the EncryptionMethod
password
- the secret inputsalt
- the complete salt (e.g. "$2a$10$gUVbkVzp79H8YaCOsCVZNu".getBytes(StandardCharsets.UTF_8)
)keyLength
- the desired key length in bitsencryptMode
- true for encrypt, false for decryptException
- if there is a problem initializing the cipherpublic Cipher getLegacyDecryptCipher(EncryptionMethod encryptionMethod, String password, byte[] salt, byte[] iv, int keyLength)
Cipher
instance in Cipher.DECRYPT_MODE
configured with the provided inputs and using the
legacy key derivation process for Bcrypt
where the complete Bcrypt hash output (including algorithm, work
factor, and salt) was used as the input to the key stretching SHA-512 digest function. This is only used for
backward-compatibility decryptions for NiFi versions prior to 1.12.0. All encryption operations moving forward use the
correct key derivation process.encryptionMethod
- the EncryptionMethod
password
- the secret inputsalt
- the complete salt (e.g. "$2a$10$gUVbkVzp79H8YaCOsCVZNu".getBytes(StandardCharsets.UTF_8)
)iv
- the Initialization Vector in bitskeyLength
- the desired key length in bitsException
- if there is a problem initializing the cipherprotected Cipher getInitializedCipher(EncryptionMethod encryptionMethod, String password, byte[] salt, byte[] iv, int keyLength, boolean encryptMode, boolean useLegacyKeyDerivation) throws Exception
Exception
private SecretKey deriveKey(String password, int keyLength, String algorithm, String provider, byte[] rawSalt, int workFactor, boolean useLegacyKeyDerivation) throws NoSuchAlgorithmException, NoSuchProviderException
public static boolean isBcryptFormattedSalt(String salt)
true
if the salt string is a valid Bcrypt salt string ($2a$10$abcdefghi..{22}
).salt
- the salt string to evaluateprivate int parseSalt(String bcryptSalt, byte[] rawSalt)
public static String formatSaltForBcrypt(byte[] salt, int workFactor)
public byte[] generateSalt()
byte[]
for this cipher provider (i.e. $2a$10$abcdef...
format).PBECipherProvider.getDefaultSaltLength()
public static byte[] extractRawSalt(String fullSalt)
byte[]
extracted from the Bcrypt formatted salt byte[].fullSalt
- the Bcrypt salt sequence as bytespublic int getDefaultSaltLength()
PBECipherProvider
protected int getWorkFactor()
Copyright © 2021 Apache NiFi Project. All rights reserved.