Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package funkcjeskrotu;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11. import java.io.UnsupportedEncodingException;
  12. import java.math.BigInteger;
  13. import java.security.MessageDigest;
  14. import java.security.NoSuchAlgorithmException;
  15. import javax.xml.bind.DatatypeConverter;
  16. import static javax.xml.bind.DatatypeConverter.printHexBinary;
  17.  
  18. /**
  19.  *
  20.  * @author tpodkowski
  21.  */
  22. public class FunkcjeSkrotu {
  23.  
  24.     /**
  25.      * @param args the command line arguments
  26.      */
  27.  
  28.     public static String getTextFromUser() {
  29.         BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
  30.         String text = "";
  31.  
  32.         try {
  33.             text = buffer.readLine();
  34.         } catch (IOException e) {
  35.         }
  36.  
  37.         return text;
  38.     }
  39.  
  40.     public static String toHash(String method, String text) {
  41.         try {
  42.             long startTime = System.nanoTime();
  43.                    
  44.             MessageDigest m = MessageDigest.getInstance(method);
  45.             m.reset();
  46.             m.update(text.getBytes());
  47.             byte[] digest = m.digest();
  48.             BigInteger bigInt = new BigInteger(1, digest);
  49.            
  50.             System.out.println(method + " time: " + (System.nanoTime() - startTime));
  51.             return bigInt.toString(16);
  52.         } catch (NoSuchAlgorithmException e) {
  53.             return "";
  54.         }
  55.     }
  56.  
  57.     public static void main(String[] args) {
  58.         String text = getTextFromUser();
  59.  
  60.         System.out.println("MD5: " + toHash("MD5", text));
  61.         System.out.println("SHA-1: " + toHash("SHA-1", text));
  62.         System.out.println("SHA-256: " + toHash("SHA-256", text));
  63.     }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement