diff --git a/src/main/java/ro/kuberam/libs/java/crypto/CryptoError.java b/src/main/java/ro/kuberam/libs/java/crypto/CryptoError.java index 70aa93a..2df67a5 100755 --- a/src/main/java/ro/kuberam/libs/java/crypto/CryptoError.java +++ b/src/main/java/ro/kuberam/libs/java/crypto/CryptoError.java @@ -19,33 +19,49 @@ */ package ro.kuberam.libs.java.crypto; +import javax.annotation.Nullable; +import javax.crypto.BadPaddingException; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; +import java.security.InvalidKeyException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.spec.InvalidKeySpecException; + public enum CryptoError { - NoSuchAlgorithmException("crypto:unknown-algorithm", "The specified algorithm is not supported."), + UNKNOWN_ALGORITHM("crypto:unknown-algorithm", "The specified algorithm is not supported.", NoSuchAlgorithmException.class), + UNKNOWN_PROVIDER("crypto:unknown-provider", "The specified provider is not available."), SIGNATURE_TYPE("crypto:signature-type", "The specified signature type is not supported."), UNREADABLE_KEYSTORE("crypto:unreadable-keystore", "I/O error while reading keystore, or the password is incorrect."), DENIED_KEYSTORE("crypto:denied-keystore", "Permission denied to read keystore."), KEYSTORE_URL("crypto:keystore-url", "The keystore URL is invalid."), - KeyStoreException("crypto:keystore-type", "The keystore type is not supported."), + KEYSTORE_TYPE("crypto:keystore-type", "The keystore type is not supported.", KeyStoreException.class), ALIAS_KEY("crypto:alias-key", "Cannot find key for alias in given keystore."), SIGNATURE_ELEMENT("crypto:signature-element", "Cannot find Signature element."), - NoSuchPaddingException("crypto:inexistent-padding", "No such padding."), - BadPaddingException("crypto:incorrect-padding", "Incorrect padding."), + INEXISTENT_PADDING("crypto:inexistent-padding", "No such padding.", NoSuchPaddingException.class), + INCORRECT_PADDING("crypto:incorrect-padding", "Incorrect padding.", BadPaddingException.class), ENCRYPTION_TYPE("crypto:encryption-type", "The encryption type is not supported."), - InvalidKeySpecException("crypto:invalid-crypto-key", "The cryptographic key is invalid."), - InvalidKeyException("crypto:invalid-crypto-key", "The cryptographic key is invalid."), - IllegalBlockSizeException("crypto:block-size", "Illegal block size."), + INVALID_CRYPTO_KEY("crypto:invalid-crypto-key", "The cryptographic key is invalid.", InvalidKeySpecException.class, InvalidKeyException.class), + BLOCK_SIZE("crypto:block-size", "Illegal block size.", IllegalBlockSizeException.class), DECRYPTION_TYPE("crypto:decryption-type", "The decryption type is not supported."), - NoSuchProviderException("crypto:no-provider", "The provider is not set."), + NO_PROVIDER("crypto:no-provider", "The provider is not set.", NoSuchProviderException.class), INPUT_RESOURCES("crypto.input-resources", "The 'enveloped' and 'enveloping' signatures have to be applied to only one resource."), INCORRECT_INITIALIZATION_VECTOR("crypto:incorrect-initialization-vector", "The initialization vector is not correct"); - private String code; - private String message; + private final String code; + private final String message; + @Nullable final Class[] describesExceptions; + + CryptoError(final String code, final String message) { + this(code, message, null); + } - CryptoError(String code, String message) { + CryptoError(final String code, final String message, @Nullable final Class... describesExceptions) { this.code = code; this.message = message; + this.describesExceptions = describesExceptions; } public String getCode() { @@ -59,4 +75,22 @@ public String getMessage() { public String getDescription() { return this.code + ", " + this.message; } + + public static @Nullable CryptoError describeException(@Nullable final Throwable t) { + if (t == null) { + return null; + } + + for (final CryptoError cryptoError : values()) { + if (cryptoError.describesExceptions != null) { + for (final Class describesException : cryptoError.describesExceptions) { + if (describesException == t.getClass()) { + return cryptoError; + } + } + } + } + + return null; + } } diff --git a/src/main/java/ro/kuberam/libs/java/crypto/CryptoException.java b/src/main/java/ro/kuberam/libs/java/crypto/CryptoException.java index c64e284..3da7519 100755 --- a/src/main/java/ro/kuberam/libs/java/crypto/CryptoException.java +++ b/src/main/java/ro/kuberam/libs/java/crypto/CryptoException.java @@ -19,28 +19,37 @@ */ package ro.kuberam.libs.java.crypto; +import javax.annotation.Nullable; + public class CryptoException extends Exception { - /** - * - */ private static final long serialVersionUID = -2606956271206243301L; - private CryptoError cryptoError; + @Nullable private final CryptoError cryptoError; - public CryptoException(CryptoError cryptoError) { + public CryptoException(final CryptoError cryptoError) { super(cryptoError.getDescription()); this.cryptoError = cryptoError; } - public CryptoException(CryptoError cryptoError, Throwable cause) { + public CryptoException(final CryptoError cryptoError, final Throwable cause) { super(cryptoError.getDescription(), cause); this.cryptoError = cryptoError; } - public static CryptoException fromCause(final Throwable cause) { - final CryptoError cryptoError = CryptoError.valueOf(cause.getClass().getSimpleName()); - return new CryptoException(cryptoError, cause); - } + public CryptoException(final Throwable cause) { + super(getDesc(cause), cause); + this.cryptoError = CryptoError.describeException(cause); + } + + private static String getDesc(final Throwable cause) { + final CryptoError cryptoError = CryptoError.describeException(cause); + if (cryptoError != null) { + return cryptoError.getDescription(); + } else { + return cause.getMessage(); + } + } + @Nullable public CryptoError getCryptoError() { return cryptoError; } diff --git a/src/main/java/ro/kuberam/libs/java/crypto/Parameters.java b/src/main/java/ro/kuberam/libs/java/crypto/Parameters.java index 7f77a6b..784ae42 100755 --- a/src/main/java/ro/kuberam/libs/java/crypto/Parameters.java +++ b/src/main/java/ro/kuberam/libs/java/crypto/Parameters.java @@ -42,7 +42,7 @@ public String getCanonicalizationAlgorithm() { public void setCanonicalizationAlgorithm(final String canonicalizationAlgorithm) throws CryptoException { if (!contains(CANONICALIZATION_ALGORITHM_VALUES, canonicalizationAlgorithm)) { - throw new CryptoException(CryptoError.NoSuchAlgorithmException); + throw new CryptoException(CryptoError.UNKNOWN_ALGORITHM); } if (canonicalizationAlgorithm.equals("exclusive")) { @@ -62,7 +62,7 @@ public String getDigestAlgorithm() { public void setDigestAlgorithm(final String digestAlgorithm) throws CryptoException { if (!contains(DIGEST_ALGORITHM_VALUES, digestAlgorithm)) { - throw new CryptoException(CryptoError.NoSuchAlgorithmException); + throw new CryptoException(CryptoError.UNKNOWN_ALGORITHM); } if (digestAlgorithm.equals("SHA256")) { @@ -80,7 +80,7 @@ public String getSignatureAlgorithm() { public void setSignatureAlgorithm(final String signatureAlgorithm) throws CryptoException { if (!contains(SIGNATURE_ALGORITHM_VALUES, signatureAlgorithm)) { - throw new CryptoException(CryptoError.NoSuchAlgorithmException); + throw new CryptoException(CryptoError.UNKNOWN_ALGORITHM); } if (signatureAlgorithm.equals("DSA_SHA1")) { diff --git a/src/main/java/ro/kuberam/libs/java/crypto/digest/Hash.java b/src/main/java/ro/kuberam/libs/java/crypto/digest/Hash.java index 87964df..2e37e6d 100755 --- a/src/main/java/ro/kuberam/libs/java/crypto/digest/Hash.java +++ b/src/main/java/ro/kuberam/libs/java/crypto/digest/Hash.java @@ -22,10 +22,8 @@ import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.util.Base64; -import java.util.Optional; +import java.security.*; +import java.util.*; import javax.annotation.Nullable; import org.slf4j.Logger; @@ -47,16 +45,20 @@ public class Hash { private static final Logger LOG = LoggerFactory.getLogger(Hash.class); public static String hashString(final String data, final String algorithm) throws CryptoException { - return hashString(data, algorithm, null); + return hashString(data, algorithm, null, null); } - public static String hashString(final String data, final String algorithm, final @Nullable String format) + public static String hashString(final String data, final String algorithm, @Nullable final String provider) throws CryptoException { + return hashString(data, algorithm, provider, null); + } + + public static String hashString(final String data, final String algorithm, @Nullable final String provider, final @Nullable String format) throws CryptoException { // TODO: validate the format final String actualFormat = Optional.ofNullable(format).filter(str -> !str.isEmpty()).orElse("base64"); - final MessageDigest messageDigester = getMessageDigester(algorithm); + final MessageDigest messageDigester = getMessageDigester(algorithm, provider); messageDigester.update(data.getBytes(StandardCharsets.UTF_8)); final byte[] resultBytes = messageDigester.digest(); @@ -70,17 +72,22 @@ public static String hashString(final String data, final String algorithm, final public static String hashBinary(final InputStream data, final String algorithm) throws CryptoException, IOException { - return hashBinary(data, algorithm, null); + return hashBinary(data, algorithm, null, null); + } + + public static String hashBinary(final InputStream data, final String algorithm, @Nullable final String provider) + throws CryptoException, IOException { + return hashBinary(data, algorithm, provider, null); } - public static String hashBinary(final InputStream data, final String algorithm, @Nullable final String format) + public static String hashBinary(final InputStream data, final String algorithm, @Nullable final String provider, @Nullable final String format) throws CryptoException, IOException { // TODO: validate the format final String actualFormat = Optional.ofNullable(format).filter(str -> !str.isEmpty()).orElse("base64"); final byte[] resultBytes; - final MessageDigest messageDigester = getMessageDigester(algorithm); + final MessageDigest messageDigester = getMessageDigester(algorithm, provider); final byte[] buf = new byte[Buffer.TRANSFER_SIZE]; int read = -1; @@ -100,11 +107,87 @@ public static String hashBinary(final InputStream data, final String algorithm, return result; } - private static MessageDigest getMessageDigester(final String algorithm) throws CryptoException { + private static MessageDigest getMessageDigester(final String algorithm, @Nullable final String provider) throws CryptoException { try { - return MessageDigest.getInstance(algorithm); - } catch (NoSuchAlgorithmException e) { - throw new CryptoException(CryptoError.NoSuchAlgorithmException, e); + if (provider != null) { + return MessageDigest.getInstance(algorithm, provider); + } else { + return MessageDigest.getInstance(algorithm); + } + } catch (final NoSuchAlgorithmException e) { + throw new CryptoException(CryptoError.UNKNOWN_ALGORITHM, e); + } catch (final NoSuchProviderException e) { + throw new CryptoException(CryptoError.UNKNOWN_PROVIDER, e); } } + + /** + * Returns a list of security providers which + * offer hash services. + * + * @return the names of the security providers which + * offer hash services. + */ + public static List listProviders() { + final List hashProviders = new ArrayList<>(); + + final Provider[] providers = Security.getProviders(); + for (final Provider provider : providers) { + final Set services = provider.getServices(); + for (final Provider.Service service : services) { + if (service.getType().equals("MessageDigest")) { + hashProviders.add(provider.getName()); + break; + } + } + } + + return hashProviders; + } + + /** + * Returns a Map of all hash services from each + * security provider. + * + * @return a map from key: `service provider name` to value: `algorithm name(s)`. + */ + public static Map> listAlgorithms() { + final Map> algorithms = new HashMap<>(); + + final Provider[] providers = Security.getProviders(); + for (final Provider provider : providers) { + final Set services = provider.getServices(); + for (final Provider.Service service : services) { + if (service.getType().equals("MessageDigest")) { + + final Set providerAlgs = algorithms.computeIfAbsent(provider.getName(), k -> new HashSet<>()); + providerAlgs.add(service.getAlgorithm()); + } + } + } + + return algorithms; + } + + /** + * Returns a Map of all hash services from a + * security provider. + * + * @param providerName the name of the security provider. + * + * @return the names of the algorithms provided by the security provider. + */ + public static Set listAlgorithms(final String providerName) { + final Set algorithms = new HashSet<>(); + + final Provider provider = Security.getProvider(providerName); + final Set services = provider.getServices(); + for (final Provider.Service service : services) { + if (service.getType().equals("MessageDigest")) { + algorithms.add(service.getAlgorithm()); + } + } + + return algorithms; + } } diff --git a/src/main/java/ro/kuberam/libs/java/crypto/digest/Hmac.java b/src/main/java/ro/kuberam/libs/java/crypto/digest/Hmac.java index 3b6e7e0..ed26067 100755 --- a/src/main/java/ro/kuberam/libs/java/crypto/digest/Hmac.java +++ b/src/main/java/ro/kuberam/libs/java/crypto/digest/Hmac.java @@ -98,7 +98,7 @@ public static byte[] hmac(final byte[] data, final byte[] secretKey, String algo return mac.doFinal(data); } catch (NoSuchAlgorithmException | InvalidKeyException e) { - throw CryptoException.fromCause(e); + throw new CryptoException(e); } } @@ -125,7 +125,7 @@ public static byte[] hmac(final InputStream data, final byte[] secretKey, String return mac.doFinal(); } catch (NoSuchAlgorithmException | InvalidKeyException e) { - throw CryptoException.fromCause(e); + throw new CryptoException(e); } } } diff --git a/src/main/java/ro/kuberam/libs/java/crypto/digitalSignature/GenerateXmlSignature.java b/src/main/java/ro/kuberam/libs/java/crypto/digitalSignature/GenerateXmlSignature.java index fb1df06..4bd19ca 100755 --- a/src/main/java/ro/kuberam/libs/java/crypto/digitalSignature/GenerateXmlSignature.java +++ b/src/main/java/ro/kuberam/libs/java/crypto/digitalSignature/GenerateXmlSignature.java @@ -103,7 +103,7 @@ public static String generate(Document inputDoc, String mechanismType, String ca NodeList nodes = (NodeList) expr.evaluate(inputDoc, XPathConstants.NODESET); if (nodes.getLength() < 1) { // TODO this error message has to replaced - throw new CryptoException(CryptoError.NoSuchAlgorithmException); + throw new CryptoException(CryptoError.UNKNOWN_ALGORITHM); } // Node nodeToSign = nodes.item(0); @@ -134,7 +134,7 @@ public static String generate(Document inputDoc, String mechanismType, String ca try { keyStore = KeyStore.getInstance(certificateDetails[0]); } catch (KeyStoreException e) { - throw CryptoException.fromCause(e); + throw new CryptoException(e); } keyStore.load(keyStoreInputStream, certificateDetails[1].toCharArray()); String alias = certificateDetails[2]; @@ -219,14 +219,14 @@ public static String generate(Document inputDoc, String mechanismType, String ca return serializer.writeToString(sigParent); } } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException e) { - throw new CryptoException(CryptoError.NoSuchAlgorithmException, e); + throw new CryptoException(CryptoError.UNKNOWN_ALGORITHM, e); } catch (CertificateException e) { // TODO error code needs improving - throw new CryptoException(CryptoError.InvalidKeySpecException, e); + throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e); } catch (KeyStoreException e) { throw new CryptoException(CryptoError.UNREADABLE_KEYSTORE, e); } catch (UnrecoverableKeyException | KeyException e) { - throw new CryptoException(CryptoError.InvalidKeySpecException, e); + throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e); } catch (ParserConfigurationException | XPathExpressionException | MarshalException e) { throw new IOException(e); } @@ -268,7 +268,7 @@ private static String getCanonicalizationAlgorithmUri(String canonicalizationAlg return CanonicalizationMethod.INCLUSIVE; default: - throw new CryptoException(CryptoError.NoSuchAlgorithmException); + throw new CryptoException(CryptoError.UNKNOWN_ALGORITHM); } } @@ -285,7 +285,7 @@ private static String getDigestAlgorithmURI(String digestAlgorithm) throws Crypt return DigestMethod.SHA1; default: - throw new CryptoException(CryptoError.NoSuchAlgorithmException); + throw new CryptoException(CryptoError.UNKNOWN_ALGORITHM); } } @@ -299,7 +299,7 @@ private static String getSignatureAlgorithmURI(String signatureAlgorithm) throws return SignatureMethod.RSA_SHA1; default: - throw new CryptoException(CryptoError.NoSuchAlgorithmException); + throw new CryptoException(CryptoError.UNKNOWN_ALGORITHM); } } } \ No newline at end of file diff --git a/src/main/java/ro/kuberam/libs/java/crypto/encrypt/AsymmetricEncryption.java b/src/main/java/ro/kuberam/libs/java/crypto/encrypt/AsymmetricEncryption.java index f562043..7bdc721 100755 --- a/src/main/java/ro/kuberam/libs/java/crypto/encrypt/AsymmetricEncryption.java +++ b/src/main/java/ro/kuberam/libs/java/crypto/encrypt/AsymmetricEncryption.java @@ -66,7 +66,7 @@ public static String encryptString(String data, Key key, String transformationNa resultBytes = cipher.doFinal(dataBytes); } catch (IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) { - throw CryptoException.fromCause(e); + throw new CryptoException(e); } catch (Exception e) { e.printStackTrace(); } @@ -93,7 +93,7 @@ public static String decryptString(String encryptedData, PrivateKey privateKey, resultBytes = cipher.doFinal(dataBytes); } catch (IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) { - throw CryptoException.fromCause(e); + throw new CryptoException(e); } catch (Exception e) { e.printStackTrace(); } @@ -120,7 +120,7 @@ public static String encryptBinary(InputStream data, String base64PublicKey, Str resultBytes = cipher.doFinal(); } catch (IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) { - throw CryptoException.fromCause(e); + throw new CryptoException(e); } catch (Exception e) { e.printStackTrace(); } @@ -147,7 +147,7 @@ public static String decryptBinary(InputStream data, String base64PrivateKey, St resultBytes = cipher.doFinal(); } catch (IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) { - throw CryptoException.fromCause(e); + throw new CryptoException(e); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/ro/kuberam/libs/java/crypto/encrypt/SymmetricEncryption.java b/src/main/java/ro/kuberam/libs/java/crypto/encrypt/SymmetricEncryption.java index 68757cc..c9ad5d4 100755 --- a/src/main/java/ro/kuberam/libs/java/crypto/encrypt/SymmetricEncryption.java +++ b/src/main/java/ro/kuberam/libs/java/crypto/encrypt/SymmetricEncryption.java @@ -66,7 +66,7 @@ public static byte[] operation(byte[] input, String secretKey, String transforma try { cipher = Cipher.getInstance(transformationName, actualProvider); } catch (NoSuchProviderException | NoSuchAlgorithmException | NoSuchPaddingException e) { - throw CryptoException.fromCause(e); + throw new CryptoException(e); } SecretKeySpec skeySpec = generateSecretKey(secretKey, algorithm); @@ -75,20 +75,20 @@ public static byte[] operation(byte[] input, String secretKey, String transforma try { cipher.init(operationType, skeySpec, ivSpec); } catch (InvalidAlgorithmParameterException | InvalidKeyException e) { - throw CryptoException.fromCause(e); + throw new CryptoException(e); } } else { try { cipher.init(operationType, skeySpec); } catch (InvalidKeyException e) { - throw new CryptoException(CryptoError.InvalidKeySpecException, e); + throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e); } } try { result = cipher.doFinal(input); } catch (IllegalBlockSizeException | BadPaddingException e) { - throw CryptoException.fromCause(e); + throw new CryptoException(e); } return result; diff --git a/src/test/java/ro/kuberam/libs/java/crypto/ParametersTest.java b/src/test/java/ro/kuberam/libs/java/crypto/ParametersTest.java index 217d37f..e6ea486 100755 --- a/src/test/java/ro/kuberam/libs/java/crypto/ParametersTest.java +++ b/src/test/java/ro/kuberam/libs/java/crypto/ParametersTest.java @@ -34,7 +34,7 @@ public void testCanonicalizationAlgorithmWrongValue() { parameters.setCanonicalizationAlgorithm("inclusive-with-commentss"); fail("Algorithm should have been unknown"); } catch (CryptoException e) { - assertEquals(CryptoError.NoSuchAlgorithmException, e.getCryptoError()); + assertEquals(CryptoError.UNKNOWN_ALGORITHM, e.getCryptoError()); } } @@ -52,7 +52,7 @@ public void testDigestAlgorithmWrongValue() { parameters.setDigestAlgorithm("SHA1008"); fail("Algorithm should have been unknown"); } catch (CryptoException e) { - assertEquals(CryptoError.NoSuchAlgorithmException, e.getCryptoError()); + assertEquals(CryptoError.UNKNOWN_ALGORITHM, e.getCryptoError()); } } @@ -71,7 +71,7 @@ public void testSignatureAlgorithmWrongValue() { parameters.setSignatureAlgorithm("RSA_SHA1008"); fail("Algorithm should have been unknown"); } catch (CryptoException e) { - assertEquals(CryptoError.NoSuchAlgorithmException, e.getCryptoError()); + assertEquals(CryptoError.UNKNOWN_ALGORITHM, e.getCryptoError()); } } diff --git a/src/test/java/ro/kuberam/libs/java/crypto/digest/HashBinaryTest.java b/src/test/java/ro/kuberam/libs/java/crypto/digest/HashBinaryTest.java index 4aacc58..6d5fd1d 100644 --- a/src/test/java/ro/kuberam/libs/java/crypto/digest/HashBinaryTest.java +++ b/src/test/java/ro/kuberam/libs/java/crypto/digest/HashBinaryTest.java @@ -73,7 +73,7 @@ public void hashBinary() throws IOException, CryptoException { try (final InputStream input = getClass().getResourceAsStream("../keystore.ks")) { assertNotNull(input); - final String result = Hash.hashBinary(input, algorithm, format); + final String result = Hash.hashBinary(input, algorithm, null, format); assertEquals(expected, result); } } diff --git a/src/test/java/ro/kuberam/libs/java/crypto/digest/HashBinaryWithWrongAlgorithmTest.java b/src/test/java/ro/kuberam/libs/java/crypto/digest/HashBinaryWithWrongAlgorithmTest.java index 6f7200d..21e1df0 100755 --- a/src/test/java/ro/kuberam/libs/java/crypto/digest/HashBinaryWithWrongAlgorithmTest.java +++ b/src/test/java/ro/kuberam/libs/java/crypto/digest/HashBinaryWithWrongAlgorithmTest.java @@ -58,10 +58,10 @@ public void hashBinaryWithWrongAlgorithm() throws IOException { try (final InputStream input = getClass().getResourceAsStream("../keystore.ks")) { assertNotNull(input); - Hash.hashBinary(input, algorithm, format); + Hash.hashBinary(input, algorithm, null, format); fail("Algorithm: " + algorithm + " should have been unknown"); } catch (final CryptoException e) { - assertEquals(CryptoError.NoSuchAlgorithmException, e.getCryptoError()); + assertEquals(CryptoError.UNKNOWN_ALGORITHM, e.getCryptoError()); } } } diff --git a/src/test/java/ro/kuberam/libs/java/crypto/digest/HashLargeBinaryTest.java b/src/test/java/ro/kuberam/libs/java/crypto/digest/HashLargeBinaryTest.java index ab57384..831dc77 100644 --- a/src/test/java/ro/kuberam/libs/java/crypto/digest/HashLargeBinaryTest.java +++ b/src/test/java/ro/kuberam/libs/java/crypto/digest/HashLargeBinaryTest.java @@ -87,7 +87,7 @@ public static void setup() throws IOException { @Test public void hashLargeBinary() throws IOException, CryptoException { try (final InputStream is = Files.newInputStream(largeFile)) { - final String result = Hash.hashBinary(is, algorithm, format); + final String result = Hash.hashBinary(is, algorithm, null, format); assertEquals(expected, result); } } diff --git a/src/test/java/ro/kuberam/libs/java/crypto/digest/HashLargeStringTest.java b/src/test/java/ro/kuberam/libs/java/crypto/digest/HashLargeStringTest.java index ab4b439..59c5134 100644 --- a/src/test/java/ro/kuberam/libs/java/crypto/digest/HashLargeStringTest.java +++ b/src/test/java/ro/kuberam/libs/java/crypto/digest/HashLargeStringTest.java @@ -68,8 +68,8 @@ public static Collection data() { public String expected; @Test - public void hashLargeString() throws IOException, CryptoException { - final String result = Hash.hashString(generate5MbString(), algorithm, format); + public void hashLargeString() throws CryptoException { + final String result = Hash.hashString(generate5MbString(), algorithm, null, format); assertEquals(expected, result); } } diff --git a/src/test/java/ro/kuberam/libs/java/crypto/digest/HashStringTest.java b/src/test/java/ro/kuberam/libs/java/crypto/digest/HashStringTest.java index 5ffe1ff..fb45492 100644 --- a/src/test/java/ro/kuberam/libs/java/crypto/digest/HashStringTest.java +++ b/src/test/java/ro/kuberam/libs/java/crypto/digest/HashStringTest.java @@ -67,9 +67,9 @@ public static Collection data() { public String expected; @Test - public void hashString() throws IOException, CryptoException { + public void hashString() throws CryptoException { final String input = "Short string for tests."; - final String result = Hash.hashString(input, algorithm, format); + final String result = Hash.hashString(input, algorithm, null, format); assertEquals(expected, result); } } diff --git a/src/test/java/ro/kuberam/libs/java/crypto/digest/HashStringWithWrongAlgorithmTest.java b/src/test/java/ro/kuberam/libs/java/crypto/digest/HashStringWithWrongAlgorithmTest.java index 92cd3fd..cf6d334 100755 --- a/src/test/java/ro/kuberam/libs/java/crypto/digest/HashStringWithWrongAlgorithmTest.java +++ b/src/test/java/ro/kuberam/libs/java/crypto/digest/HashStringWithWrongAlgorithmTest.java @@ -57,10 +57,10 @@ public static Collection data() { public void hashStringWithWrongAlgorithm() { try { final String input = "Short string for tests."; - Hash.hashString(input, algorithm, format); + Hash.hashString(input, algorithm, null, format); fail("Algorithm: " + algorithm + " should have been unknown"); } catch (final CryptoException e) { - assertEquals(CryptoError.NoSuchAlgorithmException, e.getCryptoError()); + assertEquals(CryptoError.UNKNOWN_ALGORITHM, e.getCryptoError()); } } } diff --git a/src/test/java/ro/kuberam/libs/java/crypto/encrypt/SymmetricEncryptionTest.java b/src/test/java/ro/kuberam/libs/java/crypto/encrypt/SymmetricEncryptionTest.java index ad0c704..b6fcdb6 100755 --- a/src/test/java/ro/kuberam/libs/java/crypto/encrypt/SymmetricEncryptionTest.java +++ b/src/test/java/ro/kuberam/libs/java/crypto/encrypt/SymmetricEncryptionTest.java @@ -25,7 +25,6 @@ import java.io.IOException; import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Test; import ro.kuberam.libs.java.crypto.CryptoError; @@ -41,16 +40,12 @@ public class SymmetricEncryptionTest extends CryptoModuleTests { private static String iv; @BeforeClass - public static void beforeClassFunction() { - try { - iv = Hash.hashString("initialization vector", "MD5", "hex"); - } catch (CryptoException e) { - e.printStackTrace(); - } + public static void beforeClassFunction() throws CryptoException { + iv = Hash.hashString("initialization vector", "MD5", null, "hex"); } @Test - public void aesAlgorithmAndDefaultProviderAndCbcMode() throws Exception { + public void aesAlgorithmAndDefaultProviderAndCbcMode() throws CryptoException, IOException { byte[] encryptionResult = SymmetricEncryption.encrypt(longInputBytes, key, aesAlgorithmCbcMode, iv, ""); byte[] decryptionResult = SymmetricEncryption.decrypt(encryptionResult, key, aesAlgorithmCbcMode, iv, ""); @@ -58,7 +53,7 @@ public void aesAlgorithmAndDefaultProviderAndCbcMode() throws Exception { } @Test - public void aesAlgorithmAndCbcMode() throws Exception { + public void aesAlgorithmAndCbcMode() throws CryptoException, IOException { byte[] encryptionResult = SymmetricEncryption.encrypt(longInputBytes, key, aesAlgorithmCbcMode, iv, sunProvider); byte[] decryptionResult = SymmetricEncryption.decrypt(encryptionResult, key, aesAlgorithmCbcMode, iv, @@ -68,21 +63,20 @@ public void aesAlgorithmAndCbcMode() throws Exception { } @Test - public void aesAlgorithmAndDefaultProviderAndEcbMode() throws Exception { + public void aesAlgorithmAndDefaultProviderAndEcbMode() throws CryptoException, IOException { byte[] encryptionResult = SymmetricEncryption.encrypt(longInputBytes, key, aesAlgorithmEcbMode, "", ""); byte[] decryptionResult = SymmetricEncryption.decrypt(encryptionResult, key, aesAlgorithmEcbMode, "", ""); assertEquals(longString, new String(decryptionResult, UTF_8)); } - @Ignore @Test - public void aesAlgorithmAndWrongKeyAndDefaultProviderAndCbcMode() throws IOException, CryptoException { + public void aesAlgorithmAndWrongKeyAndDefaultProviderAndCbcMode() throws IOException { try { byte[] encryptionResult = SymmetricEncryption.encrypt(longInputBytes, key, aesAlgorithmCbcMode, iv, ""); SymmetricEncryption.decrypt(encryptionResult, wrongKey, aesAlgorithmCbcMode, iv, ""); } catch (CryptoException e) { - assertEquals(CryptoError.InvalidKeySpecException, e.getCryptoError()); + assertEquals(CryptoError.INVALID_CRYPTO_KEY, e.getCryptoError()); } } }