From 1f055c9dedd3f3598d31d9cef225dc05ba31c4d9 Mon Sep 17 00:00:00 2001 From: Joe Ellis Date: Wed, 5 Feb 2020 17:43:58 +0000 Subject: [PATCH 1/2] Fix develop build --- .../palantir/crypto2/io/CryptoStreamFactory.java | 2 +- .../palantir/crypto2/cipher/AesCtrCipherTest.java | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/crypto-core/src/main/java/com/palantir/crypto2/io/CryptoStreamFactory.java b/crypto-core/src/main/java/com/palantir/crypto2/io/CryptoStreamFactory.java index 4b359d66e..fd48e7d1a 100644 --- a/crypto-core/src/main/java/com/palantir/crypto2/io/CryptoStreamFactory.java +++ b/crypto-core/src/main/java/com/palantir/crypto2/io/CryptoStreamFactory.java @@ -100,7 +100,7 @@ static OutputStream encrypt(OutputStream output, KeyMaterial keyMaterial, String /** To avoid spamming logs with exceptions, we only log the exception once. */ private static void warningLog(IOException exception) { - String message = "Unable to initialize cipher with OpenSSL, falling back to JCE implementation " + final String message = "Unable to initialize cipher with OpenSSL, falling back to JCE implementation " + "- see github.com/palantir/hadoop-crypto"; if (fullExceptionLoggedAlready) { diff --git a/crypto-core/src/test/java/com/palantir/crypto2/cipher/AesCtrCipherTest.java b/crypto-core/src/test/java/com/palantir/crypto2/cipher/AesCtrCipherTest.java index 77291d8f0..3d8d98871 100644 --- a/crypto-core/src/test/java/com/palantir/crypto2/cipher/AesCtrCipherTest.java +++ b/crypto-core/src/test/java/com/palantir/crypto2/cipher/AesCtrCipherTest.java @@ -18,6 +18,7 @@ import static org.assertj.core.api.Assertions.assertThat; +import com.google.common.io.BaseEncoding; import com.palantir.crypto2.keys.KeyMaterial; import java.util.Arrays; import java.util.LinkedHashMap; @@ -26,7 +27,6 @@ import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.spec.SecretKeySpec; -import javax.xml.bind.DatatypeConverter; import org.junit.Test; public final class AesCtrCipherTest extends AbstractSeekableCipherTest { @@ -73,10 +73,10 @@ public void testNistDecrypt() { } public void testNistExample(int opmode, int blockNumber, String input, String output) { - byte[] key = DatatypeConverter.parseHexBinary(KEY); - byte[] iv = DatatypeConverter.parseHexBinary(IV); - byte[] inputBytes = DatatypeConverter.parseHexBinary(input); - byte[] outputBytes = DatatypeConverter.parseHexBinary(output); + byte[] key = hexToBinary(KEY); + byte[] iv = hexToBinary(IV); + byte[] inputBytes = hexToBinary(input); + byte[] outputBytes = hexToBinary(output); KeyMaterial keyMaterial = KeyMaterial.of(new SecretKeySpec(key, AesCtrCipher.KEY_ALGORITHM), iv); SeekableCipher seekableCipher = getCipher(keyMaterial); @@ -133,4 +133,7 @@ public void testIvUnderflow() { cipher.seek(100); } + private static byte[] hexToBinary(String hex) { + return BaseEncoding.base16().lowerCase().decode(hex); + } } From 83aed821a33e803f4ce82e7f74fdf1ed8f72ace6 Mon Sep 17 00:00:00 2001 From: Joe Ellis Date: Wed, 5 Feb 2020 17:45:06 +0000 Subject: [PATCH 2/2] static --- .../com/palantir/crypto2/io/CryptoStreamFactory.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/crypto-core/src/main/java/com/palantir/crypto2/io/CryptoStreamFactory.java b/crypto-core/src/main/java/com/palantir/crypto2/io/CryptoStreamFactory.java index fd48e7d1a..04c174277 100644 --- a/crypto-core/src/main/java/com/palantir/crypto2/io/CryptoStreamFactory.java +++ b/crypto-core/src/main/java/com/palantir/crypto2/io/CryptoStreamFactory.java @@ -38,6 +38,8 @@ public final class CryptoStreamFactory { private static final Logger log = LoggerFactory.getLogger(CryptoStreamFactory.class); private static final Properties PROPS = ApacheCiphers.forceOpenSsl(new Properties()); private static final String AES_ALGORITHM = "AES/CTR/NoPadding"; + private static final String OPEN_SSL_INIT_WARNING = "Unable to initialize cipher with OpenSSL, falling back to " + + "JCE implementation - see github.com/palantir/hadoop-crypto"; private static volatile boolean fullExceptionLoggedAlready = false; @@ -100,13 +102,10 @@ static OutputStream encrypt(OutputStream output, KeyMaterial keyMaterial, String /** To avoid spamming logs with exceptions, we only log the exception once. */ private static void warningLog(IOException exception) { - final String message = "Unable to initialize cipher with OpenSSL, falling back to JCE implementation " - + "- see github.com/palantir/hadoop-crypto"; - if (fullExceptionLoggedAlready) { - log.warn(message); + log.warn(OPEN_SSL_INIT_WARNING); } else { - log.warn(message, exception); + log.warn(OPEN_SSL_INIT_WARNING, exception); fullExceptionLoggedAlready = true; } }