From 43f5063196e7da1a0b47c278bd37d0a678205db1 Mon Sep 17 00:00:00 2001 From: Libkyy Date: Mon, 4 Apr 2022 16:56:20 +0200 Subject: [PATCH] Java code cleaner than my browser history (Except PathFinder he is incomplete) --- src/Encryption/AdvancedEncryption.java | 68 ++++++++++++ src/FindPathsPls/FindPathsMatrix.java | 70 ++++++++++++ src/FindPathsPls/FindPathsMatrixTest.java | 49 +++++++++ src/FindPathsPls/PathFinder.java | 124 ++++++++++++++++++++++ src/Stopwatch/StopWatch.java | 2 +- src/Stopwatch/StopWatchTest.java | 22 ++-- 6 files changed, 323 insertions(+), 12 deletions(-) create mode 100644 src/Encryption/AdvancedEncryption.java create mode 100644 src/FindPathsPls/FindPathsMatrix.java create mode 100644 src/FindPathsPls/FindPathsMatrixTest.java create mode 100644 src/FindPathsPls/PathFinder.java diff --git a/src/Encryption/AdvancedEncryption.java b/src/Encryption/AdvancedEncryption.java new file mode 100644 index 0000000..8fc9de7 --- /dev/null +++ b/src/Encryption/AdvancedEncryption.java @@ -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"); + } + +} + diff --git a/src/FindPathsPls/FindPathsMatrix.java b/src/FindPathsPls/FindPathsMatrix.java new file mode 100644 index 0000000..1dede89 --- /dev/null +++ b/src/FindPathsPls/FindPathsMatrix.java @@ -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; + } +} diff --git a/src/FindPathsPls/FindPathsMatrixTest.java b/src/FindPathsPls/FindPathsMatrixTest.java new file mode 100644 index 0000000..05611a2 --- /dev/null +++ b/src/FindPathsPls/FindPathsMatrixTest.java @@ -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))); + } +} \ No newline at end of file diff --git a/src/FindPathsPls/PathFinder.java b/src/FindPathsPls/PathFinder.java new file mode 100644 index 0000000..9a31ea8 --- /dev/null +++ b/src/FindPathsPls/PathFinder.java @@ -0,0 +1,124 @@ +package FindPathsPls; + +import java.util.ArrayList; +import java.util.Arrays; + +public class PathFinder +{ + public static void main(String[] args) + { + ArrayList 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 ExtremeGraph = new ArrayList<>(); + ArrayList 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 Graph , int row_len, int col_len, int path_len) + { + int power =2; + int count = 0; + if(path_len == 2) + { + ArrayList 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 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 MultiplyMatrix(ArrayList M1, ArrayList M2, int row_len, int col_len) + { + return MatrixMultiplication(M1, M2, row_len, col_len); + } + + private static ArrayList MatrixMultiplication(ArrayList M1, ArrayList M2, int row_len, int col_len) + { + String[] R; + String[] C; + ArrayList 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 SquareMatrix(ArrayList input, int row_len, int col_len) + { + return MatrixMultiplication(input, input, row_len, col_len); + + } + + public static String[] findCart(String[] arr1, String[] arr2) + { + ArrayList 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 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 input , int index) + { + return input.get(index); + } + +} diff --git a/src/Stopwatch/StopWatch.java b/src/Stopwatch/StopWatch.java index af7881d..f5a73d5 100644 --- a/src/Stopwatch/StopWatch.java +++ b/src/Stopwatch/StopWatch.java @@ -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); diff --git a/src/Stopwatch/StopWatchTest.java b/src/Stopwatch/StopWatchTest.java index d1b6869..cb9233e 100644 --- a/src/Stopwatch/StopWatchTest.java +++ b/src/Stopwatch/StopWatchTest.java @@ -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);