From 10a45b9d8e175278d3b7bdc113016f99bf8eb840 Mon Sep 17 00:00:00 2001 From: Adam Retter Date: Thu, 22 Aug 2019 23:08:57 +0100 Subject: [PATCH] [feature] Update for the latest Crypto lib from Claudius --- pom.xml | 2 +- .../exist/crypto/EXpathCryptoException.java | 2 +- .../exist/crypto/ExpathCryptoErrorCode.java | 12 +----------- .../GenerateSignatureFunction.java | 4 ++-- .../crypto/encrypt/EncryptionFunctions.java | 16 ++++++---------- 5 files changed, 11 insertions(+), 25 deletions(-) diff --git a/pom.xml b/pom.xml index 621657f..5c3ad17 100644 --- a/pom.xml +++ b/pom.xml @@ -55,7 +55,7 @@ EXPath Cryptographic Module Implementation - 1.5 + 1.8.0-SNAPSHOT 5.0.0-RC8 http://expath.org/ns/crypto crypto diff --git a/src/main/java/org/expath/exist/crypto/EXpathCryptoException.java b/src/main/java/org/expath/exist/crypto/EXpathCryptoException.java index 3be3ab7..5b940b0 100644 --- a/src/main/java/org/expath/exist/crypto/EXpathCryptoException.java +++ b/src/main/java/org/expath/exist/crypto/EXpathCryptoException.java @@ -30,7 +30,7 @@ public class EXpathCryptoException extends XPathException { private static final long serialVersionUID = -6789727720893604433L; public EXpathCryptoException(Expression expr, CryptoError cryptoError) { - super(expr, new ExpathCryptoErrorCode(cryptoError), ExpathCryptoErrorCode.getDescription(cryptoError)); + super(expr, new ExpathCryptoErrorCode(cryptoError), cryptoError.getDescription()); } public EXpathCryptoException(Expression expr, Exception exception) { diff --git a/src/main/java/org/expath/exist/crypto/ExpathCryptoErrorCode.java b/src/main/java/org/expath/exist/crypto/ExpathCryptoErrorCode.java index a21fe0d..b16c2cb 100644 --- a/src/main/java/org/expath/exist/crypto/ExpathCryptoErrorCode.java +++ b/src/main/java/org/expath/exist/crypto/ExpathCryptoErrorCode.java @@ -13,16 +13,6 @@ public ExpathCryptoErrorCode(String code, String description) { } public ExpathCryptoErrorCode(CryptoError cryptoError) { - super(new QName(cryptoError.name(), ExistExpathCryptoModule.NAMESPACE_URI, ExistExpathCryptoModule.PREFIX), getDescription(cryptoError)); - } - - public static String getDescription(final CryptoError cryptoError) { - try { - final Field field = cryptoError.getClass().getDeclaredField("description"); - field.setAccessible(true); - return (String) field.get(cryptoError); - } catch (final NoSuchFieldException | IllegalAccessException e) { - return "UNKNOWN"; - } + super(new QName(cryptoError.getCode(), ExistExpathCryptoModule.NAMESPACE_URI, ExistExpathCryptoModule.PREFIX), cryptoError.getDescription()); } } diff --git a/src/main/java/org/expath/exist/crypto/digitalSignature/GenerateSignatureFunction.java b/src/main/java/org/expath/exist/crypto/digitalSignature/GenerateSignatureFunction.java index 261e227..7c6e0aa 100644 --- a/src/main/java/org/expath/exist/crypto/digitalSignature/GenerateSignatureFunction.java +++ b/src/main/java/org/expath/exist/crypto/digitalSignature/GenerateSignatureFunction.java @@ -258,11 +258,11 @@ private InputStream getKeyStoreInputStream(final String keystoreURI) throws Cryp } } catch (final PermissionDeniedException e) { - LOG.error(ExpathCryptoErrorCode.getDescription(CryptoError.DENIED_KEYSTORE)); + LOG.error(CryptoError.DENIED_KEYSTORE.getDescription()); return null; } } catch (final URISyntaxException e) { - LOG.error(ExpathCryptoErrorCode.getDescription(CryptoError.KEYSTORE_URL)); + LOG.error(CryptoError.KEYSTORE_URL.getDescription()); return null; } } diff --git a/src/main/java/org/expath/exist/crypto/encrypt/EncryptionFunctions.java b/src/main/java/org/expath/exist/crypto/encrypt/EncryptionFunctions.java index 872abaf..9097401 100644 --- a/src/main/java/org/expath/exist/crypto/encrypt/EncryptionFunctions.java +++ b/src/main/java/org/expath/exist/crypto/encrypt/EncryptionFunctions.java @@ -122,13 +122,11 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro private Sequence encrypt(byte[] data, CryptType encryptType, String secretKey, String algorithm, @Nullable String iv, @Nullable String provider) throws XPathException { try { - String resultBytes = null; + byte[] resultBytes = null; switch (encryptType) { case SYMMETRIC: - try (final FastByteArrayInputStream is = new FastByteArrayInputStream(data)) { - resultBytes = SymmetricEncryption.encrypt(is, secretKey, algorithm, iv, provider); - } + resultBytes = SymmetricEncryption.encrypt(data, secretKey, algorithm, iv, provider); break; case ASYMMETRIC: @@ -138,7 +136,7 @@ private Sequence encrypt(byte[] data, CryptType encryptType, String secretKey, S default: throw new EXpathCryptoException(this, CryptoError.ENCRYPTION_TYPE); } - String result = Base64.getEncoder().encodeToString(resultBytes.getBytes()); + String result = Base64.getEncoder().encodeToString(resultBytes); LOG.debug("encrypt result = {}", () -> result); return new StringValue(result); @@ -154,13 +152,11 @@ private Sequence encrypt(byte[] data, CryptType encryptType, String secretKey, S private Sequence decrypt(byte[] data, CryptType decryptType, String secretKey, String algorithm, @Nullable String iv, @Nullable String provider) throws XPathException { try { - String resultBytes = null; + byte[] resultBytes = null; switch (decryptType) { case SYMMETRIC: - try (final FastByteArrayInputStream is = new FastByteArrayInputStream(data)) { - resultBytes = SymmetricEncryption.decrypt(is, secretKey, algorithm, iv, provider); - } + resultBytes = SymmetricEncryption.decrypt(data, secretKey, algorithm, iv, provider); break; case ASYMMETRIC: @@ -172,7 +168,7 @@ private Sequence decrypt(byte[] data, CryptType decryptType, String secretKey, S throw new EXpathCryptoException(this, CryptoError.DECRYPTION_TYPE); } - String result = new String(resultBytes.getBytes(), UTF_8); + String result = new String(resultBytes, UTF_8); LOG.debug("decrypt result = {}", () -> result); return new StringValue(result);