Advertisement
SelderFeys

Java intro into crypto 2

Sep 23rd, 2022
753
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. import java.math.BigInteger;
  2. import java.security.MessageDigest;
  3. import java.security.NoSuchAlgorithmException;
  4. import java.util.ArrayList;
  5.  
  6. public class Main {
  7.  
  8.     public static void main(String[] args)  {
  9.  
  10.         String[] testStringArray = {" something ", " I don't know ", " Bruhhh "};
  11.         ArrayList<String> result = new ArrayList<>();
  12.         if (args.length > 0) {
  13.             for (String arg : args) {
  14.                 result.add(encrypt(arg));
  15.             }
  16.             taskTwo(args, result);
  17.         } else {
  18.             for (String test : testStringArray) {
  19.                 result.add(encrypt(test));
  20.             }
  21.             taskTwo(testStringArray, result);
  22.         }
  23.         System.out.println(result);
  24.  
  25.  
  26.     }
  27.  
  28.     public static void taskTwo(String[] array, ArrayList<String> hashedArray) {
  29.  
  30.  
  31.         StringBuilder temp = new StringBuilder();
  32.  
  33.         for (int i = 0; i < array.length; i++) {
  34.  
  35.             temp.append(array[i]).append(" : ").append(hashedArray.get(i));
  36.         }
  37.  
  38.         temp.append(" Hash of all:"). append(encrypt(temp.toString()));
  39.  
  40.         System.out.println(temp);
  41.  
  42.  
  43.  
  44.     }
  45.  
  46.     public static String encrypt(String text) {
  47.  
  48.         MessageDigest m = null;
  49.         try {
  50.             m = MessageDigest.getInstance("MD5");
  51.         } catch (NoSuchAlgorithmException e) {
  52.             throw new RuntimeException(e);
  53.         }
  54.  
  55.         m.reset();
  56.         m.update(text.getBytes());
  57.         byte[] digest = m.digest();
  58.         BigInteger bigInt = new BigInteger(1, digest);
  59.         String hashtext = bigInt.toString(16);
  60.         // Now we need to zero pad it if you actually want the full 32 chars.
  61.         while (hashtext.length() < 32) {
  62.             hashtext = "0" + hashtext;
  63.         }
  64.  
  65.         return hashtext;
  66.  
  67.  
  68.     }
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement