Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 1 addition & 11 deletions src/main/java/org/expath/exist/crypto/ExpathCryptoErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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);
Expand All @@ -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:
Expand All @@ -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);
Expand Down