Skip to content

Commit

Permalink
moved example classes to the KeyToolFactoryTest class and delete them
Browse files Browse the repository at this point in the history
- organized imports and formatted with spotless
  • Loading branch information
astrapisixtynine committed Jul 13, 2024
1 parent d470bde commit 821643f
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 305 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void testCreateKeyStoreAndTrustStoreWithInfoObjects() throws Exception
privateKey = keyPair.getPrivate();

// Initialize a KeyStore and store the key pair and certificate
keystoreFile = FileFactory.newFile(PathFinder.getSrcTestResourcesDir(), "new-keystore.jks");
keystoreFile = FileFactory.newFile(PathFinder.getSrcTestResourcesDir(), "ssl-keystore.jks");
keystoreFileInfo = FileInfo.toFileInfo(keystoreFile);
keyStoreInfo = KeyStoreInfo.builder().fileInfo(keystoreFileInfo).type("JKS")
.keystorePassword(password).build();
Expand All @@ -107,7 +107,7 @@ public void testCreateKeyStoreAndTrustStoreWithInfoObjects() throws Exception

// Initialize a KeyStore for the truststore and store the key pair and certificate
trustStoreFile = FileFactory.newFile(PathFinder.getSrcTestResourcesDir(),
"new-truststore.jks");
"ssl-truststore.jks");
trustStoreFileInfo = FileInfo.toFileInfo(trustStoreFile);
KeyStoreInfo trustStoreInfo = KeyStoreInfo.builder().fileInfo(trustStoreFileInfo)
.type("JKS").keystorePassword(password).build();
Expand Down Expand Up @@ -170,7 +170,7 @@ public void testCreateKeyStoreAndTrustStore() throws Exception
cert = CertFactory.newX509CertificateV3(keyPair, issuer, serial, notBefore, notAfter,
subject, signatureAlgorithm);

keystoreFile = FileFactory.newFile(PathFinder.getSrcTestResourcesDir(), "new-keystore.jks");
keystoreFile = FileFactory.newFile(PathFinder.getSrcTestResourcesDir(), "ssl-keystore.jks");

// Initialize a KeyStore and store the key pair and certificate
keyStore = KeyStoreFactory.newKeyStore(keystoreFile, "JKS", "password");
Expand All @@ -180,7 +180,7 @@ public void testCreateKeyStoreAndTrustStore() throws Exception
KeyStoreExtensions.store(keyStore, keystoreFile, "password");

trustStoreFile = FileFactory.newFile(PathFinder.getSrcTestResourcesDir(),
"new-truststore.jks");
"ssl-truststore.jks");

// Initialize a KeyStore for the truststore and store the key pair and certificate
trustStore = KeyStoreFactory.newKeyStore(trustStoreFile, "JKS", "password");
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,12 @@ public static void main(String[] args) throws Exception
{

char[] password;

password = "password".toCharArray();

// File keystoreFile = FileFactory.newFile(PathFinder.getSrcTestResourcesDir(),
// "keystore.jks");
// File trustStoreFile = FileFactory.newFile(PathFinder.getSrcTestResourcesDir(),
// "truststore.jks");
File keystoreFile = FileFactory.newFile(PathFinder.getSrcTestResourcesDir(),
"new-keystore.jks");
"ssl-keystore.jks");
File trustStoreFile = FileFactory.newFile(PathFinder.getSrcTestResourcesDir(),
"new-truststore.jks");
"ssl-truststore.jks");
// Step 2: Load KeyStore and TrustStore
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(new FileInputStream(keystoreFile), password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,11 @@ public class SecureServer
public static void main(String[] args) throws Exception
{
char[] password;
// File keystoreFile = FileFactory.newFile(PathFinder.getSrcTestResourcesDir(),
// "keystore.jks");
// File trustStoreFile = FileFactory.newFile(PathFinder.getSrcTestResourcesDir(),
// "truststore.jks");

File keystoreFile = FileFactory.newFile(PathFinder.getSrcTestResourcesDir(),
"new-keystore.jks");
"ssl-keystore.jks");
File trustStoreFile = FileFactory.newFile(PathFinder.getSrcTestResourcesDir(),
"new-truststore.jks");
"ssl-truststore.jks");
// Step 2: Load KeyStore and TrustStore
KeyStore keyStore = KeyStore.getInstance("JKS");
password = "password".toCharArray();
Expand All @@ -91,28 +88,30 @@ public static void main(String[] args) throws Exception
// Step 5: Use SSLContext for Secure Communication

SSLServerSocketFactory ssf = sslContext.getServerSocketFactory();
SSLServerSocket sss = (SSLServerSocket)ssf.createServerSocket(8443);
try (SSLServerSocket sss = (SSLServerSocket)ssf.createServerSocket(8443))
{

System.out.println("Secure server started. Waiting for connections...");
System.out.println("Secure server started. Waiting for connections...");

while (true)
{
try (SSLSocket socket = (SSLSocket)sss.accept();
var out = socket.getOutputStream();
var in = socket.getInputStream())
while (true)
{
System.out.println("Client connected");
try (SSLSocket socket = (SSLSocket)sss.accept();
var out = socket.getOutputStream();
var in = socket.getInputStream())
{
System.out.println("Client connected");

// Read client message
byte[] buffer = new byte[1024];
int bytesRead = in.read(buffer);
String clientMessage = new String(buffer, 0, bytesRead);
System.out.println("Received from client: " + clientMessage);
// Read client message
byte[] buffer = new byte[1024];
int bytesRead = in.read(buffer);
String clientMessage = new String(buffer, 0, bytesRead);
System.out.println("Received from client: " + clientMessage);

// Send response to client
String response = "Hello from secure server!";
out.write(response.getBytes());
out.flush();
// Send response to client
String response = "Hello from secure server!";
out.write(response.getBytes());
out.flush();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package io.github.astrapi69.mystic.crypt.key.agreement;

// Step 1: Import Necessary Libraries

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
package io.github.astrapi69.mystic.crypt.pw;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.nio.charset.StandardCharsets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
package io.github.astrapi69.mystic.crypt.pw;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.nio.charset.StandardCharsets;
Expand Down
Loading

0 comments on commit 821643f

Please sign in to comment.