smoothing

This commit is contained in:
Jörg Prante 2024-05-27 22:44:55 +02:00
parent 4c9fe8c72b
commit c912fbcb92
2 changed files with 27 additions and 32 deletions

View file

@ -108,26 +108,32 @@ public class PrivateKeyReader {
}; };
PrivateKey privateKey = null; PrivateKey privateKey = null;
PublicKey publicKey = null; PublicKey publicKey = null;
if (keySpec instanceof DSAPrivateKeySpec) { switch (keySpec) {
KeyFactory keyFactory = KeyFactory.getInstance("DSA"); case DSAPrivateKeySpec dsaPrivateKeySpec -> {
privateKey = keyFactory.generatePrivate(keySpec); KeyFactory keyFactory = KeyFactory.getInstance("DSA");
publicKey = keyFactory.generatePublic(getDSAPublicKeySpec(privateKey)); privateKey = keyFactory.generatePrivate(keySpec);
} else if (keySpec instanceof EncodedKeySpec) { publicKey = keyFactory.generatePublic(getDSAPublicKeySpec(privateKey));
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); }
privateKey = keyFactory.generatePrivate(keySpec); case EncodedKeySpec encodedKeySpec -> {
publicKey = keyFactory.generatePublic(getRSAPublicKeySpec(keyFactory, privateKey)); KeyFactory keyFactory = KeyFactory.getInstance("RSA");
} else if (keySpec instanceof RSAPrivateCrtKeySpec) { privateKey = keyFactory.generatePrivate(keySpec);
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); publicKey = keyFactory.generatePublic(getRSAPublicKeySpec(keyFactory, privateKey));
privateKey = keyFactory.generatePrivate(keySpec); }
publicKey = keyFactory.generatePublic(getRSAPublicKeySpec(keyFactory, privateKey)); case RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec -> {
} else if (keySpec instanceof ECPrivateKeySpec) { KeyFactory keyFactory = KeyFactory.getInstance("RSA");
KeyFactory keyFactory = KeyFactory.getInstance("EC"); privateKey = keyFactory.generatePrivate(keySpec);
privateKey = keyFactory.generatePrivate(keySpec); publicKey = keyFactory.generatePublic(getRSAPublicKeySpec(keyFactory, privateKey));
publicKey = keyFactory.generatePublic(getECPublicKeySpec(privateKey)); }
} else { case ECPrivateKeySpec ecPrivateKeySpec -> {
KeyFactory keyFactory = KeyFactory.getInstance("EdDSA"); KeyFactory keyFactory = KeyFactory.getInstance("EC");
privateKey = keyFactory.generatePrivate(keySpec); privateKey = keyFactory.generatePrivate(keySpec);
publicKey = keyFactory.generatePublic(getEdECPublicKeySpec(privateKey, rawKey.raw)); 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) { if (publicKey != null) {
return new KeyPair(publicKey, privateKey); return new KeyPair(publicKey, privateKey);
@ -238,7 +244,6 @@ public class PrivateKeyReader {
return new DSAPublicKeySpec(y, p, q, g); return new DSAPublicKeySpec(y, p, q, g);
} }
/** /**
* Read EC private key in PKCS#1 format. * Read EC private key in PKCS#1 format.
* *

View file

@ -119,16 +119,6 @@ public class PrivateKeyReaderTest {
assertEquals("RSA", privateKey.getAlgorithm()); 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, private static void match(String algo,
PrivateKey privateKey, PrivateKey privateKey,
PublicKey publicKey) throws Exception { PublicKey publicKey) throws Exception {
@ -140,7 +130,7 @@ public class PrivateKeyReaderTest {
signature.initVerify(publicKey); signature.initVerify(publicKey);
signature.update(message.getBytes(StandardCharsets.UTF_8)); signature.update(message.getBytes(StandardCharsets.UTF_8));
assertTrue(signature.verify(payload)); assertTrue(signature.verify(payload));
logger.log(Level.INFO, "verified " + algo); logger.log(Level.INFO, "verified using " + algo);
} }