From c912fbcb92f652a24d7818de5924715572e6557c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Mon, 27 May 2024 22:44:55 +0200 Subject: [PATCH] smoothing --- .../xbib/net/security/PrivateKeyReader.java | 47 ++++++++++--------- .../security/test/PrivateKeyReaderTest.java | 12 +---- 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/net-security/src/main/java/org/xbib/net/security/PrivateKeyReader.java b/net-security/src/main/java/org/xbib/net/security/PrivateKeyReader.java index dbacce6..442b376 100644 --- a/net-security/src/main/java/org/xbib/net/security/PrivateKeyReader.java +++ b/net-security/src/main/java/org/xbib/net/security/PrivateKeyReader.java @@ -108,26 +108,32 @@ public class PrivateKeyReader { }; PrivateKey privateKey = null; PublicKey publicKey = null; - if (keySpec instanceof DSAPrivateKeySpec) { - KeyFactory keyFactory = KeyFactory.getInstance("DSA"); - privateKey = keyFactory.generatePrivate(keySpec); - publicKey = keyFactory.generatePublic(getDSAPublicKeySpec(privateKey)); - } else if (keySpec instanceof EncodedKeySpec) { - KeyFactory keyFactory = KeyFactory.getInstance("RSA"); - privateKey = keyFactory.generatePrivate(keySpec); - publicKey = keyFactory.generatePublic(getRSAPublicKeySpec(keyFactory, privateKey)); - } else if (keySpec instanceof RSAPrivateCrtKeySpec) { - KeyFactory keyFactory = KeyFactory.getInstance("RSA"); - privateKey = keyFactory.generatePrivate(keySpec); - publicKey = keyFactory.generatePublic(getRSAPublicKeySpec(keyFactory, privateKey)); - } else if (keySpec instanceof ECPrivateKeySpec) { - KeyFactory keyFactory = KeyFactory.getInstance("EC"); - privateKey = keyFactory.generatePrivate(keySpec); - publicKey = keyFactory.generatePublic(getECPublicKeySpec(privateKey)); - } else { - KeyFactory keyFactory = KeyFactory.getInstance("EdDSA"); - privateKey = keyFactory.generatePrivate(keySpec); - publicKey = keyFactory.generatePublic(getEdECPublicKeySpec(privateKey, rawKey.raw)); + switch (keySpec) { + case DSAPrivateKeySpec dsaPrivateKeySpec -> { + KeyFactory keyFactory = KeyFactory.getInstance("DSA"); + privateKey = keyFactory.generatePrivate(keySpec); + publicKey = keyFactory.generatePublic(getDSAPublicKeySpec(privateKey)); + } + case EncodedKeySpec encodedKeySpec -> { + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + privateKey = keyFactory.generatePrivate(keySpec); + publicKey = keyFactory.generatePublic(getRSAPublicKeySpec(keyFactory, privateKey)); + } + case RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec -> { + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + privateKey = keyFactory.generatePrivate(keySpec); + publicKey = keyFactory.generatePublic(getRSAPublicKeySpec(keyFactory, privateKey)); + } + case ECPrivateKeySpec ecPrivateKeySpec -> { + KeyFactory keyFactory = KeyFactory.getInstance("EC"); + privateKey = keyFactory.generatePrivate(keySpec); + publicKey = keyFactory.generatePublic(getECPublicKeySpec(privateKey)); + } + case null, default -> { + KeyFactory keyFactory = KeyFactory.getInstance("EdDSA"); + privateKey = keyFactory.generatePrivate(keySpec); + publicKey = keyFactory.generatePublic(getEdECPublicKeySpec(privateKey, rawKey.raw)); + } } if (publicKey != null) { return new KeyPair(publicKey, privateKey); @@ -238,7 +244,6 @@ public class PrivateKeyReader { return new DSAPublicKeySpec(y, p, q, g); } - /** * Read EC private key in PKCS#1 format. * diff --git a/net-security/src/test/java/org/xbib/net/security/test/PrivateKeyReaderTest.java b/net-security/src/test/java/org/xbib/net/security/test/PrivateKeyReaderTest.java index 0d51eff..21ccf31 100644 --- a/net-security/src/test/java/org/xbib/net/security/test/PrivateKeyReaderTest.java +++ b/net-security/src/test/java/org/xbib/net/security/test/PrivateKeyReaderTest.java @@ -119,16 +119,6 @@ public class PrivateKeyReaderTest { assertEquals("RSA", privateKey.getAlgorithm()); } - @Test - public void testKey() throws Exception { - InputStream inputStream = getClass().getResourceAsStream("/test.key"); - if (inputStream != null) { - PrivateKeyReader privateKeyReader = new PrivateKeyReader(); - PrivateKey privateKey = privateKeyReader.readPrivateKey(inputStream, null); - assertEquals("PKCS#8", privateKey.getFormat()); - } - } - private static void match(String algo, PrivateKey privateKey, PublicKey publicKey) throws Exception { @@ -140,7 +130,7 @@ public class PrivateKeyReaderTest { signature.initVerify(publicKey); signature.update(message.getBytes(StandardCharsets.UTF_8)); assertTrue(signature.verify(payload)); - logger.log(Level.INFO, "verified " + algo); + logger.log(Level.INFO, "verified using " + algo); }