Skip to content

Commit

Permalink
Add encode base64 and functions for createKey and createIV for utils
Browse files Browse the repository at this point in the history
  • Loading branch information
sombochea committed Apr 20, 2022
1 parent 7a47dc7 commit 01663f3
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 27 deletions.
12 changes: 0 additions & 12 deletions .idea/crypto-java.iml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'com.cubetiqs'
version '1.0'
version '1.1'

repositories {
mavenCentral()
Expand Down
30 changes: 26 additions & 4 deletions src/main/java/com/cubetiqs/crypto/Crypto.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.cubetiqs.crypto;

import com.cubetiqs.crypto.core.CryptoUtil;
import com.cubetiqs.crypto.provider.DefaultCryptoProvider;
import com.cubetiqs.crypto.util.FunctionUtil;

/**
* Crypto, provides the methods to encrypt and decrypt the data with implemented for any provider
Expand Down Expand Up @@ -33,21 +35,41 @@ public byte[] decrypt(byte[] encrypted) {

private static Crypto instance;

public static Crypto createInstance(CryptoProvider provider) {
public static Crypto newInstance(CryptoProvider provider) {
return new Crypto(provider);
}

public static Crypto newInstance(CryptoProvider provider) {
public static Crypto createInstance(CryptoProvider provider) {
if (instance == null) {
instance = createInstance(provider);
instance = newInstance(provider);
}
return instance;
}

public static Crypto newDefaultInstance(String keyText, String ivText) {
return newInstance(DefaultCryptoProvider.newInstance(encodeToBase64(keyText), encodeToBase64(ivText)));
}

public static Crypto defaultInstance(String key, String iv) {
if (instance == null) {
instance = createInstance(new DefaultCryptoProvider(key, iv));
return createInstance(DefaultCryptoProvider.newInstance(key, iv));
}
return instance;
}

public static void resetInstance() {
instance = null;
}

public static String encodeToBase64(String text) {
return FunctionUtil.encodeToBase64(text);
}

public static String createKey() {
return CryptoUtil.createKey(32, null, null);
}

public static String createIV() {
return CryptoUtil.createRandomString(16);
}
}
11 changes: 9 additions & 2 deletions src/main/java/com/cubetiqs/crypto/core/CryptoUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@
import java.util.HashMap;
import java.util.Map;

import static com.cubetiqs.crypto.util.FunctionUtil.DEFAULT_ENCODING;
import static com.cubetiqs.crypto.util.FunctionUtil.decodeBase64;
import static com.cubetiqs.crypto.util.FunctionUtil.*;

public final class CryptoUtil {
private static final int BUFFER_SIZE = 4 * 1024;

public static String createKey(int length, String algorithm, Integer keySize) {
return generateKey(length, algorithm, keySize);
}

public static String createRandomString(int length) {
return generateRandomString(length);
}

public static byte[] encrypt(byte[] data, CryptoProvider.CryptoOptions options) {
try (
InputStream fis = new ByteArrayInputStream(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,52 @@
public class DefaultCryptoProvider implements CryptoProvider {
private final CryptoOptions options;

/**
* Create new instance of Default Crypto Provider.
*
* @param key String | Key (32) (Base64 encode) for encryption.
* @param iv String | IV (16) (Base64 encode) for encryption.
*/
public DefaultCryptoProvider(String key, String iv) {
assert key != null;
this.options = new Options(key, iv);
}

/**
* Create new instance of Default Crypto Provider.
*
* @param key String | Key (32) (Base64 encode) for encryption.
*/
public DefaultCryptoProvider(String key) {
this(key, null);
}

/**
* Create new instance for CryptoProvider with Default Crypto Provider.
*
* @param key String | Key (32) (Base64 encode) for encryption.
* @param iv String | IV (16) (Base64 encode) for encryption.
* @return CryptoProvider
*/
public static CryptoProvider newInstance(String key, String iv) {
return new DefaultCryptoProvider(key, iv);
}

/**
* Create new instance for CryptoProvider with Default Crypto Provider.
*
* @param key String | Key (32) (Base64 encode) for encryption.
* @return CryptoProvider
*/
public static CryptoProvider newInstance(String key) {
return newInstance(key, null);
}

/**
* Create default instance of Default Crypto Provider.
*
* @param options CryptoOptions
*/
public DefaultCryptoProvider(CryptoOptions options) {
assert options != null;
this.options = options;
Expand All @@ -29,10 +70,20 @@ public CryptoOptions getOptions() {
return options;
}

/**
* Get Key for encryption.
*
* @return String | Base64 encoded key.
*/
public String getKey() {
return getOptions().getKey();
}

/**
* Get Iv for encryption.
*
* @return String | Base64 encoded iv.
*/
public String getIv() {
return getOptions().getIv();
}
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/com/cubetiqs/crypto/util/FunctionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,25 @@
import com.cubetiqs.crypto.CryptoProvider;
import com.cubetiqs.crypto.core.CryptoUtil;

import javax.crypto.KeyGenerator;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;

public final class FunctionUtil {
public static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;

public static String encodeToBase64(String text) {
if (text == null) {
return null;
}
byte[] bytes = encodeBase64(text.getBytes(DEFAULT_ENCODING));
return new String(bytes, DEFAULT_ENCODING);
}

public static byte[] encodeBase64(byte[] bytes) {
return Base64.getEncoder().encode(bytes);
}
Expand All @@ -19,6 +31,9 @@ public static byte[] decodeBase64(byte[] bytes) {
}

public static byte[] decodeBase64FromString(String str) {
if (str == null) {
return null;
}
return decodeBase64(str.getBytes(DEFAULT_ENCODING));
}

Expand All @@ -43,4 +58,34 @@ public static String decrypt(String encrypted, CryptoProvider.CryptoOptions opti

return new String(data, DEFAULT_ENCODING);
}

public static String generateRandomString(int length) {
byte[] iv = new byte[length];
SecureRandom random = new SecureRandom();
random.nextBytes(iv);
return Base64.getEncoder().encodeToString(iv).substring(0, length);
}

public static String generateKey(int length, String algorithm, Integer keySize) {
try {
if (algorithm == null) {
algorithm = "AES";
}
if (keySize == null) {
keySize = 256;
}
if (length == 0) {
length = 32;
}
Key key;
SecureRandom random = new SecureRandom();
KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);
keyGenerator.init(keySize, random);
key = keyGenerator.generateKey();
return Base64.getEncoder().encodeToString(key.getEncoded()).substring(0, length);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
}
8 changes: 8 additions & 0 deletions src/test/java/BasicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ public void decryptTest() {
System.out.println("Decrypted: " + decrypted);
Assertions.assertEquals(TEXT, decrypted);
}

@Test
public void encodeToBase64Test() {
String text = "Hello World";
String textBase64 = "SGVsbG8gV29ybGQ=";
String encoded = FunctionUtil.encodeToBase64(text);
Assertions.assertEquals(textBase64, encoded);
}
}
29 changes: 29 additions & 0 deletions src/test/java/DefaultCryptoV2Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import com.cubetiqs.crypto.Crypto;
import com.cubetiqs.crypto.util.FunctionUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class DefaultCryptoV2Test {
@Test
public void encryptAndDecryptV2Test() {
String key = Crypto.createKey();
String iv = Crypto.createIV();
String rawText = "I'm Sambo Chea";

Crypto crypto = Crypto.newDefaultInstance(key, iv);
String encrypted = crypto.encrypt(rawText);
String decrypted = crypto.decrypt(encrypted);

System.out.println("Encrypted: " + encrypted);
System.out.println("Decrypted: " + decrypted);
Assertions.assertEquals(rawText, decrypted);
}

@Test
public void generateKeyTest() {
String ivBase64 = FunctionUtil.generateRandomString(16);
String keyBase642 = FunctionUtil.generateKey(32, null, null);
System.out.println(ivBase64);
System.out.println(keyBase642);
}
}

0 comments on commit 01663f3

Please sign in to comment.