1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.security;
import org.junit.Test;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
/**
* @author bjorncs
*/
public class KeyUtilsTest {
@Test
public void can_extract_public_key_from_rsa_private() {
KeyPair keyPair = KeyUtils.generateKeypair(KeyAlgorithm.RSA);
PublicKey publicKey = KeyUtils.extractPublicKey(keyPair.getPrivate());
assertNotNull(publicKey);
}
@Test
public void can_extract_public_key_from_ecdsa_private() {
KeyPair keyPair = KeyUtils.generateKeypair(KeyAlgorithm.EC);
PublicKey publicKey = KeyUtils.extractPublicKey(keyPair.getPrivate());
assertNotNull(publicKey);
}
@Test
public void can_serialize_and_deserialize_rsa_privatekey_using_pkcs1_pem_format() {
KeyPair keyPair = KeyUtils.generateKeypair(KeyAlgorithm.RSA);
String pem = KeyUtils.toPem(keyPair.getPrivate(), KeyFormat.PKCS1);
assertThat(pem, containsString("BEGIN RSA PRIVATE KEY"));
assertThat(pem, containsString("END RSA PRIVATE KEY"));
PrivateKey deserializedKey = KeyUtils.fromPemEncodedPrivateKey(pem);
assertEquals(keyPair.getPrivate(), deserializedKey);
assertEquals(KeyAlgorithm.RSA.getAlgorithmName(), deserializedKey.getAlgorithm());
}
@Test
public void can_serialize_and_deserialize_rsa_privatekey_using_pkcs8_pem_format() {
KeyPair keyPair = KeyUtils.generateKeypair(KeyAlgorithm.RSA);
String pem = KeyUtils.toPem(keyPair.getPrivate(), KeyFormat.PKCS8);
assertThat(pem, containsString("BEGIN PRIVATE KEY"));
assertThat(pem, containsString("END PRIVATE KEY"));
PrivateKey deserializedKey = KeyUtils.fromPemEncodedPrivateKey(pem);
assertEquals(keyPair.getPrivate(), deserializedKey);
assertEquals(KeyAlgorithm.RSA.getAlgorithmName(), deserializedKey.getAlgorithm());
}
@Test
public void can_serialize_and_deserialize_ec_privatekey_using_pkcs1_pem_format() {
KeyPair keyPair = KeyUtils.generateKeypair(KeyAlgorithm.EC);
String pem = KeyUtils.toPem(keyPair.getPrivate(), KeyFormat.PKCS1);
assertThat(pem, containsString("BEGIN EC PRIVATE KEY"));
assertThat(pem, containsString("END EC PRIVATE KEY"));
PrivateKey deserializedKey = KeyUtils.fromPemEncodedPrivateKey(pem);
assertEquals(keyPair.getPrivate(), deserializedKey);
assertEquals(KeyAlgorithm.EC.getAlgorithmName(), deserializedKey.getAlgorithm());
}
@Test
public void can_serialize_and_deserialize_ec_privatekey_using_pkcs8_pem_format() {
KeyPair keyPair = KeyUtils.generateKeypair(KeyAlgorithm.EC);
String pem = KeyUtils.toPem(keyPair.getPrivate(), KeyFormat.PKCS8);
assertThat(pem, containsString("BEGIN PRIVATE KEY"));
assertThat(pem, containsString("END PRIVATE KEY"));
PrivateKey deserializedKey = KeyUtils.fromPemEncodedPrivateKey(pem);
assertEquals(keyPair.getPrivate(), deserializedKey);
assertEquals(KeyAlgorithm.EC.getAlgorithmName(), deserializedKey.getAlgorithm());
}
@Test
public void can_serialize_and_deserialize_rsa_publickey_using_pem_format() {
KeyPair keyPair = KeyUtils.generateKeypair(KeyAlgorithm.RSA);
String pem = KeyUtils.toPem(keyPair.getPublic());
assertThat(pem, containsString("BEGIN PUBLIC KEY"));
assertThat(pem, containsString("END PUBLIC KEY"));
PublicKey deserializedKey = KeyUtils.fromPemEncodedPublicKey(pem);
assertEquals(keyPair.getPublic(), deserializedKey);
assertEquals(KeyAlgorithm.RSA.getAlgorithmName(), deserializedKey.getAlgorithm());
}
@Test
public void can_serialize_and_deserialize_ec_publickey_using_pem_format() {
KeyPair keyPair = KeyUtils.generateKeypair(KeyAlgorithm.EC);
String pem = KeyUtils.toPem(keyPair.getPublic());
assertThat(pem, containsString("BEGIN PUBLIC KEY"));
assertThat(pem, containsString("END PUBLIC KEY"));
PublicKey deserializedKey = KeyUtils.fromPemEncodedPublicKey(pem);
assertEquals(keyPair.getPublic(), deserializedKey);
assertEquals(KeyAlgorithm.EC.getAlgorithmName(), deserializedKey.getAlgorithm());
}
}
|