Skip to content

Commit 19d5541

Browse files
committed
refactor project
1 parent e0a5ab1 commit 19d5541

File tree

10 files changed

+154
-53
lines changed

10 files changed

+154
-53
lines changed

src/main/java/pl/nightdev701/OpenAPI.java

+37-12
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@
1111
*/
1212

1313
import pl.nightdev701.base.BaseKey;
14-
import pl.nightdev701.crypto.CryptoManager;
1514
import pl.nightdev701.database.DatabaseConnector;
1615
import pl.nightdev701.database.formular.DatabaseFormular;
1716
import pl.nightdev701.database.type.DatabaseType;
1817
import pl.nightdev701.key.UniqueValueKey;
1918
import pl.nightdev701.key.ValueKey;
2019
import pl.nightdev701.logger.AbstractLogger;
20+
import pl.nightdev701.logger.standard.DefaultLogger;
21+
import pl.nightdev701.manager.CryptManager;
2122
import pl.nightdev701.network.http.HttpRequestHandler;
2223
import pl.nightdev701.network.tcp.ProxyAdapter;
24+
import pl.nightdev701.util.crypto.CryptType;
2325
import pl.nightdev701.util.stream.OpenPrintStream;
2426

2527
import java.io.FileNotFoundException;
@@ -34,20 +36,43 @@ public class OpenAPI {
3436
/**
3537
* encrypt and decrypt strings
3638
*
37-
* @param value
39+
* @param key
40+
* @param type
3841
*/
39-
public static CryptoManager getCryptoManager(String value) {
40-
return new CryptoManager(value);
42+
public static CryptManager getCryptManager(String key, CryptType type) {
43+
return getCryptManager(key, type, new DefaultLogger());
4144
}
4245

4346
/**
44-
* encrypt and decrypt strings with logger implementation
47+
* encrypt and decrypt strings
4548
*
49+
* @param key
50+
* @param type
4651
* @param logger
47-
* @param value
4852
*/
49-
public static CryptoManager getCryptoManager(String value, AbstractLogger logger) {
50-
return new CryptoManager(value, logger);
53+
public static CryptManager getCryptManager(String key, CryptType type, AbstractLogger logger) {
54+
return new CryptManager(key, type, logger);
55+
}
56+
57+
/**
58+
* manage database connections and command easy
59+
*
60+
* @param formular
61+
* @param type
62+
*/
63+
public static DatabaseConnector getDatabaseManager(DatabaseFormular formular, DatabaseType type, int port) {
64+
return getDatabaseManager(formular, type, port, new DefaultLogger());
65+
}
66+
67+
/**
68+
* manage database connections and command easy with logger implementation
69+
*
70+
* @param logger
71+
* @param formular
72+
* @param type
73+
*/
74+
public static DatabaseConnector getDatabaseManager(DatabaseFormular formular, DatabaseType type, int port, AbstractLogger logger) {
75+
return new DatabaseConnector(formular, type, port, logger);
5176
}
5277

5378
/**
@@ -57,7 +82,7 @@ public static CryptoManager getCryptoManager(String value, AbstractLogger logger
5782
* @param type
5883
*/
5984
public static DatabaseConnector getDatabaseManager(DatabaseFormular formular, DatabaseType type) {
60-
return new DatabaseConnector(formular, type);
85+
return getDatabaseManager(formular, type, new DefaultLogger());
6186
}
6287

6388
/**
@@ -79,7 +104,7 @@ public static DatabaseConnector getDatabaseManager(DatabaseFormular formular, Da
79104
* @param remotePort
80105
*/
81106
public static ProxyAdapter getProxy(int localPort, String remoteHost, int remotePort) {
82-
return new ProxyAdapter(localPort, remoteHost, remotePort);
107+
return getProxy(localPort, remoteHost, remotePort, new DefaultLogger());
83108
}
84109

85110
/**
@@ -100,7 +125,7 @@ public static ProxyAdapter getProxy(int localPort, String remoteHost, int remote
100125
* @param url
101126
*/
102127
public static HttpRequestHandler getRequestHandler(String url) {
103-
return new HttpRequestHandler(url);
128+
return getRequestHandler(url, new DefaultLogger());
104129
}
105130

106131
/**
@@ -135,7 +160,7 @@ public static BaseKey getValueKey(UUID value) {
135160
* manipulate System.out
136161
*/
137162
public static OpenPrintStream getPrintStream() throws FileNotFoundException {
138-
return new OpenPrintStream();
163+
return getPrintStream(new DefaultLogger());
139164
}
140165

141166
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package pl.nightdev701.crypto;
2+
3+
import pl.nightdev701.logger.AbstractLogger;
4+
import pl.nightdev701.logger.standard.DefaultLogger;
5+
import pl.nightdev701.util.crypto.CryptoForm;
6+
7+
import javax.crypto.Cipher;
8+
import javax.crypto.SecretKey;
9+
import javax.crypto.spec.SecretKeySpec;
10+
import java.nio.charset.StandardCharsets;
11+
import java.util.Base64;
12+
import java.util.logging.Level;
13+
14+
public class BlowFishCrypto implements CryptoForm {
15+
16+
private final String encryptionKey;
17+
private final AbstractLogger logger;
18+
19+
public BlowFishCrypto(String encryptionKey) {
20+
this(encryptionKey, new DefaultLogger());
21+
}
22+
23+
public BlowFishCrypto(String encryptionKey, AbstractLogger logger) {
24+
this.encryptionKey = encryptionKey;
25+
this.logger = logger;
26+
}
27+
28+
@Override
29+
public String encrypt(String plainText) throws Exception {
30+
Cipher cipher = Cipher.getInstance("Blowfish");
31+
SecretKey secretKey = new SecretKeySpec(encryptionKey.getBytes(StandardCharsets.UTF_8), "Blowfish");
32+
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
33+
34+
logger.log(Level.INFO, "String encrypted");
35+
36+
return Base64.getEncoder().encodeToString(cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8)));
37+
}
38+
39+
@Override
40+
public String decrypt(String encryptedText) throws Exception {
41+
Cipher cipher = Cipher.getInstance("Blowfish");
42+
SecretKey secretKey = new SecretKeySpec(encryptionKey.getBytes(StandardCharsets.UTF_8), "Blowfish");
43+
cipher.init(Cipher.DECRYPT_MODE, secretKey);
44+
byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
45+
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
46+
47+
logger.log(Level.INFO, "String decrypted");
48+
49+
return new String(decryptedBytes, StandardCharsets.UTF_8);
50+
}
51+
52+
}

src/main/java/pl/nightdev701/crypto/CryptoManager.java src/main/java/pl/nightdev701/crypto/CryptoAes.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,32 @@
1212

1313
import pl.nightdev701.logger.AbstractLogger;
1414
import pl.nightdev701.logger.standard.DefaultLogger;
15+
import pl.nightdev701.util.crypto.CryptoForm;
1516

1617
import javax.crypto.Cipher;
1718
import javax.crypto.spec.SecretKeySpec;
1819
import java.nio.charset.StandardCharsets;
1920
import java.util.Base64;
2021
import java.util.logging.Level;
2122

22-
public class CryptoManager {
23+
public class CryptoAes implements CryptoForm {
2324

2425
private final String encryptionKey;
2526
private final AbstractLogger logger;
2627

27-
public CryptoManager(String encryptionKey) {
28+
public CryptoAes(String encryptionKey) {
2829
this(encryptionKey, new DefaultLogger());
2930
}
3031

31-
public CryptoManager(String encryptionKey, AbstractLogger logger) {
32+
public CryptoAes(String encryptionKey, AbstractLogger logger) {
3233
this.encryptionKey = encryptionKey;
3334
this.logger = logger;
3435
}
3536

3637
/**
3738
* encrypt data
3839
*/
40+
@Override
3941
public String encrypt(String plainText) throws Exception {
4042
SecretKeySpec secretKey = new SecretKeySpec(this.encryptionKey.getBytes(StandardCharsets.UTF_8), "AES");
4143
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
@@ -50,6 +52,7 @@ public String encrypt(String plainText) throws Exception {
5052
/**
5153
* decrypt data
5254
*/
55+
@Override
5356
public String decrypt(String encryptedText) throws Exception {
5457
SecretKeySpec secretKey = new SecretKeySpec(this.encryptionKey.getBytes(StandardCharsets.UTF_8), "AES");
5558
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

src/main/java/pl/nightdev701/database/DatabaseConnector.java

+1-22
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import pl.nightdev701.database.formular.DatabaseFormular;
1818
import pl.nightdev701.database.type.DatabaseType;
1919
import pl.nightdev701.logger.AbstractLogger;
20-
import pl.nightdev701.logger.standard.DefaultLogger;
2120

2221
import java.sql.SQLException;
2322
import java.util.logging.Level;
@@ -31,27 +30,6 @@ public class DatabaseConnector {
3130
private final String connectionURL;
3231
private JdbcPooledConnectionSource connectionSource;
3332

34-
/**
35-
* Creates a new instance of the DatabaseConnector
36-
*
37-
* @param formular
38-
* @param databaseType
39-
*/
40-
public DatabaseConnector(DatabaseFormular formular, DatabaseType databaseType) {
41-
this(formular, databaseType, new DefaultLogger());
42-
}
43-
44-
/**
45-
* Creates a new instance of the DatabaseConnector with a custom port
46-
*
47-
* @param formular
48-
* @param databaseType
49-
* @param port
50-
*/
51-
public DatabaseConnector(DatabaseFormular formular, DatabaseType databaseType, int port) {
52-
this(formular, databaseType, port, new DefaultLogger());
53-
}
54-
5533
/**
5634
* Creates a new instance of the DatabaseConnector with a custom logger
5735
*
@@ -135,6 +113,7 @@ public <T> int createTableIfNotExists(Class<T> dataClass) throws SQLException {
135113

136114
/**
137115
* get connection
116+
*
138117
* @return connectionSource
139118
*/
140119
public JdbcPooledConnectionSource getConnectionSource() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package pl.nightdev701.manager;
2+
3+
import pl.nightdev701.crypto.BlowFishCrypto;
4+
import pl.nightdev701.crypto.CryptoAes;
5+
import pl.nightdev701.logger.AbstractLogger;
6+
import pl.nightdev701.util.crypto.CryptType;
7+
import pl.nightdev701.util.crypto.CryptoForm;
8+
9+
public class CryptManager {
10+
11+
private CryptoForm crypt;
12+
13+
public CryptManager(String key, CryptType type, AbstractLogger logger) {
14+
if (type == CryptType.AES) {
15+
this.crypt = new CryptoAes(key);
16+
}
17+
if (type == CryptType.BLOWFISH) {
18+
this.crypt = new BlowFishCrypto(key);
19+
}
20+
}
21+
22+
public String encrypt(String text) {
23+
try {
24+
return getCrypt().encrypt(text);
25+
} catch (Exception e) {
26+
return null;
27+
}
28+
}
29+
30+
public String decrypt(String text) {
31+
try {
32+
return getCrypt().decrypt(text);
33+
} catch (Exception e) {
34+
return null;
35+
}
36+
}
37+
38+
public CryptoForm getCrypt() {
39+
return crypt;
40+
}
41+
}

src/main/java/pl/nightdev701/network/http/HttpRequestHandler.java

-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package pl.nightdev701.network.http;
22

33
import pl.nightdev701.logger.AbstractLogger;
4-
import pl.nightdev701.logger.standard.DefaultLogger;
54

65
import java.io.*;
76
import java.net.MalformedURLException;
@@ -20,10 +19,6 @@ public class HttpRequestHandler {
2019
private final AbstractLogger logger;
2120
private String body;
2221

23-
public HttpRequestHandler(String url) {
24-
this(url, new DefaultLogger());
25-
}
26-
2722
public HttpRequestHandler(String url, AbstractLogger logger) {
2823
this.url = url;
2924
this.logger = logger;

src/main/java/pl/nightdev701/network/tcp/ProxyAdapter.java

-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package pl.nightdev701.network.tcp;
22

33
import pl.nightdev701.logger.AbstractLogger;
4-
import pl.nightdev701.logger.standard.DefaultLogger;
54

65
import java.io.IOException;
76
import java.io.InputStream;
@@ -17,10 +16,6 @@ public class ProxyAdapter {
1716
String remoteHost;
1817
int remotePort;
1918

20-
public ProxyAdapter(int localPort, String remoteHost, int remotePort) {
21-
this(localPort, remoteHost, remotePort, new DefaultLogger());
22-
}
23-
2419
public ProxyAdapter(int localPort, String remoteHost, int remotePort, AbstractLogger logger) {
2520
this.localPort = localPort;
2621
this.remoteHost = remoteHost;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package pl.nightdev701.util.crypto;
2+
3+
public enum CryptType {
4+
5+
AES,
6+
BLOWFISH,
7+
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package pl.nightdev701.util.crypto;
2+
3+
public interface CryptoForm {
4+
5+
String encrypt(String plainText) throws Exception;
6+
7+
String decrypt(String encryptedText) throws Exception;
8+
9+
}

src/main/java/pl/nightdev701/util/stream/OpenPrintStream.java

-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package pl.nightdev701.util.stream;
22

33
import pl.nightdev701.logger.AbstractLogger;
4-
import pl.nightdev701.logger.standard.DefaultLogger;
54

65
import java.io.File;
76
import java.io.FileNotFoundException;
@@ -16,11 +15,6 @@ private OpenPrintStream(String fileName) throws FileNotFoundException {
1615
super(fileName);
1716
}
1817

19-
public OpenPrintStream() throws FileNotFoundException {
20-
super("alternative_log.txt");
21-
this.logger = new DefaultLogger();
22-
}
23-
2418
public OpenPrintStream(AbstractLogger logger) throws FileNotFoundException {
2519
super("alternative_log.txt");
2620
this.logger = logger;

0 commit comments

Comments
 (0)