├── Java-helper └── src │ ├── .gitignore │ ├── encryption │ ├── .gitignore │ ├── EncResult.java │ └── Encrypt.java │ ├── myMixer │ ├── .gitignore │ └── MyMixer.java │ ├── exceptions │ └── AlreadyRunningException.java │ ├── useful │ ├── DragListener.java │ ├── CharsetDetector.java │ └── Useful.java │ ├── imageUtil │ └── ImageHelp.java │ └── consoleColors │ └── Colors.java └── README.md /Java-helper/src/.gitignore: -------------------------------------------------------------------------------- 1 | /myPlayer/ 2 | -------------------------------------------------------------------------------- /Java-helper/src/encryption/.gitignore: -------------------------------------------------------------------------------- 1 | /test.java 2 | -------------------------------------------------------------------------------- /Java-helper/src/myMixer/.gitignore: -------------------------------------------------------------------------------- 1 | /MyMixer.java 2 | -------------------------------------------------------------------------------- /Java-helper/src/exceptions/AlreadyRunningException.java: -------------------------------------------------------------------------------- 1 | package exceptions; 2 | 3 | public class AlreadyRunningException extends Exception { 4 | private static final long serialVersionUID = 1L; 5 | 6 | public AlreadyRunningException() { 7 | super("The thead is already running!"); 8 | } 9 | public AlreadyRunningException(String msg) { 10 | super(msg); 11 | } 12 | public AlreadyRunningException(String msg, Throwable trw) { 13 | super(msg, trw); 14 | } 15 | public AlreadyRunningException(Throwable trw) { 16 | super(trw); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![CodeFactor](https://www.codefactor.io/repository/github/0x666c/java-helper-lib/badge)](https://www.codefactor.io/repository/github/0x666c/java-helper-lib) 2 | ![CodeFactor](https://img.shields.io/badge/code%20climate-0%20issues-green.svg) 3 | ![CodeFactor](https://img.shields.io/badge/java-1.8-red.svg) 4 | ![CodeFactor](https://img.shields.io/badge/platform-win%20%7C%20osx%20%7C%20linux-lightgrey.svg) 5 | 6 | This is my collection of methods and constants which helped me throughout my coding practice.

7 | Feel free to push any code that can help somebody and/or helped you, but please, put simple methods in 'Useful.java' and 8 | constansts, methods which require fields, collections of methods related to one thing into another class(es). 9 |

10 | Also the code is not licensed by any means, so you are free to use it anywhere you want :) 11 | (but please credit me ok?) 12 | -------------------------------------------------------------------------------- /Java-helper/src/useful/DragListener.java: -------------------------------------------------------------------------------- 1 | package useful; 2 | 3 | import java.awt.Point; 4 | import java.awt.event.MouseAdapter; 5 | import java.awt.event.MouseEvent; 6 | import javax.swing.JFrame; 7 | 8 | public class DragListener extends MouseAdapter { 9 | 10 | private final JFrame frame; 11 | private Point mouseDownCompCoords = null; 12 | 13 | public DragListener(JFrame frame) { 14 | this.frame = frame; 15 | } 16 | 17 | public void mouseReleased(MouseEvent e) { 18 | mouseDownCompCoords = null; 19 | } 20 | 21 | public void mousePressed(MouseEvent e) { 22 | mouseDownCompCoords = e.getPoint(); 23 | } 24 | 25 | public void mouseDragged(MouseEvent e) { 26 | Point currCoords = e.getLocationOnScreen(); 27 | frame.setLocation(currCoords.x - mouseDownCompCoords.x, currCoords.y - mouseDownCompCoords.y); 28 | } 29 | } -------------------------------------------------------------------------------- /Java-helper/src/imageUtil/ImageHelp.java: -------------------------------------------------------------------------------- 1 | package imageUtil; 2 | 3 | import java.awt.Color; 4 | import java.awt.Graphics; 5 | import java.awt.image.BufferedImage; 6 | 7 | public class ImageHelp { 8 | 9 | /////// 10 | public static BufferedImage fillColor(int w, int h, String color) { 11 | BufferedImage img = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB); 12 | Graphics gr = img.createGraphics(); 13 | gr.setColor(Color.decode(color)); 14 | gr.fillRect(0, 0, w, h); 15 | return img; 16 | } 17 | public static BufferedImage fillColor(int w, int h, Color color) { 18 | BufferedImage img = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB); 19 | Graphics gr = img.createGraphics(); 20 | gr.setColor(color); 21 | gr.fillRect(0, 0, w, h); 22 | return img; 23 | } 24 | /////// 25 | public static BufferedImage drawRect(BufferedImage img, int shapeX, int shapeY, int shapeW, int shapeH) { 26 | img.createGraphics().drawRect(shapeX, shapeY, shapeW, shapeH); 27 | return img; 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /Java-helper/src/encryption/EncResult.java: -------------------------------------------------------------------------------- 1 | package encryption; 2 | 3 | import java.util.ArrayList; 4 | 5 | public final class EncResult 6 | { 7 | private final char[] res; 8 | private final long time; // Test 9 | public EncResult(char[] res, long time) { 10 | this.res = res; 11 | this.time = time; 12 | } 13 | public long time() 14 | { 15 | return time; 16 | } 17 | public String get() 18 | { 19 | return new String(res); 20 | } 21 | public void print() 22 | { 23 | System.out.println(res); 24 | } 25 | @Override 26 | public String toString() { 27 | return this.get(); 28 | } 29 | @Override 30 | public int hashCode() { 31 | ArrayList ints = new ArrayList<>(res.length*2); 32 | for(int i = 0;i < res.length;i++) 33 | { 34 | if(!Character.isDigit(res[i])) 35 | ints.add((Integer)(~res[i]) | (~0xff << 1)); 36 | else 37 | ints.add((Integer)(res[i] * 2 >> 3) | ~31); 38 | } 39 | for(int i = 0;i < res.length;i++) 40 | { 41 | if(!Character.isDigit(res[i])) 42 | ints.add((Integer)((res[i] >> 2) | 0x666c)); 43 | else 44 | ints.add((Integer)(res[i]*2 | 1)); 45 | } 46 | ints.add(Integer.parseInt(new String(res).replaceAll("\\D+", "9").substring(0, 9)) * ~0x666c); 47 | 48 | System.out.println(ints.toString()); 49 | return ints.toString().hashCode(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Java-helper/src/myMixer/MyMixer.java: -------------------------------------------------------------------------------- 1 | package myMixer; 2 | 3 | import java.net.URL; 4 | 5 | import javax.sound.sampled.AudioInputStream; 6 | import javax.sound.sampled.AudioSystem; 7 | import javax.sound.sampled.Clip; 8 | import javax.sound.sampled.DataLine; 9 | import javax.sound.sampled.Mixer; 10 | 11 | public class MyMixer { 12 | 13 | Mixer mixer; 14 | Clip clip; 15 | Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo(); 16 | public static final int DEFAULT_MIXER = 1; 17 | 18 | public MyMixer(Mixer mixer) { 19 | //this.mixer = mixer; 20 | 21 | } 22 | /** 23 | * Methods to return mixer | doesn't work :/ | 24 | * @return mixer 25 | */ 26 | //@Deprecated 27 | public Mixer getMixer() { 28 | return this.mixer; 29 | } 30 | /** 31 | * Sets constructed mixer 32 | * @param info - System mixer info 33 | */ 34 | public void setMixer(Mixer.Info info) { 35 | this.mixer = AudioSystem.getMixer(info); 36 | } 37 | /** 38 | * @return array of system mixers 39 | */ 40 | public Mixer.Info[] getMixerInfo(){ 41 | return this.mixerInfo; 42 | } 43 | /** 44 | * @param n - system mixer index 45 | * @return mixer w/ specified index 46 | */ 47 | public Mixer.Info getMixerInfo(int n){ 48 | int number = n-1; 49 | if(number<=mixerInfo.length || !(number<=0)) { 50 | return this.mixerInfo[number];} 51 | else {throw new NullPointerException();} 52 | } 53 | /** 54 | * Tries to start clip with default system mixer 55 | */ 56 | public void tryToStartPlaying(String soundUrls) { 57 | setMixer(getMixerInfo(MyMixer.DEFAULT_MIXER)); 58 | DataLine.Info dlInfo = new DataLine.Info(Clip.class, null); 59 | try { 60 | this.clip = (Clip)this.mixer.getLine(dlInfo); 61 | } catch (Exception e) {System.err.println("NOPE " + e);} 62 | 63 | try { 64 | URL soundUrl = new URL(soundUrls); 65 | AudioInputStream ais = AudioSystem.getAudioInputStream(soundUrl); 66 | this.clip.open(ais); 67 | this.clip.start(); 68 | 69 | } catch (Exception e) {System.err.println("NOPE "); e.printStackTrace();} 70 | } 71 | 72 | public void tryToStartPlaying(URL soundUrl) { 73 | setMixer(getMixerInfo(MyMixer.DEFAULT_MIXER)); 74 | DataLine.Info dlInfo = new DataLine.Info(Clip.class, null); 75 | try { 76 | this.clip = (Clip)this.mixer.getLine(dlInfo); 77 | } catch (Exception e) {System.err.println("NOPE " + e);} 78 | 79 | try { 80 | AudioInputStream ais = AudioSystem.getAudioInputStream(soundUrl); 81 | this.clip.open(ais); 82 | this.clip.start(); 83 | 84 | } catch (Exception e) {System.err.println("NOPE "); e.printStackTrace();} 85 | } 86 | 87 | public void Looped(boolean is) { 88 | if(is==true) { 89 | this.clip.loop(Clip.LOOP_CONTINUOUSLY); 90 | } else if(is==false) { 91 | this.clip.loop(0);} 92 | } 93 | 94 | public void setLoop(int count) { 95 | 96 | this.clip.loop(count); 97 | } 98 | 99 | public void tryToClose() throws NullPointerException { 100 | clip.close(); 101 | clip.stop(); 102 | } 103 | } -------------------------------------------------------------------------------- /Java-helper/src/useful/CharsetDetector.java: -------------------------------------------------------------------------------- 1 | package useful; 2 | 3 | import java.io.BufferedInputStream; 4 | import java.io.File; 5 | import java.io.FileInputStream; 6 | import java.io.FileNotFoundException; 7 | import java.io.IOException; 8 | import java.io.InputStreamReader; 9 | import java.nio.ByteBuffer; 10 | import java.nio.charset.CharacterCodingException; 11 | import java.nio.charset.Charset; 12 | import java.nio.charset.CharsetDecoder; 13 | 14 | 15 | public class CharsetDetector { 16 | 17 | public Charset detectCharset(File f, String[] charsets) { 18 | 19 | Charset charset = null; 20 | 21 | for (String charsetName : charsets) { 22 | charset = detectCharset(f, Charset.forName(charsetName)); 23 | if (charset != null) { 24 | break; 25 | } 26 | } 27 | 28 | return charset; 29 | } 30 | 31 | private Charset detectCharset(File f, Charset charset) { 32 | try { 33 | BufferedInputStream input = new BufferedInputStream(new FileInputStream(f)); 34 | 35 | CharsetDecoder decoder = charset.newDecoder(); 36 | decoder.reset(); 37 | 38 | byte[] buffer = new byte[512]; 39 | boolean identified = false; 40 | while ((input.read(buffer) != -1) && (!identified)) { 41 | identified = identify(buffer, decoder); 42 | } 43 | 44 | input.close(); 45 | 46 | if (identified) { 47 | return charset; 48 | } else { 49 | return null; 50 | } 51 | 52 | } catch (Exception e) { 53 | return null; 54 | } 55 | } 56 | 57 | private boolean identify(byte[] bytes, CharsetDecoder decoder) { 58 | try { 59 | decoder.decode(ByteBuffer.wrap(bytes)); 60 | } catch (CharacterCodingException e) { 61 | return false; 62 | } 63 | return true; 64 | } 65 | 66 | /** 67 | * Returns the charset of a file 68 | * @param f The file with unknown charset 69 | * @param charsetsToBeTested Array of charsets (Example: {"UTF-8", "windows-1253", "ISO-8859-7", "windows-1251"}) 70 | * @return 71 | */ 72 | public static String detect(File f, String[] charsetsToBeTested) throws IOException { 73 | InputStreamReader reader = null; 74 | CharsetDetector cd = new CharsetDetector(); 75 | Charset charset = cd.detectCharset(f, charsetsToBeTested); 76 | 77 | if (charset != null) { 78 | try { 79 | reader = new InputStreamReader(new FileInputStream(f), charset); 80 | int c = 0; 81 | while ((c = reader.read()) != -1) { 82 | System.out.print((char)c); 83 | return String.valueOf(c); 84 | } 85 | } catch (FileNotFoundException fnfe) { 86 | fnfe.printStackTrace(); 87 | }catch(IOException ioe){ 88 | ioe.printStackTrace(); 89 | }finally { 90 | if(reader!=null) reader.close(); 91 | } 92 | }else{ 93 | System.out.println("Unrecognized charset."); 94 | }return null; 95 | } 96 | } -------------------------------------------------------------------------------- /Java-helper/src/consoleColors/Colors.java: -------------------------------------------------------------------------------- 1 | package consoleColors; 2 | 3 | public final class Colors { 4 | // Reset 5 | public static final String RESET = "\033[0m"; // Text Reset 6 | 7 | // Regular Colors 8 | public static final String BLACK = "\033[0;30m"; // BLACK 9 | public static final String RED = "\033[0;31m"; // RED 10 | public static final String GREEN = "\033[0;32m"; // GREEN 11 | public static final String YELLOW = "\033[0;33m"; // YELLOW 12 | public static final String BLUE = "\033[0;34m"; // BLUE 13 | public static final String PURPLE = "\033[0;35m"; // PURPLE 14 | public static final String CYAN = "\033[0;36m"; // CYAN 15 | public static final String WHITE = "\033[0;37m"; // WHITE 16 | 17 | // Bold 18 | public static final String BLACK_BOLD = "\033[1;30m"; // BLACK 19 | public static final String RED_BOLD = "\033[1;31m"; // RED 20 | public static final String GREEN_BOLD = "\033[1;32m"; // GREEN 21 | public static final String YELLOW_BOLD = "\033[1;33m"; // YELLOW 22 | public static final String BLUE_BOLD = "\033[1;34m"; // BLUE 23 | public static final String PURPLE_BOLD = "\033[1;35m"; // PURPLE 24 | public static final String CYAN_BOLD = "\033[1;36m"; // CYAN 25 | public static final String WHITE_BOLD = "\033[1;37m"; // WHITE 26 | 27 | // Underline 28 | public static final String BLACK_UNDERLINED = "\033[4;30m"; // BLACK 29 | public static final String RED_UNDERLINED = "\033[4;31m"; // RED 30 | public static final String GREEN_UNDERLINED = "\033[4;32m"; // GREEN 31 | public static final String YELLOW_UNDERLINED = "\033[4;33m"; // YELLOW 32 | public static final String BLUE_UNDERLINED = "\033[4;34m"; // BLUE 33 | public static final String PURPLE_UNDERLINED = "\033[4;35m"; // PURPLE 34 | public static final String CYAN_UNDERLINED = "\033[4;36m"; // CYAN 35 | public static final String WHITE_UNDERLINED = "\033[4;37m"; // WHITE 36 | 37 | // Background 38 | public static final String BLACK_BACKGROUND = "\033[40m"; // BLACK 39 | public static final String RED_BACKGROUND = "\033[41m"; // RED 40 | public static final String GREEN_BACKGROUND = "\033[42m"; // GREEN 41 | public static final String YELLOW_BACKGROUND = "\033[43m"; // YELLOW 42 | public static final String BLUE_BACKGROUND = "\033[44m"; // BLUE 43 | public static final String PURPLE_BACKGROUND = "\033[45m"; // PURPLE 44 | public static final String CYAN_BACKGROUND = "\033[46m"; // CYAN 45 | public static final String WHITE_BACKGROUND = "\033[47m"; // WHITE 46 | 47 | // High Intensity 48 | public static final String BLACK_BRIGHT = "\033[0;90m"; // BLACK 49 | public static final String RED_BRIGHT = "\033[0;91m"; // RED 50 | public static final String GREEN_BRIGHT = "\033[0;92m"; // GREEN 51 | public static final String YELLOW_BRIGHT = "\033[0;93m"; // YELLOW 52 | public static final String BLUE_BRIGHT = "\033[0;94m"; // BLUE 53 | public static final String PURPLE_BRIGHT = "\033[0;95m"; // PURPLE 54 | public static final String CYAN_BRIGHT = "\033[0;96m"; // CYAN 55 | public static final String WHITE_BRIGHT = "\033[0;97m"; // WHITE 56 | 57 | // Bold High Intensity 58 | public static final String BLACK_BOLD_BRIGHT = "\033[1;90m"; // BLACK 59 | public static final String RED_BOLD_BRIGHT = "\033[1;91m"; // RED 60 | public static final String GREEN_BOLD_BRIGHT = "\033[1;92m"; // GREEN 61 | public static final String YELLOW_BOLD_BRIGHT = "\033[1;93m";// YELLOW 62 | public static final String BLUE_BOLD_BRIGHT = "\033[1;94m"; // BLUE 63 | public static final String PURPLE_BOLD_BRIGHT = "\033[1;95m";// PURPLE 64 | public static final String CYAN_BOLD_BRIGHT = "\033[1;96m"; // CYAN 65 | public static final String WHITE_BOLD_BRIGHT = "\033[1;97m"; // WHITE 66 | 67 | // High Intensity backgrounds 68 | public static final String BLACK_BACKGROUND_BRIGHT = "\033[0;100m";// BLACK 69 | public static final String RED_BACKGROUND_BRIGHT = "\033[0;101m";// RED 70 | public static final String GREEN_BACKGROUND_BRIGHT = "\033[0;102m";// GREEN 71 | public static final String YELLOW_BACKGROUND_BRIGHT = "\033[0;103m";// YELLOW 72 | public static final String BLUE_BACKGROUND_BRIGHT = "\033[0;104m";// BLUE 73 | public static final String PURPLE_BACKGROUND_BRIGHT = "\033[0;105m"; // PURPLE 74 | public static final String CYAN_BACKGROUND_BRIGHT = "\033[0;106m"; // CYAN 75 | public static final String WHITE_BACKGROUND_BRIGHT = "\033[0;107m"; // WHITE 76 | } -------------------------------------------------------------------------------- /Java-helper/src/useful/Useful.java: -------------------------------------------------------------------------------- 1 | package useful; 2 | import java.awt.Color; 3 | import java.awt.Dimension; 4 | import java.awt.Frame; 5 | import java.awt.Rectangle; 6 | import java.awt.Toolkit; 7 | import java.awt.event.FocusEvent; 8 | import java.awt.event.FocusListener; 9 | import java.net.URL; 10 | import java.util.Random; 11 | import java.util.Scanner; 12 | import java.util.concurrent.ThreadLocalRandom; 13 | 14 | import javax.swing.JLabel; 15 | import javax.swing.JTextField; 16 | 17 | public class Useful { 18 | //////////////// 19 | //Constants// 20 | //////////////// 21 | public static final String ANSI_RESET = "\u001B[0m"; 22 | public static final String ANSI_BLACK = "\u001B[30m"; 23 | public static final String ANSI_RED = "\u001B[31m"; 24 | public static final String ANSI_GREEN = "\u001B[32m"; 25 | public static final String ANSI_YELLOW = "\u001B[33m"; 26 | public static final String ANSI_BLUE = "\u001B[34m"; 27 | public static final String ANSI_PURPLE = "\u001B[35m"; 28 | public static final String ANSI_CYAN = "\u001B[36m"; 29 | public static final String ANSI_WHITE = "\u001B[37m"; 30 | 31 | //////////////// 32 | //Methods// 33 | //////////////// 34 | /** 35 | * Find fibonacci number on specified place in the sequence 36 | * 37 | * @param place Specify the place in the sequence (e.g. if place equals 10 the method will return 55) 38 | * @return The fibonacci number in the specified place 39 | */ 40 | public static long findFibonacciAt (long place) { 41 | 42 | long value = place; 43 | 44 | double Gs1 = Math.pow(1.618034, value); 45 | double Gs2 = Math.pow(-0.618034, value); 46 | double root = Math.sqrt(5); 47 | 48 | double num = ((Gs1-Gs2)/root); 49 | 50 | return (Math.round(num)); 51 | 52 | } 53 | /** 54 | * Generate java.awt.Color variable with random (r, g, b) parameters 55 | * @return Random Color variable 56 | */ 57 | public static Color randomColor() { 58 | 59 | Random rand = new Random(); 60 | float r = rand.nextFloat(); 61 | float g = rand.nextFloat(); 62 | float b = rand.nextFloat(); 63 | 64 | Color fieldColor = new Color(r, g, b); 65 | 66 | return fieldColor; 67 | } 68 | 69 | public static int randomInt(int range) { 70 | Random rand = new Random(); 71 | if(range!=0) {return rand.nextInt(range);} 72 | else {return rand.nextInt();} 73 | } 74 | public static int randomInt(int rangeMin, int rangeMax) { 75 | ThreadLocalRandom rand = ThreadLocalRandom.current(); 76 | if(rangeMin!=0 || rangeMax!=0) {return rand.nextInt(rangeMax - rangeMin)+rangeMin;} 77 | else {return rand.nextInt();} 78 | } 79 | /** 80 | * Centers your Frame on screen 81 | * Does not use frame.setLocationRelativeTo(null) 82 | * @param frame The name of your Frame variable 83 | */ 84 | public static void placeAtCenter(Frame frame) { 85 | Dimension screenSize = (Toolkit.getDefaultToolkit().getScreenSize()); 86 | int Dx = (int) screenSize.getWidth(); 87 | int Dy = (int) screenSize.getHeight(); 88 | frame.setLocation(Dx/2 - frame.getSize().width/2, Dy/2 - frame.getSize().height/2-40); 89 | 90 | } 91 | /** 92 | * Centers your Frame on screen 93 | * Does the same thing as frame.setLocationRelativeTo(null) 94 | * @param frame The name of your Frame variable 95 | */ 96 | public static void center(Frame frame) { 97 | frame.setLocationRelativeTo(null); 98 | } 99 | /** 100 | * Centers your Frame on screen and resizes if (idk why i added this) 101 | * Does not use frame.setLocationRelativeTo(null) 102 | * @param frame The name of your Frame variable 103 | * @param width Desired width 104 | * @param height Desired height 105 | */ 106 | public static void centerAndResize(Frame frame, int width, int height) { 107 | Dimension screenSize = (Toolkit.getDefaultToolkit().getScreenSize()); 108 | int Dx = (int) screenSize.getWidth(); 109 | int Dy = (int) screenSize.getHeight(); 110 | frame.setBounds(new Rectangle(Dx/2 - frame.getSize().width/2, Dy/2 - frame.getSize().height/2-40, width, height)); 111 | 112 | } 113 | /** 114 | * Returns your ip adress 115 | * 116 | * @param site Optional. A site that will ONLY contain your ip or any other string. 117 | * if set to 'null', the function reaches "https://api.ipify.org/" to get the ip. 118 | */ 119 | public static String getIp(String site) throws Exception { 120 | String tmp = null; 121 | try { 122 | if(site = null) throw new Exception(); 123 | 124 | Scanner sc = new Scanner(new URL(site).openStream(),"UTF-8"); 125 | tmp=sc.next(); 126 | sc.close(); 127 | return tmp; 128 | } catch (Exception e1) { 129 | try { 130 | Scanner sc1 = new Scanner(new URL("https://api.ipify.org/").openStream(),"UTF-8"); 131 | tmp=sc1.next(); 132 | sc1.close(); 133 | return tmp; 134 | } catch (Exception e2) { 135 | throw new Exception("The site is probably unavalable! "+ e2.getStackTrace()); 136 | } 137 | } 138 | } 139 | /** 140 | * Add input tip on your TextField 141 | * @param field field to add tip 142 | * @param tip the tip to add 143 | */ 144 | public static void setFieldTip (final JTextField field, final String tip) { 145 | field.addFocusListener(new FocusListener() { 146 | public void focusLost(FocusEvent e) { 147 | if (field.getText().isEmpty()) { 148 | field.setText(tip); 149 | }else return; 150 | } 151 | public void focusGained(FocusEvent e) { 152 | System.out.println(field.getText()); 153 | if(field.getText().equals(tip)) { 154 | field.setText(null); 155 | } 156 | } 157 | }); 158 | } 159 | /** 160 | * Sets text color of JLabel to red and 161 | * als sets text to provided string 162 | * @param label Labet to use 163 | * @param text Desired text 164 | */ 165 | public static void errorLabel(JLabel label, String text) { 166 | label.setForeground(Color.RED); 167 | label.setText(text); 168 | } 169 | /** 170 | * Sets text color of JLabel to green and 171 | * als sets text to provided string 172 | * @param label Labet to use 173 | * @param text Desired text 174 | */ 175 | public void successLabel(JLabel label, String text) { 176 | label.setForeground(Color.GREEN); 177 | label.setText(text); 178 | } 179 | 180 | /** 181 | * Probably the most useful function 182 | * 183 | * Does the same as Thread.sleep(int millis), but keeps your code cleaner 184 | * as you don't need to catch an InterruptedException. 185 | * @param millis Amount of milliseconds to sleep 186 | */ 187 | public static void easySleep(long millis) { 188 | try { 189 | Thread.sleep(millis); 190 | } catch (InterruptedException e) { 191 | System.err.println("The thread was interrupted!\nIf it was done on purpose, please use regular sleep method"); 192 | } 193 | } 194 | /** 195 | * Probably the most useful function 196 | * 197 | * Does the same as Thread.sleep(long millis, int nanos), but keeps your code cleaner 198 | * as you don't need to catch an InterruptedException. 199 | * @param millis Amount of milliseconds to sleep 200 | * @param nanos Amount of nanoseconds to sleep 201 | */ 202 | public static void easySleep(long millis, int nanos) { 203 | try { 204 | Thread.sleep(millis, nanos); 205 | } catch (InterruptedException e) { 206 | System.err.println("The thread was interrupted!\nIf it was done on purpose, please use regular sleep method"); 207 | } 208 | } 209 | } 210 | -------------------------------------------------------------------------------- /Java-helper/src/encryption/Encrypt.java: -------------------------------------------------------------------------------- 1 | package encryption; 2 | 3 | import java.math.BigInteger; 4 | import java.nio.charset.StandardCharsets; 5 | import java.security.MessageDigest; 6 | import java.util.concurrent.ThreadLocalRandom; 7 | 8 | public class Encrypt { 9 | 10 | private final static char[] hexArray = "0123456789ABCDEF".toCharArray(); 11 | 12 | private static enum DTYPE 13 | { 14 | STRING, OBJ, OTHER; 15 | } 16 | 17 | static EncResult _enc_internal(final TYPE toEnc, final String algo, DTYPE t) 18 | { 19 | synchronized(Encrypt.class) { 20 | long st = System.nanoTime(); 21 | 22 | MessageDigest mdig = null; 23 | try 24 | { 25 | mdig = MessageDigest.getInstance(algo); 26 | } 27 | catch(Exception ex) 28 | { 29 | ex.printStackTrace(); 30 | } 31 | byte[] sorted_todigest; 32 | if(t == DTYPE.STRING) 33 | sorted_todigest = ((String)toEnc).getBytes(StandardCharsets.UTF_8); 34 | else if(t == DTYPE.OBJ) 35 | sorted_todigest = (toEnc.toString() + (toEnc.getClass().getName() + "@" + Integer.toHexString(toEnc.hashCode()))).getBytes(StandardCharsets.UTF_8); 36 | else 37 | sorted_todigest = toEnc.toString().getBytes(StandardCharsets.UTF_8); 38 | 39 | byte[] digested_bytes = mdig.digest(sorted_todigest); 40 | 41 | char[] hexChars = new char[digested_bytes.length * 2]; 42 | 43 | for ( int j = 0; j < digested_bytes.length; j++ ) { 44 | int v = digested_bytes[j] & 0xFF; 45 | hexChars[j * 2] = hexArray[v >>> 4]; 46 | hexChars[j * 2 + 1] = hexArray[v & 0x0F]; 47 | } 48 | return new EncResult(hexChars, System.nanoTime() - st); 49 | } 50 | } 51 | 52 | static String bench(final String alg) 53 | { 54 | synchronized(Encrypt.class) { 55 | BigInteger bint = BigInteger.valueOf(0); 56 | long first_calc = 0; 57 | long tenth_calc = 0; 58 | ThreadLocalRandom loc_rand = ThreadLocalRandom.current(); 59 | for(int i=0;i<9999;i++) 60 | { 61 | bint = bint.add(BigInteger.valueOf(Encrypt._enc_internal(String.valueOf(loc_rand.nextInt()), alg, DTYPE.STRING).time())); 62 | if(i==0) first_calc = bint.intValue(); 63 | if(i==10) tenth_calc = Encrypt._enc_internal(String.valueOf(loc_rand.nextInt()), alg, DTYPE.STRING).time(); 64 | } 65 | BigInteger bint_res = bint.divide(BigInteger.valueOf(9999)); 66 | 67 | try { 68 | return (String.valueOf(first_calc) + "|" + String.valueOf(tenth_calc) + "|" + bint_res.longValueExact()); 69 | } catch (ArithmeticException ae) { 70 | System.err.println("Your hardware friccin' weak! Average value somehow doesnt fit into long!"); 71 | System.err.println("Here's the strimg representation: " + bint.toString()); 72 | return "lmao_noob|" + bint.toString(); 73 | } 74 | } 75 | } 76 | 77 | public static final class md5 78 | { 79 | /** 80 | * Performs an encryption of a given String. 81 | * Supports concurrency! 82 | * 83 | * Clear jvm 1.8 x64 test 84 | * fx-6300 4ghz speed (10000 hashes total): 85 | * 86 | * First calculation: 15745647 nanoseconds 87 | * Tenth calculation: 50355 nanoseconds 88 | * Average time: 10785 nanoseconds 89 | * 90 | * NOTE: The time does in fact reduce with every iteration, so after 10-50 cycles you time will be much 91 | * lower and more consistent. 92 | * All first-time calculations (since JVM startup) take the most time, 93 | * then if you proceed to hash, your time will be getting lower and lower 94 | * EVEN IF YOU CALL THE FUNCTION AFTER SEVERAL MINUTES AND/OR FROM ANOTHER THREAD. 95 | * 96 | * You can use the {@link #Bench() Bench} method to benchmark you hashing performance. 97 | * 98 | * @param enc 99 | * String to encrypt. 100 | * @return 101 | * EncResult - utility class, which provides helper methods such as {@link encryption.EncResult#print() print} or {@link encryption.EncResult#time() time}, 102 | * also toString() is overridden to return the hash string (equivalent to calling {@link encryption.EncResult#get() get}). 103 | */ 104 | static public EncResult String(String enc) {return Encrypt._enc_internal(enc, "MD5", DTYPE.STRING);} 105 | /** 106 | * Performs an encryption of a given Integer. 107 | * Supports concurrency! 108 | * 109 | * Clear jvm 1.8 x64 test 110 | * fx-6300 4ghz speed (10000 hashes total): 111 | * 112 | * First calculation: 15745647 nanoseconds 113 | * Tenth calculation: 50355 nanoseconds 114 | * Average time: 10785 nanoseconds 115 | * 116 | * NOTE: The time does in fact reduce with every iteration, so after 10-50 cycles you time will be much 117 | * lower and more consistent. 118 | * All first-time calculations (since JVM startup) take the most time, 119 | * then if you proceed to hash, your time will be getting lower and lower 120 | * EVEN IF YOU CALL THE FUNCTION AFTER SEVERAL MINUTES AND/OR FROM ANOTHER THREAD. 121 | * 122 | * You can use the {@link #Bench() Bench} method to benchmark you hashing performance. 123 | * 124 | * @param enc 125 | * String to encrypt. 126 | * @return 127 | * EncResult - utility class, which provides helper methods such as {@link encryption.EncResult#print() print} or {@link encryption.EncResult#time() time}, 128 | * also toString() is overridden to return the hash string (equivalent to calling {@link encryption.EncResult#get() get}). 129 | */ 130 | static public EncResult Int(Integer enc) {return Encrypt._enc_internal(enc, "MD5", DTYPE.OTHER);} 131 | /** 132 | * Performs an encryption of a given Float. 133 | * Supports concurrency! 134 | * 135 | * Clear jvm 1.8 x64 test 136 | * fx-6300 4ghz speed (10000 hashes total): 137 | * 138 | * First calculation: 15745647 nanoseconds 139 | * Tenth calculation: 50355 nanoseconds 140 | * Average time: 10785 nanoseconds 141 | * 142 | * NOTE: The time does in fact reduce with every iteration, so after 10-50 cycles you time will be much 143 | * lower and more consistent. 144 | * All first-time calculations (since JVM startup) take the most time, 145 | * then if you proceed to hash, your time will be getting lower and lower 146 | * EVEN IF YOU CALL THE FUNCTION AFTER SEVERAL MINUTES AND/OR FROM ANOTHER THREAD. 147 | * 148 | * You can use the {@link #Bench() Bench} method to benchmark you hashing performance. 149 | * 150 | * @param enc 151 | * String to encrypt. 152 | * @return 153 | * EncResult - utility class, which provides helper methods such as {@link encryption.EncResult#print() print} or {@link encryption.EncResult#time() time}, 154 | * also toString() is overridden to return the hash string (equivalent to calling {@link encryption.EncResult#get() get}). 155 | */ 156 | static public EncResult Float(Float enc) {return Encrypt._enc_internal(enc, "MD5", DTYPE.OTHER);} 157 | /** 158 | * Performs an encryption of a given Double. 159 | * Supports concurrency! 160 | * 161 | * Clear jvm 1.8 x64 test 162 | * fx-6300 4ghz speed (10000 hashes total): 163 | * 164 | * First calculation: 15745647 nanoseconds 165 | * Tenth calculation: 50355 nanoseconds 166 | * Average time: 10785 nanoseconds 167 | * 168 | * NOTE: The time does in fact reduce with every iteration, so after 10-50 cycles you time will be much 169 | * lower and more consistent. 170 | * All first-time calculations (since JVM startup) take the most time, 171 | * then if you proceed to hash, your time will be getting lower and lower 172 | * EVEN IF YOU CALL THE FUNCTION AFTER SEVERAL MINUTES AND/OR FROM ANOTHER THREAD. 173 | * 174 | * You can use the {@link #Bench() Bench} method to benchmark you hashing performance. 175 | * 176 | * @param enc 177 | * String to encrypt. 178 | * @return 179 | * EncResult - utility class, which provides helper methods such as {@link encryption.EncResult#print() print} or {@link encryption.EncResult#time() time}, 180 | * also toString() is overridden to return the hash string (equivalent to calling {@link encryption.EncResult#get() get}). 181 | */ 182 | static public EncResult Double(Double enc) {return Encrypt._enc_internal(enc, "MD5", DTYPE.OTHER);} 183 | /** 184 | * Performs an encryption of a given Byte. 185 | * Supports concurrency! 186 | * 187 | * Clear jvm 1.8 x64 test 188 | * fx-6300 4ghz speed (10000 hashes total): 189 | * 190 | * First calculation: 15745647 nanoseconds 191 | * Tenth calculation: 50355 nanoseconds 192 | * Average time: 10785 nanoseconds 193 | * 194 | * NOTE: The time does in fact reduce with every iteration, so after 10-50 cycles you time will be much 195 | * lower and more consistent. 196 | * All first-time calculations (since JVM startup) take the most time, 197 | * then if you proceed to hash, your time will be getting lower and lower 198 | * EVEN IF YOU CALL THE FUNCTION AFTER SEVERAL MINUTES AND/OR FROM ANOTHER THREAD. 199 | * 200 | * You can use the {@link #Bench() Bench} method to benchmark you hashing performance. 201 | * 202 | * @param enc 203 | * String to encrypt. 204 | * @return 205 | * EncResult - utility class, which provides helper methods such as {@link encryption.EncResult#print() print} or {@link encryption.EncResult#time() time}, 206 | * also toString() is overridden to return the hash string (equivalent to calling {@link encryption.EncResult#get() get}). 207 | */ 208 | static public EncResult Byte(Byte enc) {return Encrypt._enc_internal(enc, "MD5", DTYPE.OTHER);} 209 | /** 210 | * Performs an encryption of a given Character. 211 | * Supports concurrency! 212 | * 213 | * Clear jvm 1.8 x64 test 214 | * fx-6300 4ghz speed (10000 hashes total): 215 | * 216 | * First calculation: 15745647 nanoseconds 217 | * Tenth calculation: 50355 nanoseconds 218 | * Average time: 10785 nanoseconds 219 | * 220 | * NOTE: The time does in fact reduce with every iteration, so after 10-50 cycles you time will be much 221 | * lower and more consistent. 222 | * All first-time calculations (since JVM startup) take the most time, 223 | * then if you proceed to hash, your time will be getting lower and lower 224 | * EVEN IF YOU CALL THE FUNCTION AFTER SEVERAL MINUTES AND/OR FROM ANOTHER THREAD. 225 | * 226 | * You can use the {@link #Bench() Bench} method to benchmark you hashing performance. 227 | * 228 | * @param enc 229 | * String to encrypt. 230 | * @return 231 | * EncResult - utility class, which provides helper methods such as {@link encryption.EncResult#print() print} or {@link encryption.EncResult#time() time}, 232 | * also toString() is overridden to return the hash string (equivalent to calling {@link encryption.EncResult#get() get}). 233 | */ 234 | static public EncResult Char(Character enc) {return Encrypt._enc_internal(enc, "MD5", DTYPE.OTHER);} 235 | /** 236 | * Performs an encryption of a given Object. 237 | * Supports concurrency! 238 | * 239 | * Clear jvm 1.8 x64 test 240 | * fx-6300 4ghz speed (10000 hashes total): 241 | * 242 | * First calculation: 15745647 nanoseconds 243 | * Tenth calculation: 50355 nanoseconds 244 | * Average time: 10785 nanoseconds 245 | * 246 | * NOTE: The time does in fact reduce with every iteration, so after 10-50 cycles you time will be much 247 | * lower and more consistent. 248 | * All first-time calculations (since JVM startup) take the most time, 249 | * then if you proceed to hash, your time will be getting lower and lower 250 | * EVEN IF YOU CALL THE FUNCTION AFTER SEVERAL MINUTES AND/OR FROM ANOTHER THREAD. 251 | * 252 | * You can use the {@link #Bench() Bench} method to benchmark you hashing performance. 253 | * 254 | * @param enc 255 | * String to encrypt. 256 | * @return 257 | * EncResult - utility class, which provides helper methods such as {@link encryption.EncResult#print() print} or {@link encryption.EncResult#time() time}, 258 | * also toString() is overridden to return the hash string (equivalent to calling {@link encryption.EncResult#get() get}). 259 | */ 260 | static public EncResult Object(Object enc) {return Encrypt._enc_internal(enc, "MD5", DTYPE.OBJ);} 261 | 262 | /** 263 | * 264 | */ 265 | static public String Bench() {return Encrypt.bench("md5");} 266 | } 267 | public static final class sha256 268 | { 269 | /** 270 | * Performs an encryption of a given String.
271 | * Supports concurrency!

272 | * 273 | * Clear jvm 1.8 x64 test
274 | * fx-6300 4ghz speed (10000 hashes total):

275 | * 276 | * First calculation: 17505158 nanoseconds
277 | * Tenth calculation: 87022 nanoseconds
278 | * Average time: 12631 nanoseconds

279 | * 280 | * NOTE: The time does in fact reduce with every iteration, so after 10-50 cycles you time will be much 281 | * lower and more consistent. 282 | * The first-time calculation (the first one after JVM startup) take the most time, 283 | * then if you proceed to hash, your time will be getting lower and lower 284 | * EVEN IF YOU CALL THE FUNCTION AFTER SEVERAL MINUTES AND/OR FROM ANOTHER THREAD. 285 | * 286 | * You can use the {@link #Bench() Bench} method to benchmark you hashing performance. 287 | * 288 | * @param enc 289 | * String to encrypt. 290 | * @return 291 | * EncResult - utility class, which provides helper methods such as {@link encryption.EncResult#print() print} or {@link encryption.EncResult#time() time}, 292 | * also toString() is overridden to return the hash string (equivalent to calling {@link encryption.EncResult#get() get}). 293 | */ 294 | static public EncResult String(String enc) {return Encrypt._enc_internal(enc, "SHA-256", DTYPE.STRING);} 295 | static public EncResult Int(Integer enc) {return Encrypt._enc_internal(enc, "SHA-256", DTYPE.OTHER);} 296 | static public EncResult Float(Float enc) {return Encrypt._enc_internal(enc, "SHA-256", DTYPE.OTHER);} 297 | static public EncResult Double(Double enc) {return Encrypt._enc_internal(enc, "SHA-256", DTYPE.OTHER);} 298 | static public EncResult Byte(Byte enc) {return Encrypt._enc_internal(enc, "SHA-256", DTYPE.OTHER);} 299 | static public EncResult Char(Character enc) {return Encrypt._enc_internal(enc, "SHA-256", DTYPE.OTHER);} 300 | static public EncResult Object(Object enc) {return Encrypt._enc_internal(enc, "SHA-256", DTYPE.OBJ);} 301 | 302 | static public String Bench() {return Encrypt.bench("sha-256");} 303 | } 304 | public static final class sha512 305 | { 306 | /** 307 | * Performs an encryption of a given String. 308 | * Supports concurrency! 309 | * 310 | * Clear jvm 1.8 x64 test 311 | * fx-6300 4ghz speed (10000 hashes total): 312 | * 313 | * First calculation: 16481424 nanoseconds 314 | * Tenth calculation: 78222 nanoseconds 315 | * Average time: 12016 nanoseconds 316 | * 317 | * NOTE: The time does in fact reduce with every iteration, so after 10-50 cycles you time will be much 318 | * lower and more consistent. 319 | * The first-time calculation (the first one after JVM startup) take the most time, 320 | * then if you proceed to hash, your time will be getting lower and lower 321 | * EVEN IF YOU CALL THE FUNCTION AFTER SEVERAL MINUTES AND/OR FROM ANOTHER THREAD. 322 | * 323 | * You can use the {@link #Bench() Bench} method to benchmark you hashing performance. 324 | * 325 | * @param enc 326 | * String to encrypt. 327 | * @return 328 | * EncResult - utility class, which provides helper methods such as {@link encryption.EncResult#print() print} or {@link encryption.EncResult#time() time}, 329 | * also toString() is overridden to return the hash string (equivalent to calling {@link encryption.EncResult#get() get}). 330 | */ 331 | static public EncResult String(String enc) {return Encrypt._enc_internal(enc, "SHA-512", DTYPE.STRING);} 332 | static public EncResult Int(Integer enc) {return Encrypt._enc_internal(enc, "SHA-512", DTYPE.OTHER);} 333 | static public EncResult Float(Float enc) {return Encrypt._enc_internal(enc, "SHA-512", DTYPE.OTHER);} 334 | static public EncResult Double(Double enc) {return Encrypt._enc_internal(enc, "SHA-512", DTYPE.OTHER);} 335 | static public EncResult Byte(Byte enc) {return Encrypt._enc_internal(enc, "SHA-512", DTYPE.OTHER);} 336 | static public EncResult Char(Character enc) {return Encrypt._enc_internal(enc, "SHA-512", DTYPE.OTHER);} 337 | static public EncResult Object(Object enc) {return Encrypt._enc_internal(enc, "SHA-512", DTYPE.OBJ);} 338 | 339 | static public String Bench() {return Encrypt.bench("sha-512");} 340 | } 341 | public static final class sha 342 | { 343 | /** 344 | * Performs an encryption of a given String. 345 | * Supports concurrency! 346 | * 347 | * Clear jvm 1.8 x64 test 348 | * fx-6300 4ghz speed (10000 hashes total): 349 | * 350 | * First calculation: 14076090 nanoseconds 351 | * Tenth calculation: 60622 nanoseconds 352 | * Average time: 11048 nanoseconds 353 | * 354 | * NOTE: The time does in fact reduce with every iteration, so after 10-50 cycles you time will be much 355 | * lower and more consistent. 356 | * The first-time calculation (the first one after JVM startup) take the most time, 357 | * then if you proceed to hash, your time will be getting lower and lower 358 | * EVEN IF YOU CALL THE FUNCTION AFTER SEVERAL MINUTES AND/OR FROM ANOTHER THREAD. 359 | * 360 | * You can use the {@link #Bench() Bench} method to benchmark you hashing performance. 361 | * 362 | * @param enc 363 | * String to encrypt. 364 | * @return 365 | * EncResult - utility class, which provides helper methods such as {@link encryption.EncResult#print() print} or {@link encryption.EncResult#time() time}, 366 | * also toString() is overridden to return the hash string (equivalent to calling {@link encryption.EncResult#get() get}). 367 | */ 368 | static public EncResult String(String enc) {return Encrypt._enc_internal(enc, "SHA", DTYPE.STRING);} 369 | static public EncResult Int(Integer enc) {return Encrypt._enc_internal(enc, "SHA", DTYPE.OTHER);} 370 | static public EncResult Float(Float enc) {return Encrypt._enc_internal(enc, "SHA", DTYPE.OTHER);} 371 | static public EncResult Double(Double enc) {return Encrypt._enc_internal(enc, "SHA", DTYPE.OTHER);} 372 | static public EncResult Byte(Byte enc) {return Encrypt._enc_internal(enc, "SHA", DTYPE.OTHER);} 373 | static public EncResult Char(Character enc) {return Encrypt._enc_internal(enc, "SHA", DTYPE.OTHER);} 374 | static public EncResult Object(Object enc) {return Encrypt._enc_internal(enc, "SHA", DTYPE.OBJ);} 375 | 376 | static public String Bench() {return Encrypt.bench("sha");} 377 | } 378 | } 379 | 380 | --------------------------------------------------------------------------------