Search in sources :

Example 16 with Key

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));
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) Resource(io.churchkey.Resource) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) Decoder(io.churchkey.Decoder) KeyFactory(java.security.KeyFactory) RSAPublicKey(java.security.interfaces.RSAPublicKey) Key(io.churchkey.Key) Test(org.junit.Test)

Example 17 with Key

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));
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) Resource(io.churchkey.Resource) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) Decoder(io.churchkey.Decoder) KeyFactory(java.security.KeyFactory) RSAPublicKey(java.security.interfaces.RSAPublicKey) Key(io.churchkey.Key) Test(org.junit.Test)

Example 18 with Key

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);
    }
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeyFactory(java.security.KeyFactory) Key(io.churchkey.Key) RSAPublicKey(java.security.interfaces.RSAPublicKey)

Example 19 with Key

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);
}
Also used : PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) KeyFactory(java.security.KeyFactory) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) Key(io.churchkey.Key)

Example 20 with Key

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());
}
Also used : ECPublicKey(java.security.interfaces.ECPublicKey) Key(io.churchkey.Key) ECPrivateKey(java.security.interfaces.ECPrivateKey) ECPublicKey(java.security.interfaces.ECPublicKey)

Aggregations

Key (io.churchkey.Key)66 RSAPublicKey (java.security.interfaces.RSAPublicKey)33 Test (org.junit.Test)31 Resource (io.churchkey.Resource)30 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)30 ECPrivateKey (java.security.interfaces.ECPrivateKey)27 ECPublicKey (java.security.interfaces.ECPublicKey)25 DSAPublicKey (java.security.interfaces.DSAPublicKey)21 KeyFactory (java.security.KeyFactory)20 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)18 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)12 PublicKey (java.security.PublicKey)11 BigInteger (java.math.BigInteger)10 PrivateKey (java.security.PrivateKey)10 Asn1Object (io.churchkey.asn1.Asn1Object)8 DerParser (io.churchkey.asn1.DerParser)8 Decoder (io.churchkey.Decoder)7 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)7 IOException (java.io.IOException)5 UncheckedIOException (java.io.UncheckedIOException)5