Skip to content

Commit

Permalink
New socket managers released
Browse files Browse the repository at this point in the history
  • Loading branch information
N7ghtm4r3 committed Jun 24, 2023
1 parent 58d79d0 commit b442705
Show file tree
Hide file tree
Showing 11 changed files with 448 additions and 135 deletions.
Binary file modified .gradle/file-system.probe
Binary file not shown.
49 changes: 18 additions & 31 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# APIManager
**v2.1.4**
**v2.1.5**

This is a Java Based library useful to work with all json api services.

Expand All @@ -23,7 +23,7 @@ allprojects {

```gradle
dependencies {
implementation 'com.github.N7ghtm4r3:APIManager:2.1.4'
implementation 'com.github.N7ghtm4r3:APIManager:2.1.5'
}
```

Expand All @@ -45,7 +45,7 @@ dependencies {
<dependency>
<groupId>com.github.N7ghtm4r3</groupId>
<artifactId>APIManager</artifactId>
<version>2.1.4</version>
<version>2.1.5</version>
</dependency>
```

Expand All @@ -69,6 +69,7 @@ The other tools will be gradually released
- <a href="https://github.com/N7ghtm4r3/APIManager/blob/main/documd/APIRequest.md">**APIRequest**</a> allows you to manage the API request of the APIs services
- <a href="https://github.com/N7ghtm4r3/APIManager/blob/main/documd/SocketManager.md">**SocketManager**</a> allows you to manage the socket communication easily
- <a href="https://github.com/N7ghtm4r3/APIManager/blob/main/documd/AES.md">**AES encryption package**</a> allows you to use the AES encryption type easily
- <a href="https://github.com/N7ghtm4r3/APIManager/blob/main/documd/RSA.md">**RSA encryption package**</a> allows you to use the RSA encryption type easily
- <a href="https://github.com/N7ghtm4r3/APIManager/blob/main/documd/QRCodeHelper.md">**QRCodeHelper**</a> allows you to create the QRCODE easily
- <a href="https://github.com/N7ghtm4r3/APIManager/blob/main/documd/ConsolePainter.md">**ConsolePainter**</a> allows you to customize your console outputs easily

Expand Down
75 changes: 42 additions & 33 deletions documd/SocketManager.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
## SocketManager

For encryption are available the following algorithms:
<ul>
<li>
<b>AES</b>
</li>
<li>
<b>RSA</b>
</li>
</ul>

The other algorithms will be gradually released

### Usage/Examples

#### Single listener
Expand All @@ -11,13 +24,15 @@ public class Client {

public static void main(String[] args) throws Exception {

//NOT CIPHERED the communication will not be automatically ciphered
//NOT ENCRYPTED the communication will not be automatically encrypted

SocketManager client = new SocketManager("localhost", 1000);

//CIPHERED -- the communication will be automatically ciphered with the keys inserted
//ENCRYPTED -- the communication will be automatically encrypted with the keys and the algorithm choosen

SocketManager client = new SocketManager("localhost", 1000, new ClientCipher("ivSpec", "secretKey", algorithm));
AESSocketManager client = new AESSocketManager("localhost", 1000, new AESClientCipher("ivSpec", "secretKey", algorithm));

RSASocketManager client = new RSASocketManager("localhost", 1000, new RSAClientCipher("privateKey", "publicKey"));

// will start the communication sending the content message
client.writeContent("makeSomething");
Expand All @@ -39,14 +54,16 @@ public class Server {

public static void main(String[] args) throws Exception {

//NOT CIPHERED the communication will not be automatically ciphered
//NOT ENCRYPTED the communication will not be automatically encrypted

// set to false to allow only a single listener
SocketManager server = new SocketManager(false);

//CIPHERED -- the communication will be automatically ciphered with the keys inserted
//ENCRYPTED -- the communication will be automatically encrypted with the keys and the algorithm choosen

SocketManager server = new SocketManager(false, new ClientCipher("ivSpec", "secretKey", algorithm));
AESSocketManager server = new AESSocketManager(false, new AESClientCipher("ivSpec", "secretKey", algorithm));

RSASocketManager server = new RSASocketManager(false, new RSAClientCipher("privateKey", "publicKey"));

// set the default error response
socketManager.setDefaultErrorResponse(SocketManager.StandardResponseCode.FAILED);
Expand All @@ -66,16 +83,12 @@ public class Server {
// will start the communication reading the content message
String request = server.readContent();
if (request != null) {
switch (request) {
case "makeSomething":
// will end the communication sending the response content message
if(request.equals("makeSomething")){// will end the communication sending the response content message
socketManager.writeContent("executed");
//or
socketManager.sendSuccessResponse();
break;
default:
socketManager.sendDefaultErrorResponse();
}
}else {socketManager.sendDefaultErrorResponse();
}
}
} catch (IOException e) {
e.printStackTrace();
Expand All @@ -99,13 +112,15 @@ public class Client {

public static void main(String[] args) throws Exception {

//NOT CIPHERED the communication will not be automatically ciphered
//NOT ENCRYPTED the communication will not be automatically encrypted

SocketManager client = new SocketManager("localhost", 1000);

//CIPHERED -- the communication will be automatically ciphered with the keys inserted
//ENCRYPTED -- the communication will be automatically encrypted with the keys inserted

SocketManager client = new SocketManager("localhost", 1000, new ClientCipher("ivSpec", "secretKey", algorithm));
AESSocketManager client = new AESSocketManager("localhost", 1000, new AESClientCipher("ivSpec", "secretKey", algorithm));

RSASocketManager client = new RSASocketManager("localhost", 1000, new RSAClientCipher("privateKey", "publicKey"));

// will start the communication with the listener with 1000 as port,
// sending the content message
Expand Down Expand Up @@ -134,14 +149,16 @@ public class Server {

public static void main(String[] args) throws Exception {

//NOT CIPHERED the communication will not be automatically ciphered
//NOT ENCRYPTED the communication will not be automatically encrypted

// set to true to allow a multiple listeners
SocketManager server = new SocketManager(true);

//CIPHERED -- the communication will be automatically ciphered with the keys inserted
//ENCRYPTED -- the communication will be automatically encrypted with the keys inserted

SocketManager server = new SocketManager(true, new ClientCipher("ivSpec", "secretKey", algorithm));
AESSocketManager server = new AESSocketManager(true, new AESClientCipher("ivSpec", "secretKey", algorithm));

RSASocketManager server = new RSASocketManager(false, new RSAClientCipher("privateKey", "publicKey"));

// set the default error response
socketManager.setDefaultErrorResponse(SocketManager.StandardResponseCode.FAILED);
Expand All @@ -161,16 +178,12 @@ public class Server {
// will start the communication reading the content message
String request = server.readContent();
if (request != null) {
switch (request) {
case "makeSomething":
// will end the communication sending the response content message
if(request.equals("makeSomething")){// will end the communication sending the response content message
socketManager.writeContent("executedFrom1000");
//or
socketManager.sendSuccessResponse();
break;
default:
socketManager.sendDefaultErrorResponse();
}
}else {socketManager.sendDefaultErrorResponse();
}
}
} catch (IOException e) {
e.printStackTrace();
Expand All @@ -192,16 +205,12 @@ public class Server {
// will start the communication reading the content message
String request = server.readContent();
if (request != null) {
switch (request) {
case "makeSomething":
// will end the communication sending the response content message
if(request.equals("makeSomething")){// will end the communication sending the response content message
socketManager.writeContent("executedFrom1001");
//or
socketManager.sendSuccessResponse();
break;
default:
socketManager.sendDefaultErrorResponse();
}
}else {socketManager.sendDefaultErrorResponse();
}
}
} catch (IOException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tecknobit.apimanager.apis.encryption;

import com.tecknobit.apimanager.annotations.Structure;
import com.tecknobit.apimanager.annotations.Wrapper;
import com.tecknobit.apimanager.apis.encryption.aes.AESClientCipher;
import org.json.JSONObject;

Expand Down Expand Up @@ -114,6 +115,7 @@ public BaseCipher(Cipher cipher) {
* @param content: content to encrypt
* @return content encrypted as {@link Base64}-{@link String}
*/
@Wrapper
public <T> String encryptBase64(T content) throws Exception {
return encodeBase64(encrypt(content));
}
Expand All @@ -134,6 +136,7 @@ public <T> byte[] encrypt(T content) throws Exception {
* @param content: content to decrypt
* @return {@link Base64} content decrypted as {@link String}
*/
@Wrapper
public String decryptBase64(String content) throws Exception {
return new String(decrypt(decodeBase64(content)), UTF_8);
}
Expand Down Expand Up @@ -183,6 +186,7 @@ public void setAlgorithm(Algorithm algorithm) {
* @param source: source to encode
* @return source in {@link Base64} encoded as {@link String}
*/
@Wrapper
public static String encodeBase64(String source) {
return encodeBase64(source.getBytes());
}
Expand Down
Loading

0 comments on commit b442705

Please sign in to comment.