Skip to content

Commit 7e4fdd5

Browse files
author
zihluwang
committed
feat: load ECDSA key pairs with pem-formatted text
1 parent f29be80 commit 7e4fdd5

File tree

4 files changed

+102
-76
lines changed

4 files changed

+102
-76
lines changed

key-pair-loader/src/main/java/com/onixbyte/keypairloader/KeyLoader.java

Lines changed: 0 additions & 74 deletions
This file was deleted.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright (C) 2024-2024 OnixByte.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
*
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.onixbyte.security;
19+
20+
import com.onixbyte.security.exception.KeyLoadingException;
21+
import lombok.extern.slf4j.Slf4j;
22+
23+
import java.security.KeyFactory;
24+
import java.security.NoSuchAlgorithmException;
25+
import java.security.interfaces.ECPrivateKey;
26+
import java.security.interfaces.ECPublicKey;
27+
import java.security.spec.InvalidKeySpecException;
28+
import java.security.spec.PKCS8EncodedKeySpec;
29+
import java.security.spec.X509EncodedKeySpec;
30+
import java.util.Base64;
31+
32+
/**
33+
* KeyLoader can load key pairs from PEM formated content.
34+
*
35+
* @author zihluwang
36+
* @version 1.6.0
37+
* @since 1.6.0
38+
*/
39+
@Slf4j
40+
public class KeyLoader {
41+
42+
/**
43+
* Private constructor prevents from being initialised.
44+
*/
45+
private KeyLoader() {
46+
}
47+
48+
/**
49+
* Load ECDSA private key from pem-formatted key text.
50+
*
51+
* @param pemKeyText pem-formatted key text
52+
* @return loaded private key
53+
* @throws KeyLoadingException if the generated key is not a {@link ECPrivateKey} instance, or EC Key Factory is
54+
* not loaded, or key spec is invalid
55+
*/
56+
public ECPrivateKey loadEcdsaPrivateKey(String pemKeyText) {
57+
try {
58+
var decodedKeyString = Base64.getDecoder().decode(pemKeyText);
59+
var keySpec = new PKCS8EncodedKeySpec(decodedKeyString);
60+
var keyFactory = KeyFactory.getInstance("EC");
61+
var _key = keyFactory.generatePrivate(keySpec);
62+
if (_key instanceof ECPrivateKey privateKey) {
63+
return privateKey;
64+
} else {
65+
throw new KeyLoadingException("Unable to load private key from pem-formatted key text.");
66+
}
67+
} catch (NoSuchAlgorithmException e) {
68+
throw new KeyLoadingException("Cannot get EC Key Factory.", e);
69+
} catch (InvalidKeySpecException e) {
70+
throw new KeyLoadingException("Key spec is invalid.", e);
71+
}
72+
}
73+
74+
/**
75+
* Load ECDSA public key from pem-formatted key text.
76+
*
77+
* @param pemKeyText pem-formatted key text
78+
* @return loaded private key
79+
* @throws KeyLoadingException if the generated key is not a {@link ECPrivateKey} instance, or EC Key Factory is
80+
* not loaded, or key spec is invalid
81+
*/
82+
public ECPublicKey loadEcdsaPublicKey(String pemKeyText) {
83+
try {
84+
var keyBytes = Base64.getDecoder().decode(pemKeyText);
85+
var spec = new X509EncodedKeySpec(keyBytes);
86+
var keyFactory = KeyFactory.getInstance("EC");
87+
var key = keyFactory.generatePublic(spec);
88+
if (key instanceof ECPublicKey publicKey) {
89+
return publicKey;
90+
} else {
91+
throw new KeyLoadingException("Unable to load private key from pem-formatted key text.");
92+
}
93+
} catch (NoSuchAlgorithmException e) {
94+
throw new KeyLoadingException("Cannot get EC Key Factory.", e);
95+
} catch (InvalidKeySpecException e) {
96+
throw new KeyLoadingException("Key spec is invalid.", e);
97+
}
98+
}
99+
100+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
package com.onixbyte.keypairloader.exception;
18+
package com.onixbyte.security.exception;
1919

2020
public class KeyLoadingException extends RuntimeException {
2121

key-pair-loader/src/test/java/com/onixbyte/keypairloader/KeyPairLoaderTest.java renamed to key-pair-loader/src/test/java/com/onixbyte/security/KeyPairLoaderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
package com.onixbyte.keypairloader;
18+
package com.onixbyte.security;
1919

2020
import org.junit.jupiter.api.Test;
2121

0 commit comments

Comments
 (0)