|
6 | 6 | import org.bouncycastle.asn1.x9.ECNamedCurveTable; |
7 | 7 | import org.bouncycastle.asn1.x9.X962Parameters; |
8 | 8 | import org.bouncycastle.asn1.x9.X9ECPoint; |
9 | | -import org.bouncycastle.cert.X509CertificateHolder; |
10 | 9 | import org.bouncycastle.crypto.digests.SHA256Digest; |
11 | 10 | import org.bouncycastle.crypto.generators.HKDFBytesGenerator; |
12 | 11 | import org.bouncycastle.crypto.params.HKDFParameters; |
13 | 12 | import org.bouncycastle.jcajce.provider.asymmetric.ec.KeyPairGeneratorSpi; |
14 | | -import org.bouncycastle.jce.provider.BouncyCastleProvider; |
15 | | -import org.bouncycastle.openssl.PEMException; |
16 | 13 | import org.bouncycastle.openssl.PEMParser; |
17 | 14 | import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; |
18 | 15 | import org.bouncycastle.util.io.pem.*; |
|
28 | 25 | // https://www.bouncycastle.org/latest_releases.html |
29 | 26 |
|
30 | 27 | public class ECKeyPair { |
31 | | - |
32 | 28 | private static final int SHA256_BYTES = 32; |
33 | 29 |
|
34 | | - static { |
35 | | - Security.addProvider(new BouncyCastleProvider()); |
36 | | - } |
37 | | - |
38 | 30 | private final ECCurve curve; |
39 | 31 |
|
40 | 32 | public enum ECAlgorithm { |
41 | 33 | ECDH, |
42 | 34 | ECDSA |
43 | 35 | } |
44 | 36 |
|
45 | | - private static final BouncyCastleProvider BOUNCY_CASTLE_PROVIDER = new BouncyCastleProvider(); |
46 | | - |
47 | 37 | private KeyPair keyPair; |
48 | 38 |
|
49 | 39 | public ECKeyPair() { |
@@ -140,33 +130,6 @@ private static byte[] getCompressedECPublicKey(PublicKey publicKey) { |
140 | 130 | return new X9ECPoint(p, true).getPointEncoding(); |
141 | 131 | } |
142 | 132 |
|
143 | | - public static String getPEMPublicKeyFromX509Cert(String pemInX509Format) { |
144 | | - try { |
145 | | - PEMParser parser = new PEMParser(new StringReader(pemInX509Format)); |
146 | | - X509CertificateHolder x509CertificateHolder = (X509CertificateHolder) parser.readObject(); |
147 | | - parser.close(); |
148 | | - SubjectPublicKeyInfo publicKeyInfo = x509CertificateHolder.getSubjectPublicKeyInfo(); |
149 | | - JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider(BOUNCY_CASTLE_PROVIDER); |
150 | | - ECPublicKey publicKey = null; |
151 | | - try { |
152 | | - publicKey = (ECPublicKey) converter.getPublicKey(publicKeyInfo); |
153 | | - } catch (PEMException e) { |
154 | | - throw new RuntimeException(e); |
155 | | - } |
156 | | - |
157 | | - // EC public key to pem formated. |
158 | | - StringWriter writer = new StringWriter(); |
159 | | - PemWriter pemWriter = new PemWriter(writer); |
160 | | - |
161 | | - pemWriter.writeObject(new PemObject("PUBLIC KEY", publicKey.getEncoded())); |
162 | | - pemWriter.flush(); |
163 | | - pemWriter.close(); |
164 | | - return writer.toString(); |
165 | | - } catch (IOException e) { |
166 | | - throw new RuntimeException(e); |
167 | | - } |
168 | | - } |
169 | | - |
170 | 133 | public static byte[] compressECPublickey(String pemECPubKey) { |
171 | 134 | try { |
172 | 135 | KeyFactory ecKeyFac = KeyFactory.getInstance("EC"); |
|
0 commit comments