smoothing
This commit is contained in:
parent
4c9fe8c72b
commit
c912fbcb92
2 changed files with 27 additions and 32 deletions
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue