Advertisement
SelderFeys

Java intro into crypto 1

Sep 23rd, 2022 (edited)
682
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 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) throws NoSuchAlgorithmException {
  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.         }else
  17.         {
  18.             for (String test: testStringArray) {
  19.                 result.add(encrypt(test));
  20.             }
  21.         }
  22.         System.out.println(result);
  23.        
  24.     }
  25.  
  26.     public static String encrypt(String text){
  27.  
  28.         MessageDigest m = null;
  29.         try {
  30.             m = MessageDigest.getInstance("MD5");
  31.         } catch (NoSuchAlgorithmException e) {
  32.             throw new RuntimeException(e);
  33.         }
  34.  
  35.         m.reset();
  36.         m.update(text.getBytes());
  37.         byte[] digest = m.digest();
  38.         BigInteger bigInt = new BigInteger(1,digest);
  39.         String hashtext = bigInt.toString(16);
  40.         // Now we need to zero pad it if you actually want the full 32 chars.
  41.         while(hashtext.length() < 32 ){
  42.             hashtext = "0"+hashtext;
  43.         }
  44.  
  45.         return hashtext;
  46.  
  47.  
  48.     }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement