mirror of https://github.com/apache/nifi.git
NIFI-13599 Replaced Commons Codec Hex with HexFormat for Encryptor (#9125)
- Removed commons-codec dependency from nifi-property-encryptor
This commit is contained in:
parent
8067d5155e
commit
dfa015ec33
|
@ -31,9 +31,5 @@
|
|||
<artifactId>nifi-security-crypto-key</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -16,14 +16,12 @@
|
|||
*/
|
||||
package org.apache.nifi.encrypt;
|
||||
|
||||
import org.apache.commons.codec.DecoderException;
|
||||
import org.apache.commons.codec.binary.Hex;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HexFormat;
|
||||
|
||||
/**
|
||||
* Cipher Property Encryptor provides hexadecimal encoding and decoding around cipher operations
|
||||
|
@ -31,6 +29,8 @@ import java.nio.charset.StandardCharsets;
|
|||
abstract class CipherPropertyEncryptor implements PropertyEncryptor {
|
||||
private static final Charset PROPERTY_CHARSET = StandardCharsets.UTF_8;
|
||||
|
||||
private static final HexFormat HEX_FORMAT = HexFormat.of();
|
||||
|
||||
/**
|
||||
* Encrypt property and encode as a hexadecimal string
|
||||
*
|
||||
|
@ -45,7 +45,7 @@ abstract class CipherPropertyEncryptor implements PropertyEncryptor {
|
|||
final Cipher cipher = getEncryptionCipher(encodedParameters);
|
||||
try {
|
||||
final byte[] encrypted = cipher.doFinal(binary);
|
||||
return Hex.encodeHexString(getConcatenatedBinary(encodedParameters, encrypted));
|
||||
return HEX_FORMAT.formatHex(getConcatenatedBinary(encodedParameters, encrypted));
|
||||
} catch (final BadPaddingException | IllegalBlockSizeException e) {
|
||||
final String message = String.format("Encryption Failed with Algorithm [%s]", cipher.getAlgorithm());
|
||||
throw new EncryptionException(message, e);
|
||||
|
@ -74,8 +74,8 @@ abstract class CipherPropertyEncryptor implements PropertyEncryptor {
|
|||
|
||||
private byte[] getDecodedBinary(final String encryptedProperty) {
|
||||
try {
|
||||
return Hex.decodeHex(encryptedProperty);
|
||||
} catch (final DecoderException e) {
|
||||
return HEX_FORMAT.parseHex(encryptedProperty);
|
||||
} catch (final IllegalArgumentException e) {
|
||||
throw new EncryptionException("Hexadecimal decoding failed", e);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,8 +16,6 @@
|
|||
*/
|
||||
package org.apache.nifi.encrypt;
|
||||
|
||||
import org.apache.commons.codec.DecoderException;
|
||||
import org.apache.commons.codec.binary.Hex;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -25,8 +23,10 @@ import javax.crypto.SecretKey;
|
|||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HexFormat;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
|
@ -60,18 +60,25 @@ public class KeyedCipherPropertyEncryptorTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testEncryptHexadecimalEncoded() throws DecoderException {
|
||||
public void testEncryptHexadecimalEncoded() {
|
||||
final String encrypted = encryptor.encrypt(PROPERTY);
|
||||
final byte[] decoded = Hex.decodeHex(encrypted);
|
||||
final byte[] decoded = HexFormat.of().parseHex(encrypted);
|
||||
assertEquals(ENCRYPTED_BINARY_LENGTH, decoded.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecryptEncryptionException() {
|
||||
final String encodedProperty = Hex.encodeHexString(PROPERTY.getBytes(DEFAULT_CHARSET));
|
||||
final String encodedProperty = HexFormat.of().formatHex(PROPERTY.getBytes(DEFAULT_CHARSET));
|
||||
assertThrows(Exception.class, () -> encryptor.decrypt(encodedProperty));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecryptHexadecimalInvalid() {
|
||||
final String invalidProperty = String.class.getName();
|
||||
final EncryptionException exception = assertThrows(EncryptionException.class, () -> encryptor.decrypt(invalidProperty));
|
||||
assertInstanceOf(IllegalArgumentException.class, exception.getCause());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCipherEncryptionException() {
|
||||
encryptor = new KeyedCipherPropertyEncryptor(INVALID_SECRET_KEY);
|
||||
|
|
Loading…
Reference in New Issue