Skip to content

Commit

Permalink
java: move to security
Browse files Browse the repository at this point in the history
  • Loading branch information
l-lin committed Jun 26, 2024
1 parent d2bbb45 commit d758e4a
Show file tree
Hide file tree
Showing 39 changed files with 186 additions and 218 deletions.
108 changes: 9 additions & 99 deletions java/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>lin.louis</groupId>
Expand All @@ -11,109 +12,18 @@

<modules>
<module>logging-with-aop</module>
<module>custom-protocol</module>
<module>data-structures</module>
<module>reactive-logging</module>
<module>jsr303</module>
<module>security</module>
</modules>

<properties>
<java.version>21</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<revision>1.0-SNAPSHOT</revision>

<!-- DEPENDENCIES -->
<bouncycastle.jdk15on.version>1.69</bouncycastle.jdk15on.version>
<commons-beanutils.version>1.9.4</commons-beanutils.version>
<commons-lang3.version>3.12.0</commons-lang3.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<!-- <dependencies> -->
<!-- COMMON -->
<!-- <dependency> -->
<!-- <groupId>org.projectlombok</groupId> -->
<!-- <artifactId>lombok</artifactId> -->
<!-- <scope>provided</scope> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>commons-beanutils</groupId> -->
<!-- <artifactId>commons-beanutils</artifactId> -->
<!-- <version>${commons-beanutils.version}</version> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>org.apache.commons</groupId> -->
<!-- <artifactId>commons-lang3</artifactId> -->
<!-- <version>${commons-lang3.version}</version> -->
<!-- </dependency> -->
<!---->
<!-- SECURITY -->
<!-- <dependency> -->
<!-- <groupId>org.bouncycastle</groupId> -->
<!-- <artifactId>bcprov-ext-jdk15on</artifactId> -->
<!-- <version>${bouncycastle.jdk15on.version}</version> -->
<!-- </dependency> -->
<!---->
<!-- VALIDATION -->
<!-- <dependency> -->
<!-- <groupId>javax.validation</groupId> -->
<!-- <artifactId>validation-api</artifactId> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>org.hibernate.validator</groupId> -->
<!-- <artifactId>hibernate-validator</artifactId> -->
<!-- </dependency> -->
<!---->
<!-- REACTIVE -->
<!-- <dependency> -->
<!-- <groupId>io.projectreactor</groupId> -->
<!-- <artifactId>reactor-core</artifactId> -->
<!-- </dependency> -->
<!---->
<!-- SPRING -->
<!-- <dependency> -->
<!-- <groupId>org.springframework.boot</groupId> -->
<!-- <artifactId>spring-boot-starter-aop</artifactId> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>org.springframework.boot</groupId> -->
<!-- <artifactId>spring-boot-starter</artifactId> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>org.springframework.boot</groupId> -->
<!-- <artifactId>spring-boot-starter-web</artifactId> -->
<!-- </dependency> -->
<!---->
<!-- TEST -->
<!-- <dependency> -->
<!-- <groupId>org.springframework.boot</groupId> -->
<!-- <artifactId>spring-boot-starter-test</artifactId> -->
<!-- <scope>test</scope> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>io.projectreactor</groupId> -->
<!-- <artifactId>reactor-test</artifactId> -->
<!-- <scope>test</scope> -->
<!-- </dependency> -->
<!-- </dependencies> -->
<!---->
<!-- <build> -->
<!-- <plugins> -->
<!-- <plugin> -->
<!-- <groupId>org.apache.maven.plugins</groupId> -->
<!-- <artifactId>maven-compiler-plugin</artifactId> -->
<!-- <configuration> -->
<!-- <source>${java.version}</source> -->
<!-- <target>${java.version}</target> -->
<!-- </configuration> -->
<!-- </plugin> -->
<!-- <plugin> -->
<!-- <groupId>org.apache.maven.plugins</groupId> -->
<!-- <artifactId>maven-surefire-plugin</artifactId> -->
<!-- <configuration> -->
<!-- <includes> -->
<!-- <include>**/*</include> -->
<!-- </includes> -->
<!-- </configuration> -->
<!-- </plugin> -->
<!-- </plugins> -->
<!-- </build> -->
</project>
57 changes: 57 additions & 0 deletions java/security/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>dev-cheat-sheet</artifactId>
<groupId>lin.louis</groupId>
<version>${revision}</version>
</parent>

<artifactId>security</artifactId>

<properties>
<bouncycastle.jdk15on.version>1.69</bouncycastle.jdk15on.version>
<junit.version>5.10.2</junit.version>
<logback.version>1.5.6</logback.version>
<lombok.version>1.18.32</lombok.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>${junit.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-ext-jdk15on</artifactId>
<version>${bouncycastle.jdk15on.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lin.louis.encryption;
package lin.louis.security.encryption;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lin.louis.encryption;
package lin.louis.security.encryption;

import java.io.IOException;
import java.io.InputStream;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lin.louis.encryption;
package lin.louis.security.encryption;

import java.io.IOException;
import java.io.InputStream;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lin.louis.encryption;
package lin.louis.security.encryption;

import java.security.SecureRandom;
import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lin.louis.hash;
package lin.louis.security.hash;

import java.io.FilterInputStream;
import java.io.InputStream;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lin.louis.hash;
package lin.louis.security.hash;

import java.io.FilterInputStream;
import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lin.louis.password;
package lin.louis.security.password;

import java.util.Arrays;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lin.louis.password;
package lin.louis.security.password;

public interface PasswordBuilder {
Password build(char[] passwordValue, byte[] salt);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package lin.louis.password;
package lin.louis.security.password;

import lin.louis.password.plain.PlainPasswordBuilder;
import lin.louis.password.scrypt.SCryptPasswordBuilder;
import lin.louis.password.sha256.Sha256PasswordBuilder;
import lin.louis.security.password.plain.PlainPasswordBuilder;
import lin.louis.security.password.scrypt.SCryptPasswordBuilder;
import lin.louis.security.password.sha256.Sha256PasswordBuilder;


class PasswordFactory {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lin.louis.password;
package lin.louis.security.password;

import java.security.SecureRandom;
import java.util.function.Supplier;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package lin.louis.password.plain;
package lin.louis.security.password.plain;

import lin.louis.password.Password;
import lin.louis.password.PasswordBuilder;
import lin.louis.security.password.Password;
import lin.louis.security.password.PasswordBuilder;


public class PlainPasswordBuilder implements PasswordBuilder {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package lin.louis.password.scrypt;
package lin.louis.security.password.scrypt;

import java.nio.CharBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

import org.bouncycastle.crypto.generators.SCrypt;

import lin.louis.password.Password;
import lin.louis.password.PasswordBuilder;
import lin.louis.security.password.Password;
import lin.louis.security.password.PasswordBuilder;


public class SCryptPasswordBuilder implements PasswordBuilder {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
package lin.louis.password.sha256;
package lin.louis.security.password.sha256;

import java.nio.CharBuffer;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import lin.louis.password.Password;
import lin.louis.password.PasswordBuilder;

import lin.louis.security.password.Password;
import lin.louis.security.password.PasswordBuilder;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Sha256PasswordBuilder implements PasswordBuilder {

private static final Logger LOGGER = LoggerFactory.getLogger(Sha256PasswordBuilder.class);

private static final MessageDigest DIGEST;

static {
try {
DIGEST = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
LOGGER.error(e.getMessage(), e);
log.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lin.louis.sign;
package lin.louis.security.sign;

public enum HashAlgo {
SHA256("SHA-256");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package lin.louis.sign;
package lin.louis.security.sign;

import java.security.Key;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiPredicate;

import lin.louis.sign.HashAlgo;
import lin.louis.sign.supplier.Sha256WithEcdsaSignatureSupplier;
import lin.louis.sign.supplier.Sha256WithRsaSignatureSupplier;
import lin.louis.sign.supplier.SignatureSupplier;
import lin.louis.sign.supplier.SignatureSupplierFactory;
import lin.louis.security.sign.supplier.Sha256WithEcdsaSignatureSupplier;
import lin.louis.security.sign.supplier.Sha256WithRsaSignatureSupplier;
import lin.louis.security.sign.supplier.SignatureSupplier;
import lin.louis.security.sign.supplier.SignatureSupplierFactory;


public class TestFactories {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package lin.louis.sign;
package lin.louis.security.sign;

public class X509Certificates {
// Content between the -----BEGIN RSA CERTIFICATE----- and -----END RSA CERTIFICATE----- of the private part of the certificate
// of the file https://github.com/l-lin/dev-cheat-sheet/blob/master/java/tomcat-sample-cert-auth/foobar/certificate/foobarCA.key
public static final String PRIVATE_KEY = "MIICXQIBAAKBgQDWCOCt8f72/PE4dfEhaDZY6bjue3MFu7LbSOr3dTGHrPXlgAA6j+ATct6Rb8eBcxvPhqKGwv0Ho+dygT78K6xZmze7n27aY3Dln2MS9f9uS7AoElQRQxgqmuXPS1VRi3YBOdqk4z2D/+cH0/FACUFpGc5fuECIWFiugK/IhgWkGQIDAQABAoGANG1GQ7VUI8G/gHn7T5iMP2k4oEni2dOpMueAjo7JTBeEv+uDotSdKYZomC1OLBo7BLFQ3Duk6Rsv1S9tcy1rcLDIhyxLjkX7SbU7pRQmsiTMKbxoyP0Fck0TORp9M6I+u22LrokJ10/KMcV5dg9e5wRiqSvKTVaTzqA33VAke1ECQQDw8Jtc1M5nCDRP5k+gCwTnuXYV9yuAw+nNy/hinRMdJafXcjy0hfDG6jKKSIv6ulobKlrbQ8YIC+4WAQtok9YFAkEA42m/ftrAWxw7GgrxbSVA0CDiCpyBIrDil8f606H2tZeG0EAPL45o668AgoXdvs7pDUDh6nBwE3WCZdOp9lx+BQJBAMPu9nj8eckhy+C560C8FUYX9OaR9Semqkh4Ocp/795BFAfJV4J6db5dD7KSonrH9qSmwfITYESE5x2vxcZKir0CQQCcqO2VWuamHSWNxDoaoU4r0mtFOhkvp8EBJG9jOTD2WcMyVN7hOO6IZY8pW0StvGYJjkfTM8/RZ+MDeLOeFottAkAxMWFGB+HwF5NZdNzwq9Q4DLu/geI/H9HBejAigHnNUfTInFWxlJOoGQuHwTmjf1G48NicXya8IsLsPgBvHs8w";
// Content between the -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- of the private part of the certificate
// of the file https://github.com/l-lin/dev-cheat-sheet/blob/master/java/tomcat-sample-cert-auth/foobar/certificate/foobarCA.crt
public static final String PUBLIC_KEY = "MIICdDCCAd2gAwIBAgIJAIjyT+oKZ7UKMA0GCSqGSIb3DQEBBQUAMFMxCzAJBgNVBAYTAkZSMQwwCgYDVQQIDANJREYxDjAMBgNVBAcMBVBhcmlzMREwDwYDVQQKDAhGb29iYXJDQTETMBEGA1UEAwwKKi5sb2NhbC5mcjAeFw0xNDA4MTQxNjE3MjlaFw0yNDA4MTExNjE3MjlaMFMxCzAJBgNVBAYTAkZSMQwwCgYDVQQIDANJREYxDjAMBgNVBAcMBVBhcmlzMREwDwYDVQQKDAhGb29iYXJDQTETMBEGA1UEAwwKKi5sb2NhbC5mcjCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA1gjgrfH+9vzxOHXxIWg2WOm47ntzBbuy20jq93Uxh6z15YAAOo/gE3LekW/HgXMbz4aihsL9B6PncoE+/CusWZs3u59u2mNw5Z9jEvX/bkuwKBJUEUMYKprlz0tVUYt2ATnapOM9g//nB9PxQAlBaRnOX7hAiFhYroCvyIYFpBkCAwEAAaNQME4wHQYDVR0OBBYEFDttawgSlrgwwc92/asJh/JfL2bzMB8GA1UdIwQYMBaAFDttawgSlrgwwc92/asJh/JfL2bzMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADgYEArrGXPUJ11XnjsRAjG3L8hQS/ncnK1wFD6uVJTIntt8jHSCe7VyRkdYtskBgvHmaW7v6YOmaZ5KpN4Ii7cH+Lf4OEXO76IDFBWfknplp54+io+qbgGDTjZoQyiw25QbQgJQbDeKJUpQ8d0cdO05XXgv9eIy6lvD9iHc+UxJOtktA=";
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package lin.louis.sign.signer;
package lin.louis.security.sign.signer;

import java.security.GeneralSecurityException;

import lin.louis.sign.HashAlgo;
import lin.louis.security.sign.HashAlgo;


public interface Signer {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lin.louis.sign.signer;
package lin.louis.security.sign.signer;

import java.security.GeneralSecurityException;
import java.security.KeyFactory;
Expand All @@ -9,8 +9,8 @@

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import lin.louis.sign.HashAlgo;
import lin.louis.sign.supplier.SignatureSupplierFactory;
import lin.louis.security.sign.HashAlgo;
import lin.louis.security.sign.supplier.SignatureSupplierFactory;


public class X509Signer implements Signer {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lin.louis.sign.supplier;
package lin.louis.security.sign.supplier;

import java.security.GeneralSecurityException;
import java.security.Key;
Expand All @@ -10,7 +10,7 @@

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import lin.louis.sign.HashAlgo;
import lin.louis.security.sign.HashAlgo;


public class Sha256WithEcdsaSignatureSupplier implements SignatureSupplier {
Expand Down
Loading

0 comments on commit d758e4a

Please sign in to comment.