smoothing
This commit is contained in:
parent
4c9fe8c72b
commit
c912fbcb92
2 changed files with 27 additions and 32 deletions
|
@ -108,27 +108,33 @@ public class PrivateKeyReader {
|
||||||
};
|
};
|
||||||
PrivateKey privateKey = null;
|
PrivateKey privateKey = null;
|
||||||
PublicKey publicKey = null;
|
PublicKey publicKey = null;
|
||||||
if (keySpec instanceof DSAPrivateKeySpec) {
|
switch (keySpec) {
|
||||||
|
case DSAPrivateKeySpec dsaPrivateKeySpec -> {
|
||||||
KeyFactory keyFactory = KeyFactory.getInstance("DSA");
|
KeyFactory keyFactory = KeyFactory.getInstance("DSA");
|
||||||
privateKey = keyFactory.generatePrivate(keySpec);
|
privateKey = keyFactory.generatePrivate(keySpec);
|
||||||
publicKey = keyFactory.generatePublic(getDSAPublicKeySpec(privateKey));
|
publicKey = keyFactory.generatePublic(getDSAPublicKeySpec(privateKey));
|
||||||
} else if (keySpec instanceof EncodedKeySpec) {
|
}
|
||||||
|
case EncodedKeySpec encodedKeySpec -> {
|
||||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||||
privateKey = keyFactory.generatePrivate(keySpec);
|
privateKey = keyFactory.generatePrivate(keySpec);
|
||||||
publicKey = keyFactory.generatePublic(getRSAPublicKeySpec(keyFactory, privateKey));
|
publicKey = keyFactory.generatePublic(getRSAPublicKeySpec(keyFactory, privateKey));
|
||||||
} else if (keySpec instanceof RSAPrivateCrtKeySpec) {
|
}
|
||||||
|
case RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec -> {
|
||||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||||
privateKey = keyFactory.generatePrivate(keySpec);
|
privateKey = keyFactory.generatePrivate(keySpec);
|
||||||
publicKey = keyFactory.generatePublic(getRSAPublicKeySpec(keyFactory, privateKey));
|
publicKey = keyFactory.generatePublic(getRSAPublicKeySpec(keyFactory, privateKey));
|
||||||
} else if (keySpec instanceof ECPrivateKeySpec) {
|
}
|
||||||
|
case ECPrivateKeySpec ecPrivateKeySpec -> {
|
||||||
KeyFactory keyFactory = KeyFactory.getInstance("EC");
|
KeyFactory keyFactory = KeyFactory.getInstance("EC");
|
||||||
privateKey = keyFactory.generatePrivate(keySpec);
|
privateKey = keyFactory.generatePrivate(keySpec);
|
||||||
publicKey = keyFactory.generatePublic(getECPublicKeySpec(privateKey));
|
publicKey = keyFactory.generatePublic(getECPublicKeySpec(privateKey));
|
||||||
} else {
|
}
|
||||||
|
case null, default -> {
|
||||||
KeyFactory keyFactory = KeyFactory.getInstance("EdDSA");
|
KeyFactory keyFactory = KeyFactory.getInstance("EdDSA");
|
||||||
privateKey = keyFactory.generatePrivate(keySpec);
|
privateKey = keyFactory.generatePrivate(keySpec);
|
||||||
publicKey = keyFactory.generatePublic(getEdECPublicKeySpec(privateKey, rawKey.raw));
|
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.
|
||||||
*
|
*
|
||||||
|
|
|
@ -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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue