- update jwt

- add security
This commit is contained in:
2024-01-04 01:15:47 +09:00
parent e41240ccb2
commit 1f2b1607c8
12 changed files with 248 additions and 20 deletions

3
security/README.md Normal file
View File

@@ -0,0 +1,3 @@
# Security
https://github.com/jwtk/jjwt

14
security/build.gradle.kts Normal file
View File

@@ -0,0 +1,14 @@
/*
* Examples for Java
*
* Copyright (c) 2023. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
plugins {
id("elex-java")
}
dependencies {
}

20
security/logback.xml Normal file
View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Examples for Java
~
~ Copyright (c) 2021. Elex. All Rights Reserved.
~ https://www.elex-project.com/
-->
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="TRACE">
<appender-ref ref="CONSOLE" />
</root>
</configuration>

View File

@@ -0,0 +1,45 @@
/*
* Copyright (c) 2024. Elex. All Rights Reesrved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.util.Base64;
public class AESKeys {
public static SecretKey generateSecretKey() throws NoSuchAlgorithmException {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128, new SecureRandom());
return keyGenerator.generateKey();
}
public static SecretKey generateSecretKey(char[] password, byte[] salt, int iteration) throws NoSuchAlgorithmException, InvalidKeySpecException {
PBEKeySpec keySpec = new PBEKeySpec(password, salt, iteration);
return SecretKeyFactory.getInstance("PBEWithHmacSHA256AndAES_128")
.generateSecret(keySpec);
}
public static String encodeSecretKey(final SecretKey secretKey){
return Base64.getEncoder().encodeToString(secretKey.getEncoded());
}
public static SecretKey decodeSecretKey(final String secretKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
SecretKey keySpec = new SecretKeySpec(Base64.getDecoder().decode(secretKey), "AES");
return keySpec;
}
public static IvParameterSpec generateIV(byte[] iv){
IvParameterSpec parameterSpec = new IvParameterSpec(iv);
return parameterSpec;
}
}

View File

@@ -0,0 +1,44 @@
/*
* Examples for Java
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import lombok.extern.slf4j.Slf4j;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
@Slf4j
public class RSAKeys {
public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(2048, new SecureRandom());
return keyPairGen.generateKeyPair();
}
public static String encodePrivateKey(final PrivateKey privateKey) {
return Base64.getEncoder().encodeToString(privateKey.getEncoded());
}
public static String encodePublicKey(final PublicKey publicKey) {
return Base64.getEncoder().encodeToString(publicKey.getEncoded());
}
public static PublicKey decodePublicKey(final String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(publicKey));
return KeyFactory.getInstance("RSA").generatePublic(keySpec);
}
public static PrivateKey decodePrivateKey(final String privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(privateKey));
return KeyFactory.getInstance("RSA").generatePrivate(keySpec);
}
}

View File

@@ -0,0 +1,8 @@
/*
* Examples for Java
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;

View File

@@ -0,0 +1,40 @@
/*
* Copyright (c) 2024. Elex. All Rights Reesrved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import org.junit.jupiter.api.Test;
import javax.crypto.SecretKey;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Random;
import static org.junit.jupiter.api.Assertions.*;
class AESKeysTest {
@Test
void test() throws NoSuchAlgorithmException, InvalidKeySpecException {
SecretKey secretKey = AESKeys.generateSecretKey();
String base64SecretKey = AESKeys.encodeSecretKey(secretKey);
SecretKey secretKey1 = AESKeys.decodeSecretKey(base64SecretKey);
assertEquals(secretKey, secretKey1);
}
@Test
void test1() throws NoSuchAlgorithmException, InvalidKeySpecException {
byte[] salt = new byte[8];
new Random().nextBytes(salt);
SecretKey secretKey = AESKeys.generateSecretKey("Hello".toCharArray(), salt, 10);
SecretKey secretKey1 = AESKeys.generateSecretKey("Hello".toCharArray(), salt, 10);
assertEquals(secretKey, secretKey1);
}
}

View File

@@ -0,0 +1,21 @@
/*
* Examples for Java
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import org.junit.jupiter.api.Test;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
class SampleTest {
@Test
void test() throws NoSuchAlgorithmException, InvalidKeySpecException {
}
}