Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ subprojects {
testAnnotationProcessor("org.projectlombok:lombok:$lombokVersion")
}

repositories {
mavenLocal()
maven(url = "https://maven.proxy.ustclug.org/maven2/")
mavenCentral()
}

tasks.withType<JavaCompile> {
options.encoding = "UTF-8"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
import java.util.UUID;

/**
* {@link AesUtil} can help you encrypt and decrypt data with specified secret
* by AES algorithm.
* {@link AesUtil} can help you encrypt and decrypt data with specified secret by AES algorithm.
*
* @author hubin@baomidou
* @version 1.1.0
Expand Down Expand Up @@ -81,8 +80,9 @@ public static byte[] decrypt(byte[] data, byte[] secret) {
var cipher = Cipher.getInstance(AES_CBC_CIPHER);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(secret));
return cipher.doFinal(data);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | UnsupportedOperationException |
InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException |
} catch (NoSuchAlgorithmException | NoSuchPaddingException |
UnsupportedOperationException | InvalidKeyException |
InvalidAlgorithmParameterException | IllegalBlockSizeException |
BadPaddingException exception) {
log.error(exception.getMessage());
for (var stackTraceElement : exception.getStackTrace()) {
Expand All @@ -100,7 +100,8 @@ public static byte[] decrypt(byte[] data, byte[] secret) {
* @return the encryption result or {@code null} if encryption failed
*/
public static String encrypt(String data, String secret) {
return Base64.getEncoder().encodeToString(encrypt(data.getBytes(StandardCharsets.UTF_8), secret.getBytes(StandardCharsets.UTF_8)));
return Base64.getEncoder().encodeToString(encrypt(data.getBytes(StandardCharsets.UTF_8),
secret.getBytes(StandardCharsets.UTF_8)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
* provided. It is recommended to specify the charset explicitly to ensure consistent
* encoding and decoding.
*
* @author Zihlu Wang
* @author zihluwang
* @version 1.1.0
* @since 1.0.0
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,9 @@ private ChainedCalcUtil operator(BiFunction<BigDecimal, BigDecimal, BigDecimal>
* or null if not applicable
* @return a ChainedCalcUtil instance with the updated value
*/
private ChainedCalcUtil operator(BiFunction<BigDecimal, BigDecimal, BigDecimal> operator, Object other, Integer beforeOperateScale) {
private ChainedCalcUtil operator(BiFunction<BigDecimal, BigDecimal, BigDecimal> operator,
Object other,
Integer beforeOperateScale) {
return baseOperator((otherValue) ->
operator.apply(this.value, otherValue),
other,
Expand All @@ -311,7 +313,8 @@ private ChainedCalcUtil operator(BiFunction<BigDecimal, BigDecimal, BigDecimal>
* @return a ChainedCalcUtil instance with the updated value
*/
private synchronized ChainedCalcUtil baseOperator(Function<BigDecimal, BigDecimal> operatorFunction,
Object anotherValue, Integer beforeOperateScale) {
Object anotherValue,
Integer beforeOperateScale) {
if (Objects.isNull(anotherValue)) {
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
* for data integrity checks and password storage, but they should not be used for
* encryption purposes.
*
* @author Zihlu Wang
* @author zihluwang
* @version 1.1.0
* @see java.security.MessageDigest
* @since 1.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@
* Note: Since version 1.4.2, this util class removed reflection API and transferred to a safer API.
* Please see documentation for more information.
*
* @author Zihlu Wang
* @author zihluwang
* @version 1.4.2
* @see com.onixbyte.devkit.utils.unsafe.ReflectMapUtil
* @since 1.0.0
*/
@Slf4j
Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
# limitations under the License.
#

jacksonVersion=2.17.0
jacksonVersion=2.17.2
javaJwtVersion=4.4.0
jjwtVersion=0.12.5
jjwtVersion=0.12.6
junitVersion=5.10.2
logbackVersion=1.5.4
lombokVersion=1.18.30
Expand All @@ -26,7 +26,7 @@ springVersion=6.1.3
springBootVersion=3.2.3

buildGroupId=com.onixbyte
buildVersion=1.5.0
buildVersion=1.6.0
projectUrl=https://onixbyte.com/JDevKit
projectGithubUrl=https://github.com/OnixByte/JDevKit
licenseName=The Apache License, Version 2.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,11 @@ val projectGithubUrl: String by project
val licenseName: String by project
val licenseUrl: String by project

val jacksonVersion: String by project
val jjwtVersion: String by project

group = buildGroupId
version = buildVersion

dependencies {
implementation(project(":devkit-utils"))
implementation(project(":guid"))
implementation("com.fasterxml.jackson.core:jackson-databind:$jacksonVersion")
implementation("io.jsonwebtoken:jjwt-api:$jjwtVersion")
implementation("io.jsonwebtoken:jjwt-impl:$jjwtVersion")
implementation("io.jsonwebtoken:jjwt-jackson:$jjwtVersion")
implementation(project(":simple-jwt-facade"))
implementation(project(":devkit-core"))
}

java {
Expand All @@ -53,14 +44,15 @@ tasks.test {

publishing {
publications {
create<MavenPublication>("simpleJwtJjwt") {
create<MavenPublication>("keyPairLoader") {
groupId = buildGroupId
artifactId = "simple-jwt-jjwt"
artifactId = "key-pair-loader"
version = buildVersion

pom {
name = "Simple JWT :: JJWT"
description = "SSimple JWT implemented with io.jsonwebtoken:jjwt."
name = "Key Pair Loader"
description =
"This module can easily load key pairs from a PEM content."
url = projectUrl

licenses {
Expand Down Expand Up @@ -89,7 +81,7 @@ publishing {
from(components["java"])

signing {
sign(publishing.publications["simpleJwtJjwt"])
sign(publishing.publications["keyPairLoader"])
}
}

Expand Down
100 changes: 100 additions & 0 deletions key-pair-loader/src/main/java/com/onixbyte/security/KeyLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (C) 2024-2024 OnixByte.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.onixbyte.security;

import com.onixbyte.security.exception.KeyLoadingException;
import lombok.extern.slf4j.Slf4j;

import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

/**
* KeyLoader can load key pairs from PEM formated content.
*
* @author zihluwang
* @version 1.6.0
* @since 1.6.0
*/
@Slf4j
public class KeyLoader {

/**
* Private constructor prevents from being initialised.
*/
private KeyLoader() {
}

/**
* Load ECDSA private key from pem-formatted key text.
*
* @param pemKeyText pem-formatted key text
* @return loaded private key
* @throws KeyLoadingException if the generated key is not a {@link ECPrivateKey} instance,
* or EC Key Factory is not loaded, or key spec is invalid
*/
public static ECPrivateKey loadEcdsaPrivateKey(String pemKeyText) {
try {
var decodedKeyString = Base64.getDecoder().decode(pemKeyText);
var keySpec = new PKCS8EncodedKeySpec(decodedKeyString);
var keyFactory = KeyFactory.getInstance("EC");
var _key = keyFactory.generatePrivate(keySpec);
if (_key instanceof ECPrivateKey privateKey) {
return privateKey;
} else {
throw new KeyLoadingException("Unable to load private key from pem-formatted key text.");
}
} catch (NoSuchAlgorithmException e) {
throw new KeyLoadingException("Cannot get EC Key Factory.", e);
} catch (InvalidKeySpecException e) {
throw new KeyLoadingException("Key spec is invalid.", e);
}
}

/**
* Load ECDSA public key from pem-formatted key text.
*
* @param pemKeyText pem-formatted key text
* @return loaded private key
* @throws KeyLoadingException if the generated key is not a {@link ECPrivateKey} instance,
* or EC Key Factory is not loaded, or key spec is invalid
*/
public static ECPublicKey loadEcdsaPublicKey(String pemKeyText) {
try {
var keyBytes = Base64.getDecoder().decode(pemKeyText);
var spec = new X509EncodedKeySpec(keyBytes);
var keyFactory = KeyFactory.getInstance("EC");
var key = keyFactory.generatePublic(spec);
if (key instanceof ECPublicKey publicKey) {
return publicKey;
} else {
throw new KeyLoadingException("Unable to load private key from pem-formatted key text.");
}
} catch (NoSuchAlgorithmException e) {
throw new KeyLoadingException("Cannot get EC Key Factory.", e);
} catch (InvalidKeySpecException e) {
throw new KeyLoadingException("Key spec is invalid.", e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (C) 2024-2024 OnixByte.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.onixbyte.security.exception;

/**
* {@code KeyLoadingException} is an exception indicating an error occurred while loading a key.
*
* @author zihluwang
* @version 1.6.0
* @since 1.6.0
*/
public class KeyLoadingException extends RuntimeException {

/**
* Creates a new instance of {@code KeyLoadingException} without a specific message or cause.
*/
public KeyLoadingException() {
}

/**
* Creates a new instance of {@code KeyLoadingException} with the specified detail message.
*
* @param message the detail message
*/
public KeyLoadingException(String message) {
super(message);
}

/**
* Creates a new instance of {@code KeyLoadingException} with the specified detail message
* and cause.
*
* @param message the detail message
* @param cause the cause of this exception
*/
public KeyLoadingException(String message, Throwable cause) {
super(message, cause);
}

/**
* Creates a new instance of {@code KeyLoadingException} with the specified cause.
*
* @param cause the cause of this exception
*/
public KeyLoadingException(Throwable cause) {
super(cause);
}

/**
* Constructs a new exception with the specified detail message, cause, suppression enabled
* or disabled, and writable stack trace enabled or disabled.
*
* @param message the detail message
* @param cause the cause of this exception
* @param enableSuppression whether suppression is enabled or disabled
* @param writableStackTrace whether the stack trace should be writable
*/
public KeyLoadingException(String message,
Throwable cause,
boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (C) 2023 CodeCraftersCN.
~ Copyright (C) 2024-2024 OnixByte.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2024-2024 OnixByte.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.onixbyte.security;

import org.junit.jupiter.api.Test;

public class KeyPairLoaderTest {

@Test
public void test() {

}

}
Loading