public class StringEncryptor extends Object
An application specific string encryptor that collects configuration from the application properties, system properties, and/or system environment.
Instance of this class are thread-safe
The encryption provider and algorithm is configured using the application properties:
The encryptor's password may be set by configuring the below property:
| Modifier and Type | Field and Description |
|---|---|
private String |
algorithm |
private static String |
ARGON2_AES_GCM_128_ALGORITHM |
private static String |
ARGON2_AES_GCM_256_ALGORITHM |
private static String |
B64_ENCODING |
private org.apache.nifi.security.util.crypto.CipherProvider |
cipherProvider |
static int |
CUSTOM_ALGORITHM_SALT_LENGTH |
private static List<String> |
CUSTOM_ALGORITHMS |
private static String |
DEFAULT_SENSITIVE_PROPS_KEY |
private String |
encoding |
private static String |
HEX_ENCODING |
private static int |
IV_LENGTH |
private SecretKeySpec |
key |
private static org.slf4j.Logger |
logger |
static String |
NF_SENSITIVE_PROPS_ALGORITHM |
static String |
NF_SENSITIVE_PROPS_KEY |
static String |
NF_SENSITIVE_PROPS_PROVIDER |
private PBEKeySpec |
password |
private String |
provider |
private static List<String> |
SUPPORTED_ALGORITHMS |
private static List<String> |
SUPPORTED_PROVIDERS |
| Modifier | Constructor and Description |
|---|---|
protected |
StringEncryptor()
A default constructor for mocking during testing.
|
|
StringEncryptor(String algorithm,
String provider,
byte[] key)
This constructor creates an encryptor using Keyed Encryption.
|
|
StringEncryptor(String algorithm,
String provider,
String key)
This constructor creates an encryptor using Password-Based Encryption (PBE).
|
| Modifier and Type | Method and Description |
|---|---|
protected static boolean |
algorithmIsValid(String algorithm) |
private static String |
centerString(String msg) |
static StringEncryptor |
createEncryptor(NiFiProperties niFiProperties)
Deprecated.
as of NiFi 1.4.0 because the entire
NiFiProperties object is not necessary to generate the encryptor. |
static StringEncryptor |
createEncryptor(String algorithm,
String provider,
String password)
Creates an instance of the NiFi sensitive property encryptor.
|
private boolean |
customSecretIsValid(PBEKeySpec password,
SecretKeySpec key,
String algorithm) |
private byte[] |
decode(String encoded) |
String |
decrypt(String cipherText)
Decrypts the given cipher text.
|
private byte[] |
decryptKeyed(byte[] cipherBytes) |
private byte[] |
decryptPBE(byte[] cipherBytes) |
private static int |
determineSaltLength(String algorithm) |
private String |
encode(byte[] rawBytes) |
String |
encrypt(String clearText)
Encrypts the given clear text.
|
private byte[] |
encryptKeyed(String plaintext) |
private byte[] |
encryptPBE(String plaintext) |
boolean |
equals(Object o)
Returns
true if the two StringEncryptor objects are logically equivalent. |
private String |
extractKeyTypeFromAlgorithm(String algorithm)
Extracts the cipher "family" (i.e.
|
private EncryptionMethod |
getEncryptionMethodForAlgorithm(String algorithm) |
int |
hashCode()
Returns the hashcode of this object.
|
protected void |
initialize() |
static boolean |
isCustomAlgorithm(String algorithm)
Returns
true if the provided algorithm is considered a "custom" algorithm (a combination of KDF
and cipher not present in EncryptionMethod and implemented specially for string encryption). |
boolean |
isInitialized() |
private static boolean |
isPBEKeySpecEqual(PBEKeySpec a,
PBEKeySpec b)
Returns true if the two
PBEKeySpec objects are logically equivalent (same params and password). |
private boolean |
keyIsValid(SecretKeySpec key,
String algorithm) |
private boolean |
paramsAreValid() |
private boolean |
passwordIsValid(PBEKeySpec password) |
private static void |
printBlankKeyWarning() |
protected static boolean |
providerIsValid(String provider) |
private boolean |
secretsAreEqual(PBEKeySpec otherPassword,
SecretKeySpec otherKey)
Returns true if the provided
password and key match those contained in this StringEncryptor. |
void |
setEncoding(String base) |
String |
toString()
Returns a String containing the
algorithm, provider, encoding, and cipherProvider class name. |
private static final org.slf4j.Logger logger
private static final String ARGON2_AES_GCM_256_ALGORITHM
private static final String ARGON2_AES_GCM_128_ALGORITHM
public static final int CUSTOM_ALGORITHM_SALT_LENGTH
private static final int IV_LENGTH
private final String algorithm
private final String provider
private final PBEKeySpec password
private SecretKeySpec key
private static final String HEX_ENCODING
private static final String B64_ENCODING
private String encoding
private org.apache.nifi.security.util.crypto.CipherProvider cipherProvider
public static final String NF_SENSITIVE_PROPS_KEY
public static final String NF_SENSITIVE_PROPS_ALGORITHM
public static final String NF_SENSITIVE_PROPS_PROVIDER
private static final String DEFAULT_SENSITIVE_PROPS_KEY
public StringEncryptor(String algorithm, String provider, String key)
nifi.sensitive.props.key in
nifi.properties, which is a PASSWORD rather than a KEY, but is named such for backward/legacy logical compatibility throughout the rest of the codebase.
For actual raw key provision, see StringEncryptor(String, String, byte[]).
algorithm - the PBE cipher algorithm (EncryptionMethod.getAlgorithm())provider - the JCA Security provider (EncryptionMethod.getProvider())key - the UTF-8 characters from nifi.properties -- nifi.sensitive.props.keypublic StringEncryptor(String algorithm, String provider, byte[] key)
algorithm - the PBE cipher algorithm (EncryptionMethod.getAlgorithm())provider - the JCA Security provider (EncryptionMethod.getProvider())key - a raw encryption key in bytesprotected StringEncryptor()
private String extractKeyTypeFromAlgorithm(String algorithm) throws EncryptionException
algorithm - the algorithm (EncryptionMethod.getAlgorithm())EncryptionException - if the algorithm is null/empty or not supported@Deprecated public static StringEncryptor createEncryptor(NiFiProperties niFiProperties) throws EncryptionException
NiFiProperties object is not necessary to generate the encryptor.niFiProperties - propertiesEncryptionException - if any issues arise initializing or
validating the encryptorcreateEncryptor(String, String, String)public static StringEncryptor createEncryptor(String algorithm, String provider, String password)
algorithm - the encryption (and key derivation) algorithm (EncryptionMethod.getAlgorithm())provider - the JCA Security provider (EncryptionMethod.getProvider())password - the UTF-8 characters from nifi.properties -- nifi.sensitive.props.keyprivate static void printBlankKeyWarning()
protected void initialize()
public static boolean isCustomAlgorithm(String algorithm)
true if the provided algorithm is considered a "custom" algorithm (a combination of KDF
and cipher not present in EncryptionMethod and implemented specially for string encryption). Case-insensitive.algorithm - the algorithm to evaluateCUSTOM_ALGORITHMSprivate boolean paramsAreValid()
private boolean customSecretIsValid(PBEKeySpec password, SecretKeySpec key, String algorithm)
private boolean keyIsValid(SecretKeySpec key, String algorithm)
private boolean passwordIsValid(PBEKeySpec password)
public void setEncoding(String base)
public String encrypt(String clearText) throws EncryptionException
clearText - the message to encryptEncryptionException - if the encrypt failsprivate byte[] encryptPBE(String plaintext)
private EncryptionMethod getEncryptionMethodForAlgorithm(String algorithm)
private byte[] encryptKeyed(String plaintext)
private String encode(byte[] rawBytes)
public String decrypt(String cipherText) throws EncryptionException
cipherText - the message to decryptEncryptionException - if the decrypt failsprivate byte[] decryptPBE(byte[] cipherBytes)
private static int determineSaltLength(String algorithm)
private byte[] decryptKeyed(byte[] cipherBytes)
private byte[] decode(String encoded) throws org.apache.commons.codec.DecoderException
org.apache.commons.codec.DecoderExceptionpublic boolean isInitialized()
protected static boolean algorithmIsValid(String algorithm)
protected static boolean providerIsValid(String provider)
public boolean equals(Object o)
true if the two StringEncryptor objects are logically equivalent.
This requires the same algorithm, provider, encoding, and
key/password.
A ciphertext generated by one object can be decrypted by a separate object if they are equal as determined by this method.
private boolean secretsAreEqual(PBEKeySpec otherPassword, SecretKeySpec otherKey)
password and key match those contained in this StringEncryptor. This method does not compare password == key.
Internally, uses isPBEKeySpecEqual(PBEKeySpec, PBEKeySpec) and SecretKeySpec.equals(Object).
otherPassword - the password PBEKeySpecotherKey - the key SecretKeySpecprivate static boolean isPBEKeySpecEqual(PBEKeySpec a, PBEKeySpec b)
PBEKeySpec objects are logically equivalent (same params and password).a - a PBEKeySpec to compareb - a PBEKeySpec to comparepublic int hashCode()
cipherProvider in hashcode calculations.Copyright © 2021 Apache NiFi Project. All rights reserved.