Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigInteger;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.util.ArrayList;
- public class Main {
- public static void main(String[] args) {
- String[] testStringArray = {" something ", " I don't know ", " Bruhhh "};
- ArrayList<String> result = new ArrayList<>();
- if (args.length > 0) {
- for (String arg : args) {
- result.add(encrypt(arg));
- }
- taskTwo(args, result);
- } else {
- for (String test : testStringArray) {
- result.add(encrypt(test));
- }
- taskTwo(testStringArray, result);
- }
- System.out.println(result);
- }
- public static void taskTwo(String[] array, ArrayList<String> hashedArray) {
- StringBuilder temp = new StringBuilder();
- for (int i = 0; i < array.length; i++) {
- temp.append(array[i]).append(" : ").append(hashedArray.get(i));
- }
- temp.append(" Hash of all:"). append(encrypt(temp.toString()));
- System.out.println(temp);
- }
- public static String encrypt(String text) {
- MessageDigest m = null;
- try {
- m = MessageDigest.getInstance("MD5");
- } catch (NoSuchAlgorithmException e) {
- throw new RuntimeException(e);
- }
- m.reset();
- m.update(text.getBytes());
- byte[] digest = m.digest();
- BigInteger bigInt = new BigInteger(1, digest);
- String hashtext = bigInt.toString(16);
- // Now we need to zero pad it if you actually want the full 32 chars.
- while (hashtext.length() < 32) {
- hashtext = "0" + hashtext;
- }
- return hashtext;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement