Skip to content

Commit 10a45b9

Browse files
committed
[feature] Update for the latest Crypto lib from Claudius
1 parent 8d34e32 commit 10a45b9

File tree

5 files changed

+11
-25
lines changed

5 files changed

+11
-25
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555

5656
<properties>
5757
<package.title>EXPath Cryptographic Module Implementation</package.title>
58-
<crypto.java.lib.version>1.5</crypto.java.lib.version>
58+
<crypto.java.lib.version>1.8.0-SNAPSHOT</crypto.java.lib.version>
5959
<exist.version>5.0.0-RC8</exist.version>
6060
<crypto.module.ns>http://expath.org/ns/crypto</crypto.module.ns>
6161
<package-abbrev>crypto</package-abbrev>

src/main/java/org/expath/exist/crypto/EXpathCryptoException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class EXpathCryptoException extends XPathException {
3030
private static final long serialVersionUID = -6789727720893604433L;
3131

3232
public EXpathCryptoException(Expression expr, CryptoError cryptoError) {
33-
super(expr, new ExpathCryptoErrorCode(cryptoError), ExpathCryptoErrorCode.getDescription(cryptoError));
33+
super(expr, new ExpathCryptoErrorCode(cryptoError), cryptoError.getDescription());
3434
}
3535

3636
public EXpathCryptoException(Expression expr, Exception exception) {

src/main/java/org/expath/exist/crypto/ExpathCryptoErrorCode.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,6 @@ public ExpathCryptoErrorCode(String code, String description) {
1313
}
1414

1515
public ExpathCryptoErrorCode(CryptoError cryptoError) {
16-
super(new QName(cryptoError.name(), ExistExpathCryptoModule.NAMESPACE_URI, ExistExpathCryptoModule.PREFIX), getDescription(cryptoError));
17-
}
18-
19-
public static String getDescription(final CryptoError cryptoError) {
20-
try {
21-
final Field field = cryptoError.getClass().getDeclaredField("description");
22-
field.setAccessible(true);
23-
return (String) field.get(cryptoError);
24-
} catch (final NoSuchFieldException | IllegalAccessException e) {
25-
return "UNKNOWN";
26-
}
16+
super(new QName(cryptoError.getCode(), ExistExpathCryptoModule.NAMESPACE_URI, ExistExpathCryptoModule.PREFIX), cryptoError.getDescription());
2717
}
2818
}

src/main/java/org/expath/exist/crypto/digitalSignature/GenerateSignatureFunction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,11 @@ private InputStream getKeyStoreInputStream(final String keystoreURI) throws Cryp
258258
}
259259

260260
} catch (final PermissionDeniedException e) {
261-
LOG.error(ExpathCryptoErrorCode.getDescription(CryptoError.DENIED_KEYSTORE));
261+
LOG.error(CryptoError.DENIED_KEYSTORE.getDescription());
262262
return null;
263263
}
264264
} catch (final URISyntaxException e) {
265-
LOG.error(ExpathCryptoErrorCode.getDescription(CryptoError.KEYSTORE_URL));
265+
LOG.error(CryptoError.KEYSTORE_URL.getDescription());
266266
return null;
267267
}
268268
}

src/main/java/org/expath/exist/crypto/encrypt/EncryptionFunctions.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,11 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
122122
private Sequence encrypt(byte[] data, CryptType encryptType, String secretKey, String algorithm,
123123
@Nullable String iv, @Nullable String provider) throws XPathException {
124124
try {
125-
String resultBytes = null;
125+
byte[] resultBytes = null;
126126

127127
switch (encryptType) {
128128
case SYMMETRIC:
129-
try (final FastByteArrayInputStream is = new FastByteArrayInputStream(data)) {
130-
resultBytes = SymmetricEncryption.encrypt(is, secretKey, algorithm, iv, provider);
131-
}
129+
resultBytes = SymmetricEncryption.encrypt(data, secretKey, algorithm, iv, provider);
132130
break;
133131

134132
case ASYMMETRIC:
@@ -138,7 +136,7 @@ private Sequence encrypt(byte[] data, CryptType encryptType, String secretKey, S
138136
default:
139137
throw new EXpathCryptoException(this, CryptoError.ENCRYPTION_TYPE);
140138
}
141-
String result = Base64.getEncoder().encodeToString(resultBytes.getBytes());
139+
String result = Base64.getEncoder().encodeToString(resultBytes);
142140
LOG.debug("encrypt result = {}", () -> result);
143141

144142
return new StringValue(result);
@@ -154,13 +152,11 @@ private Sequence encrypt(byte[] data, CryptType encryptType, String secretKey, S
154152
private Sequence decrypt(byte[] data, CryptType decryptType, String secretKey, String algorithm,
155153
@Nullable String iv, @Nullable String provider) throws XPathException {
156154
try {
157-
String resultBytes = null;
155+
byte[] resultBytes = null;
158156

159157
switch (decryptType) {
160158
case SYMMETRIC:
161-
try (final FastByteArrayInputStream is = new FastByteArrayInputStream(data)) {
162-
resultBytes = SymmetricEncryption.decrypt(is, secretKey, algorithm, iv, provider);
163-
}
159+
resultBytes = SymmetricEncryption.decrypt(data, secretKey, algorithm, iv, provider);
164160
break;
165161

166162
case ASYMMETRIC:
@@ -172,7 +168,7 @@ private Sequence decrypt(byte[] data, CryptType decryptType, String secretKey, S
172168
throw new EXpathCryptoException(this, CryptoError.DECRYPTION_TYPE);
173169
}
174170

175-
String result = new String(resultBytes.getBytes(), UTF_8);
171+
String result = new String(resultBytes, UTF_8);
176172
LOG.debug("decrypt result = {}", () -> result);
177173

178174
return new StringValue(result);

0 commit comments

Comments
 (0)