Java code cleaner than my browser history (Except PathFinder he is incomplete)
This commit is contained in:
68
src/Encryption/AdvancedEncryption.java
Normal file
68
src/Encryption/AdvancedEncryption.java
Normal file
@@ -0,0 +1,68 @@
|
||||
package Encryption;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.crypto.*;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.*;
|
||||
import java.util.Base64;
|
||||
|
||||
public class AdvancedEncryption
|
||||
{
|
||||
|
||||
|
||||
private static final String CIPHER = "AES";
|
||||
|
||||
private static Key getSecureRandomKey(String cipher, int keySize)
|
||||
{
|
||||
byte[] secureRandomKeyBytes = new byte[keySize / 8];
|
||||
SecureRandom secureRandom = new SecureRandom();
|
||||
secureRandom.nextBytes(secureRandomKeyBytes);
|
||||
return new SecretKeySpec(secureRandomKeyBytes, cipher);
|
||||
}
|
||||
|
||||
public static String encrypt(String algorithm, String input, SecretKey key) throws NoSuchPaddingException, NoSuchAlgorithmException,
|
||||
InvalidKeyException,
|
||||
BadPaddingException, IllegalBlockSizeException
|
||||
{
|
||||
|
||||
Cipher cipher = Cipher.getInstance(algorithm);
|
||||
cipher.init(Cipher.ENCRYPT_MODE, key);
|
||||
byte[] cipherText = cipher.doFinal(input.getBytes(StandardCharsets.UTF_8));
|
||||
return Base64.getEncoder().encodeToString(cipherText);
|
||||
}
|
||||
|
||||
public static String decrypt(String algorithm, String cipherText, SecretKey key) throws NoSuchPaddingException, NoSuchAlgorithmException,
|
||||
InvalidKeyException,
|
||||
BadPaddingException, IllegalBlockSizeException
|
||||
{
|
||||
|
||||
Cipher cipher = Cipher.getInstance(algorithm);
|
||||
cipher.init(Cipher.DECRYPT_MODE, key);
|
||||
byte[] plainText = cipher.doFinal(Base64.getDecoder().decode(cipherText));
|
||||
|
||||
return new String(plainText);
|
||||
}
|
||||
|
||||
@Test
|
||||
void Key_Of_128_Speed()
|
||||
throws NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException,
|
||||
BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException
|
||||
{
|
||||
|
||||
String input = "012345678901234567890012345678912";
|
||||
SecretKey key = (SecretKey) getSecureRandomKey("AES", 128);
|
||||
String algorithm = "AES/ECB/PKCS5Padding";
|
||||
String cipherText = encrypt(algorithm, input , key);
|
||||
long start = System.nanoTime();
|
||||
String plainText = decrypt(algorithm, cipherText, key);
|
||||
long finish = System.nanoTime();
|
||||
Assertions.assertEquals(input, plainText);
|
||||
long time = finish - start;
|
||||
System.out.println(time + " nanoseconds");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
70
src/FindPathsPls/FindPathsMatrix.java
Normal file
70
src/FindPathsPls/FindPathsMatrix.java
Normal file
@@ -0,0 +1,70 @@
|
||||
package FindPathsPls;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class FindPathsMatrix
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int[][] M1 =
|
||||
{
|
||||
{1, 2, 3, 4},
|
||||
{3, 4, 5, 6},
|
||||
{6, 7, 8, 10},
|
||||
{2, 5, 2, 8}
|
||||
};
|
||||
GetPaths(M1);
|
||||
}
|
||||
public static void GetPaths(int[][] M)
|
||||
{
|
||||
int[][] output = MultiplyMatrix(M,M);
|
||||
assert output != null;
|
||||
for (int[] ints : output) System.out.println(Arrays.toString(ints));
|
||||
|
||||
}
|
||||
|
||||
public static int[][] MultiplyMatrix(int[][] M1, int[][] M2)
|
||||
{
|
||||
if(M1.length == M2[0].length)
|
||||
{
|
||||
int[][] output = new int[M1.length][M1.length];
|
||||
for (int i = 0; i < M1.length; i++)
|
||||
{
|
||||
int[] R = getRow(M1, i);
|
||||
for (int j = 0; j < M2.length; j++)
|
||||
{
|
||||
int[] C = getColumn(M2, j);
|
||||
output[i][j] = RxC(R, C);
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
public static int[] getColumn(int[][] input , int index)
|
||||
{
|
||||
int[] output = new int[input.length];
|
||||
for(int i = 0; i < input.length; i++)
|
||||
output[i] = input[i][index];
|
||||
return output;
|
||||
}
|
||||
|
||||
public static int[] getRow(int[][] input , int index)
|
||||
{
|
||||
return input[index];
|
||||
}
|
||||
|
||||
public static int RxC(int[] R , int[] C)
|
||||
{
|
||||
int output = 0;
|
||||
int temp;
|
||||
for (int i = 0; i < R.length ; i++)
|
||||
{
|
||||
temp = R[i] * C[i];
|
||||
output += temp;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
}
|
||||
49
src/FindPathsPls/FindPathsMatrixTest.java
Normal file
49
src/FindPathsPls/FindPathsMatrixTest.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package FindPathsPls;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class FindPathsMatrixTest
|
||||
{
|
||||
|
||||
|
||||
@Test
|
||||
void getColumn()
|
||||
{
|
||||
int[][] M1 =
|
||||
{
|
||||
{1, 2, 3},
|
||||
{3, 4, 5},
|
||||
{6, 7, 8}
|
||||
};
|
||||
assertEquals(Arrays.toString(new int[]{1, 3, 6}), Arrays.toString(FindPathsMatrix.getColumn(M1,0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getRow()
|
||||
{
|
||||
int[][] M1 =
|
||||
{
|
||||
{1, 2, 3},
|
||||
{3, 4, 5},
|
||||
{6, 7, 8}
|
||||
};
|
||||
|
||||
assertEquals(Arrays.toString(new int[]{1, 2, 6}), Arrays.toString(FindPathsMatrix.getRow(M1,0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rxC()
|
||||
{
|
||||
int[][] M1 =
|
||||
{
|
||||
{1, 2, 3},
|
||||
{3, 4, 5},
|
||||
{6, 7, 8}
|
||||
};
|
||||
assertEquals(25,FindPathsMatrix.RxC(FindPathsMatrix.getRow(M1,0),FindPathsMatrix.getColumn(M1,0)));
|
||||
}
|
||||
}
|
||||
124
src/FindPathsPls/PathFinder.java
Normal file
124
src/FindPathsPls/PathFinder.java
Normal file
@@ -0,0 +1,124 @@
|
||||
package FindPathsPls;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class PathFinder
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
ArrayList<String[]> Graph = new ArrayList<>();
|
||||
Graph.add(new String[]{"", "e1", "e3"});
|
||||
Graph.add( new String[]{"e1", "", "e2"});
|
||||
Graph.add( new String[]{"e3", "e2", ""});
|
||||
GetPaths(Graph,3,3,2);
|
||||
ArrayList<String[]> ExtremeGraph = new ArrayList<>();
|
||||
ArrayList<String[]> Graph2 = new ArrayList<>();
|
||||
Graph2.add(new String[]{"", "e1,e2,e3", "e3,e4"});
|
||||
Graph2.add( new String[]{"e1,e2,e3", "", "e6"});
|
||||
Graph2.add( new String[]{"e3,e4", "e6", ""});
|
||||
GetPaths(Graph2,3,3,2);
|
||||
|
||||
}
|
||||
private static void GetPaths(ArrayList<String[]> Graph , int row_len, int col_len, int path_len)
|
||||
{
|
||||
int power =2;
|
||||
int count = 0;
|
||||
if(path_len == 2)
|
||||
{
|
||||
ArrayList<String[]> output = SquareMatrix(Graph, row_len, col_len);
|
||||
for(int i = 0; i < output.size();i++)
|
||||
{
|
||||
count++;
|
||||
System.out.print(Arrays.toString(output.get(i))+" , ");
|
||||
if(count % 3 == 0)
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ArrayList<String[]> output = Graph;
|
||||
|
||||
while(power <= path_len)
|
||||
{
|
||||
output = MultiplyMatrix(output, Graph, row_len, col_len);
|
||||
power++;
|
||||
}
|
||||
for(int i = 0; i <= output.size();i++)
|
||||
{
|
||||
count++;
|
||||
System.out.print(Arrays.toString(output.get(i)));
|
||||
if((count % 3) == 0)
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static ArrayList<String[]> MultiplyMatrix(ArrayList<String[]> M1, ArrayList<String[]> M2, int row_len, int col_len)
|
||||
{
|
||||
return MatrixMultiplication(M1, M2, row_len, col_len);
|
||||
}
|
||||
|
||||
private static ArrayList<String[]> MatrixMultiplication(ArrayList<String[]> M1, ArrayList<String[]> M2, int row_len, int col_len)
|
||||
{
|
||||
String[] R;
|
||||
String[] C;
|
||||
ArrayList<String[]> output = new ArrayList<>();
|
||||
for(int rows = 0; rows < row_len; rows++)
|
||||
{
|
||||
R = getRow(M1,rows);
|
||||
|
||||
for (int cols = 0; cols < col_len; cols++)
|
||||
{
|
||||
C = getColumn(M2, cols);
|
||||
output.add(findCart(R, C));
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
private static ArrayList<String[]> SquareMatrix(ArrayList<String[]> input, int row_len, int col_len)
|
||||
{
|
||||
return MatrixMultiplication(input, input, row_len, col_len);
|
||||
|
||||
}
|
||||
|
||||
public static String[] findCart(String[] arr1, String[] arr2)
|
||||
{
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
for (String s : arr1)
|
||||
for (String value : arr2)
|
||||
{
|
||||
if (!(s.equals("") || value.equals("")))
|
||||
{
|
||||
list.add(s + "->" + value);
|
||||
}
|
||||
|
||||
}
|
||||
String[] arr = new String[list.size()];
|
||||
|
||||
return list.toArray(arr);
|
||||
}
|
||||
|
||||
public static String[] getColumn(ArrayList<String[]> input , int index)
|
||||
{
|
||||
String[] output = new String[input.size()];
|
||||
for(int i = 0; i < input.size(); i++)
|
||||
{
|
||||
if(input.get(i) != null)
|
||||
output[i] = input.get(i)[index];
|
||||
}
|
||||
return output;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static String[] getRow(ArrayList<String[]> input , int index)
|
||||
{
|
||||
return input.get(index);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import java.time.temporal.ChronoUnit;
|
||||
public class StopWatch
|
||||
{
|
||||
private LocalTime startTime,endTime;
|
||||
StopWatch()
|
||||
public StopWatch()
|
||||
{
|
||||
startTime=LocalTime.of(0,0,0);
|
||||
endTime= LocalTime.of(0,0,0);
|
||||
|
||||
@@ -16,16 +16,16 @@ public class StopWatchTest
|
||||
StopWatch Watch2 = new StopWatch();
|
||||
StopWatch Watch3 = new StopWatch();
|
||||
StopWatch Watch4 = new StopWatch();
|
||||
Watch1.start();
|
||||
try
|
||||
{
|
||||
TimeUnit.MINUTES.sleep(1);
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
Watch1.stop();
|
||||
// Watch1.start();
|
||||
// try
|
||||
// {
|
||||
// TimeUnit.MINUTES.sleep(1);
|
||||
// }
|
||||
// catch (InterruptedException e)
|
||||
// {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// Watch1.stop();
|
||||
|
||||
Watch2.start();
|
||||
TimeUnit.MILLISECONDS.sleep(69);
|
||||
@@ -43,7 +43,7 @@ public class StopWatchTest
|
||||
|
||||
|
||||
|
||||
assertEquals(60000, Watch1.getElapsedTime(), 10);
|
||||
// assertEquals(60000, Watch1.getElapsedTime(), 10);
|
||||
assertEquals(69, Watch2.getElapsedTime(),10);
|
||||
assertEquals(10, Watch3.getElapsedTime(),10);
|
||||
assertEquals(69, Watch4.getElapsedTime(),10);
|
||||
|
||||
Reference in New Issue
Block a user