Skip to content

Commit f29be80

Browse files
committed
feat: added key pair loader
1 parent cc5ed4b commit f29be80

File tree

8 files changed

+424
-41
lines changed

8 files changed

+424
-41
lines changed

key-pair-loader/build.gradle.kts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
import java.net.URI
19+
20+
val buildGroupId: String by project
21+
val buildVersion: String by project
22+
val projectUrl: String by project
23+
val projectGithubUrl: String by project
24+
val licenseName: String by project
25+
val licenseUrl: String by project
26+
27+
group = buildGroupId
28+
version = buildVersion
29+
30+
dependencies {
31+
implementation(project(":devkit-core"))
32+
}
33+
34+
java {
35+
sourceCompatibility = JavaVersion.VERSION_17
36+
targetCompatibility = JavaVersion.VERSION_17
37+
withSourcesJar()
38+
withJavadocJar()
39+
}
40+
41+
tasks.test {
42+
useJUnitPlatform()
43+
}
44+
45+
publishing {
46+
publications {
47+
create<MavenPublication>("keyPairLoader") {
48+
groupId = buildGroupId
49+
artifactId = "key-pair-loader"
50+
version = buildVersion
51+
52+
pom {
53+
name = "Key Pair Loader"
54+
description =
55+
"This module can easily load key pairs from a PEM content."
56+
url = projectUrl
57+
58+
licenses {
59+
license {
60+
name = licenseName
61+
url = licenseUrl
62+
}
63+
}
64+
65+
scm {
66+
connection = "scm:git:git://github.com:OnixByte/JDevKit.git"
67+
developerConnection = "scm:git:git://github.com:OnixByte/JDevKit.git"
68+
url = projectGithubUrl
69+
}
70+
71+
developers {
72+
developer {
73+
id = "zihluwang"
74+
name = "Zihlu Wang"
75+
email = "really@zihlu.wang"
76+
timezone = "Asia/Hong_Kong"
77+
}
78+
}
79+
}
80+
81+
from(components["java"])
82+
83+
signing {
84+
sign(publishing.publications["keyPairLoader"])
85+
}
86+
}
87+
88+
repositories {
89+
maven {
90+
name = "sonatypeNexus"
91+
url = URI(providers.gradleProperty("repo.maven-central.host").get())
92+
credentials {
93+
username = providers.gradleProperty("repo.maven-central.username").get()
94+
password = providers.gradleProperty("repo.maven-central.password").get()
95+
}
96+
}
97+
}
98+
}
99+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.keypairloader;
19+
20+
import com.onixbyte.keypairloader.exception.KeyLoadingException;
21+
import lombok.extern.slf4j.Slf4j;
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
24+
25+
import java.security.KeyFactory;
26+
import java.security.NoSuchAlgorithmException;
27+
import java.security.interfaces.ECPrivateKey;
28+
import java.security.interfaces.ECPublicKey;
29+
import java.security.spec.InvalidKeySpecException;
30+
import java.security.spec.PKCS8EncodedKeySpec;
31+
import java.util.Base64;
32+
import java.util.Optional;
33+
34+
/**
35+
* KeyLoader can load key pairs from PEM formated content.
36+
*
37+
* @author zihluwang
38+
* @version 1.6.0
39+
* @since 1.6.0
40+
*/
41+
@Slf4j
42+
public class KeyLoader {
43+
44+
/**
45+
* Private constructor prevents from being initialised.
46+
*/
47+
private KeyLoader() {
48+
}
49+
50+
public ECPrivateKey loadEcdsaPrivateKey(String pemKeyText) {
51+
return Optional.ofNullable(pemKeyText)
52+
.map(Base64.getDecoder()::decode)
53+
.map(PKCS8EncodedKeySpec::new)
54+
.map((keySpec) -> {
55+
try {
56+
var kf = KeyFactory.getInstance("EC");
57+
return kf.generatePrivate(keySpec);
58+
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
59+
throw new KeyLoadingException("Unable to load key from text.", e);
60+
}
61+
}).map((publicKey) -> {
62+
if (publicKey instanceof ECPrivateKey privateKey) {
63+
return privateKey;
64+
}
65+
return null;
66+
})
67+
.orElse(null);
68+
}
69+
70+
// public ECPublicKey loadEcdsaPublicKey(String pemKeyText) {
71+
//
72+
// }
73+
74+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.keypairloader.exception;
19+
20+
public class KeyLoadingException extends RuntimeException {
21+
22+
public KeyLoadingException() {
23+
}
24+
25+
public KeyLoadingException(String message) {
26+
super(message);
27+
}
28+
29+
public KeyLoadingException(String message, Throwable cause) {
30+
super(message, cause);
31+
}
32+
33+
public KeyLoadingException(Throwable cause) {
34+
super(cause);
35+
}
36+
37+
public KeyLoadingException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
38+
super(message, cause, enableSuppression, writableStackTrace);
39+
}
40+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright (C) 2024-2024 OnixByte.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
19+
<configuration>
20+
<property name="COLOURFUL_OUTPUT" value="%black(%date{'dd MMM, yyyy HH:mm:ss', Asia/Hong_Kong, en-UK}) %highlight(%-5level) %black(---) %black([%10.10t]) %cyan(%-20.20logger{20}) %black(:) %msg%n"/>
21+
<property name="STANDARD_OUTPUT" value="%date{'dd MMM, yyyy HH:mm:ss', Asia/Hong_Kong, en-UK} %-5level %black(---) [%10.10t] %-20.20logger{20} : %msg%n"/>
22+
23+
<statusListener class="ch.qos.logback.core.status.NopStatusListener" />
24+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
25+
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
26+
<pattern>${COLOURFUL_OUTPUT}</pattern>
27+
</encoder>
28+
</appender>
29+
<root level="INFO">
30+
<appender-ref ref="STDOUT"/>
31+
</root>
32+
</configuration>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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.keypairloader;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
public class KeyPairLoaderTest {
23+
24+
@Test
25+
public void test() {
26+
27+
}
28+
29+
}

map-util-unsafe/build.gradle.kts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
import java.net.URI
19+
20+
val buildGroupId: String by project
21+
val buildVersion: String by project
22+
val projectUrl: String by project
23+
val projectGithubUrl: String by project
24+
val licenseName: String by project
25+
val licenseUrl: String by project
26+
27+
group = buildGroupId
28+
version = buildVersion
29+
30+
dependencies {
31+
implementation(project(":devkit-core"))
32+
}
33+
34+
java {
35+
sourceCompatibility = JavaVersion.VERSION_17
36+
targetCompatibility = JavaVersion.VERSION_17
37+
withSourcesJar()
38+
withJavadocJar()
39+
}
40+
41+
tasks.test {
42+
useJUnitPlatform()
43+
}
44+
45+
publishing {
46+
publications {
47+
create<MavenPublication>("mapUtilUnsafe") {
48+
groupId = buildGroupId
49+
artifactId = "map-util-unsafe"
50+
version = buildVersion
51+
52+
pom {
53+
name = "Unsafe Map Util"
54+
description =
55+
"This module is a converter that can convert an object to a map with unsafe methods."
56+
url = projectUrl
57+
58+
licenses {
59+
license {
60+
name = licenseName
61+
url = licenseUrl
62+
}
63+
}
64+
65+
scm {
66+
connection = "scm:git:git://github.com:OnixByte/JDevKit.git"
67+
developerConnection = "scm:git:git://github.com:OnixByte/JDevKit.git"
68+
url = projectGithubUrl
69+
}
70+
71+
developers {
72+
developer {
73+
id = "zihluwang"
74+
name = "Zihlu Wang"
75+
email = "really@zihlu.wang"
76+
timezone = "Asia/Hong_Kong"
77+
}
78+
}
79+
}
80+
81+
from(components["java"])
82+
83+
signing {
84+
sign(publishing.publications["mapUtilUnsafe"])
85+
}
86+
}
87+
88+
repositories {
89+
maven {
90+
name = "sonatypeNexus"
91+
url = URI(providers.gradleProperty("repo.maven-central.host").get())
92+
credentials {
93+
username = providers.gradleProperty("repo.maven-central.username").get()
94+
password = providers.gradleProperty("repo.maven-central.password").get()
95+
}
96+
}
97+
}
98+
}
99+
}

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ include(
2929
"property-guard-spring-boot-starter"
3030
)
3131
include("map-util-unsafe")
32+
include("key-pair-loader")

0 commit comments

Comments
 (0)