From 58bafb860b060c609a01815657b4df22ce8fbffc Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Tue, 12 Apr 2022 12:54:16 +0200 Subject: test on algorithm name resolver --- .../java/org/uic/barcode/utils/SecurityUtils.java | 57 +++++++++++++++++----- 1 file changed, 46 insertions(+), 11 deletions(-) (limited to 'src/main/java/org/uic/barcode/utils/SecurityUtils.java') diff --git a/src/main/java/org/uic/barcode/utils/SecurityUtils.java b/src/main/java/org/uic/barcode/utils/SecurityUtils.java index af1a65a..1fcc18a 100644 --- a/src/main/java/org/uic/barcode/utils/SecurityUtils.java +++ b/src/main/java/org/uic/barcode/utils/SecurityUtils.java @@ -11,22 +11,30 @@ import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; +/** + * The Class SecurityUtils. + */ public class SecurityUtils { - public static KeyFactory findKeyFactory(String oid, byte[] keyBytes) { + /** + * Find provider by public key. + * + * @param algorithmOid the algorithm oid used to generate the key + * @param keyBytes the encoded bytes of the public key + * @return the provider + */ + public static Provider findPublicKeyProvider(String keyAlgorithmOid, byte[] keyBytes) { + X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); - String name = null; + String name; try { - name = AlgorithmNameResolver.getName(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, oid); + name = AlgorithmNameResolver.getAlgorithmName(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, keyAlgorithmOid, null); } catch (Exception e2) { return null; } - if (name == null || name.length() == 0) { - return null; - } - + KeyFactory keyFactory = null; Provider[] provs = Security.getProviders(); @@ -39,17 +47,23 @@ public class SecurityUtils { if (keyFactory != null) { try { keyFactory.generatePublic(keySpec); - return keyFactory; + return provider; } catch (Exception e) { + provider = null; //try next } } } - return null; - + + return null; } - + /** + * Find private key provider. + * + * @param key the private key + * @return the provider + */ public static Provider findPrivateKeyProvider(PrivateKey key) { String name = key.getAlgorithm(); @@ -84,6 +98,13 @@ public class SecurityUtils { + /** + * Convert. + * + * @param key the key + * @param provider the provider + * @return the public key + */ public static PublicKey convert(PublicKey key, Provider provider) { PublicKey publicKey; @@ -108,6 +129,13 @@ public class SecurityUtils { } + /** + * Convert. + * + * @param key the key + * @param provider the provider + * @return the private key + */ public static PrivateKey convert(PrivateKey key, Provider provider) { PrivateKey privateKey; @@ -131,6 +159,13 @@ public class SecurityUtils { } + /** + * Find signature provider. + * + * @param encoded the encoded + * @param oid the oid + * @return the provider + */ public static Provider findSignatureProvider(byte[] encoded, String oid) { KeyFactory keyFactory = null; -- cgit v1.2.3