use of io.churchkey.Key in project churchkey by tomitribe.
the class JwtRsaPublicKeyEncodeTest method testKeysDecode2048.
@Test
public void testKeysDecode2048() throws Exception {
final Decoder decoder = Keys::decode;
final Resource resource = Resource.resource("rsa", 2048, 256);
final KeyFactory rsa = KeyFactory.getInstance("RSA");
final RSAPublicKey expected = (RSAPublicKey) rsa.generatePublic(new X509EncodedKeySpec(resource.bytes("public.pkcs8.der")));
final Key key = decoder.decode(resource.bytes("public.jwk"));
assertKey(expected, key);
final byte[] encode = Keys.encode(key);
JsonAsserts.assertJson(new String(resource.bytes("public.jwk")), new String(encode));
assertKey(expected, Keys.decode(encode));
}
use of io.churchkey.Key in project churchkey by tomitribe.
the class JwtRsaPublicKeyEncodeTest method testKeysDecode1024.
@Test
public void testKeysDecode1024() throws Exception {
final Decoder decoder = Keys::decode;
final Resource resource = Resource.resource("rsa", 1024, 256);
final KeyFactory rsa = KeyFactory.getInstance("RSA");
final RSAPublicKey expected = (RSAPublicKey) rsa.generatePublic(new X509EncodedKeySpec(resource.bytes("public.pkcs8.der")));
final Key key = decoder.decode(resource.bytes("public.jwk"));
assertKey(expected, key);
final byte[] encode = Keys.encode(key);
JsonAsserts.assertJson(new String(resource.bytes("public.jwk")), new String(encode));
assertKey(expected, Keys.decode(encode));
}
use of io.churchkey.Key in project churchkey by tomitribe.
the class JwtRsaPublicKeyTest method assertDecode.
public static void assertDecode(final Decoder decoder, final Resource resource) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
final KeyFactory rsa = KeyFactory.getInstance("RSA");
final RSAPublicKey expected = (RSAPublicKey) rsa.generatePublic(new X509EncodedKeySpec(resource.bytes("public.pkcs8.der")));
final Key key = decoder.decode(resource.bytes("public.jwk"));
assertEquals(Key.Algorithm.RSA, key.getAlgorithm());
assertEquals(Key.Type.PUBLIC, key.getType());
assertEquals(Key.Format.JWK, key.getFormat());
final RSAPublicKey actual = (RSAPublicKey) key.getKey();
assertEquals(expected.getPublicExponent(), actual.getPublicExponent());
assertEquals(expected.getModulus(), actual.getModulus());
{
// Export to PEM
final String exported = new String(key.encode(Key.Format.PEM));
assertEquals(new String(resource.bytes("public.pkcs8.pem")), exported);
}
{
// Export to OPENSSH
// PEM Public Keys do not have comments, so remove the comment from the expected output
final String exported = new String(key.encode(Key.Format.OPENSSH));
assertEquals(new String(resource.bytes("public.openssh")).replace(" dblevins@mingus.lan", ""), new String(exported.getBytes()));
}
{
// Export to JWK
final String exported = new String(key.encode(Key.Format.JWK));
JsonAsserts.assertJson(new String(resource.bytes("public.jwk")), exported);
}
}
use of io.churchkey.Key in project churchkey by tomitribe.
the class BeginDsaPrivateKeyTest method assertDecode.
public void assertDecode(final Decoder decoder, final Resource resource) throws Exception {
final KeyFactory dsa = KeyFactory.getInstance("DSA");
final DSAPrivateKey expected = (DSAPrivateKey) dsa.generatePrivate(new PKCS8EncodedKeySpec(resource.bytes("private.pkcs8.der")));
final byte[] bytes = resource.bytes("private.pkcs1.pem");
final Key key = decoder.decode(bytes);
assertEquals(Key.Algorithm.DSA, key.getAlgorithm());
final DSAPrivateKey actual = (DSAPrivateKey) key.getKey();
KeyAsserts.assertDsaPrivateKey(expected, actual);
}
use of io.churchkey.Key in project churchkey by tomitribe.
the class BeginEcPrivateKeyTest method assertPublicKey.
private void assertPublicKey(final String format) throws IOException {
final Key key = EcKeys.decode(resource.bytes("private.pkcs1." + openSslCurveName + "." + format + ".pem"));
final Key publicKey = key.getPublicKey();
assertNotNull(publicKey);
assertTrue(publicKey.getKey() instanceof ECPublicKey);
assertEquals(Key.Algorithm.EC, publicKey.getAlgorithm());
assertEquals(Key.Format.PEM, publicKey.getFormat());
assertEquals(Key.Type.PUBLIC, publicKey.getType());
}
Aggregations