use of io.churchkey.Key in project churchkey by tomitribe.
the class BeginPublicKeyTest method assertRsaDecode.
public static void assertRsaDecode(final Decoder decoder, final String algorithm, final int bits) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
final Resource resource = Resource.resource(algorithm, bits, 256);
final KeyFactory rsa = KeyFactory.getInstance(algorithm.toUpperCase());
final RSAPublicKey expected = (RSAPublicKey) rsa.generatePublic(new X509EncodedKeySpec(resource.bytes("public.pkcs8.der")));
final Key key = decoder.decode(resource.bytes("public.pkcs8.pem"));
assertEquals(Key.Algorithm.RSA, key.getAlgorithm());
assertEquals(Key.Type.PUBLIC, key.getType());
assertEquals(Key.Format.PEM, 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", ""), exported);
}
{
// 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 BeginPublicKeyTest method assertDsaDecode.
public static void assertDsaDecode(final Decoder decoder, final String algorithm, final int bits) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
final Resource resource = Resource.resource(algorithm, bits, 256);
final KeyFactory rsa = KeyFactory.getInstance(algorithm.toUpperCase());
final DSAPublicKey expected = (DSAPublicKey) rsa.generatePublic(new X509EncodedKeySpec(resource.bytes("public.pkcs8.der")));
final Key key = decoder.decode(resource.bytes("public.pkcs8.pem"));
assertEquals(Key.Algorithm.DSA, key.getAlgorithm());
assertEquals(Key.Type.PUBLIC, key.getType());
assertEquals(Key.Format.PEM, key.getFormat());
final DSAPublicKey actual = (DSAPublicKey) key.getKey();
assertEquals(expected.getY(), actual.getY());
assertEquals(expected.getParams().getG(), actual.getParams().getG());
assertEquals(expected.getParams().getP(), actual.getParams().getP());
assertEquals(expected.getParams().getQ(), actual.getParams().getQ());
{
// 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", ""), exported);
}
}
use of io.churchkey.Key in project churchkey by tomitribe.
the class BeginRsaPublicKeyTest 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.pkcs1.pem"));
assertEquals(Key.Algorithm.RSA, key.getAlgorithm());
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", ""), exported);
}
{
// 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 OpenSSHPrivateKeyTest method rsa.
@Test
public void rsa() throws Exception {
final Decoder decoder = Keys::decode;
final Resource resource = Resource.resource("opensshrsa", 2048, 256);
final KeyFactory rsa = KeyFactory.getInstance("RSA");
final RSAPrivateCrtKey expected = (RSAPrivateCrtKey) rsa.generatePrivate(new PKCS8EncodedKeySpec(resource.bytes("private.pkcs8.der")));
final byte[] bytes = resource.bytes("private.openssh");
final Key key = decoder.decode(bytes);
assertEquals(Key.Algorithm.RSA, key.getAlgorithm());
assertEquals(Key.Format.OPENSSH, key.getFormat());
assertEquals(Key.Type.PRIVATE, key.getType());
final RSAPrivateCrtKey actual = (RSAPrivateCrtKey) key.getKey();
KeyAsserts.assertRsaPrivateKey(expected, actual);
}
use of io.churchkey.Key in project churchkey by tomitribe.
the class JwkSecretKeyTest method testHs256SecretKey.
@Test
public void testHs256SecretKey() throws Exception {
final String jwk = "" + "{ \"kty\": \"oct\",\n" + " \"use\": \"sig\",\n" + " \"kid\": \"orange-1234\",\n" + " \"k\": \"VZ-0QGLZ2P_RPUSW10CIu0WMyXq-ND2pmDYzA0OTKW" + "THlp5iac5K4VeiRr-_BOoXJ4X2fSTt4nHwo_quta7j" + "JJKT4PEWyYanBSFsi0DW7owT-HExAGDyJtHUtNw5xs" + "s8Nj6OxNPv6rROE-kevhL2wB9cqgdIscbvDhras39c" + "wfs\",\n" + " \"alg\": \"HS256\"\n" + "}";
final Key key = Keys.decode(jwk.getBytes());
assertEquals("orange-1234", key.getAttribute("kid"));
assertEquals("sig", key.getAttribute("use"));
assertEquals("HS256", key.getAttribute("alg"));
assertTrue(!key.getAttributes().containsKey("kty"));
assertTrue(!key.getAttributes().containsKey("k"));
assertEquals(3, key.getAttributes().size());
final String encoded = "VZ-0QGLZ2P_RPUSW10CIu0WMyXq-ND2" + "pmDYzA0OTKWTHlp5iac5K4VeiRr-_BOoXJ4X2fSTt4nHwo_" + "quta7jJJKT4PEWyYanBSFsi0DW7owT-HExAGDyJtHUtNw5x" + "ss8Nj6OxNPv6rROE-kevhL2wB9cqgdIscbvDhras39cwfs";
assertArrayEquals(key.getKey().getEncoded(), Base64.getUrlDecoder().decode(encoded));
assertEquals(key.getAlgorithm(), Key.Algorithm.OCT);
assertEquals(key.getFormat(), Key.Format.JWK);
assertEquals(key.getType(), Key.Type.SECRET);
}
Aggregations