Advertisement
0no

String para MD5

0no
Oct 18th, 2017
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.54 KB | None | 0 0
  1. // Transforma uma String em md5 hash
  2.  
  3. import java.security.MessageDigest;
  4. import java.security.NoSuchAlgorithmException;
  5.  
  6. class Inicio{
  7.     public static void main(String[] args) {
  8.         try {
  9.             System.out.println(md5("Palavra"));
  10.         } catch (NoSuchAlgorithmException e) {
  11.             e.printStackTrace();
  12.         }
  13.     }
  14.  
  15.     //Jeito mais fácil
  16.     public static void md5() throws NoSuchAlgorithmException {
  17.         String s="1234";
  18.         MessageDigest m = MessageDigest.getInstance("MD5");
  19.         m.update(s.getBytes(),0,s.length());
  20.         System.out.println("MD5: "+new BigInteger(1,m.digest()).toString(16));
  21.     }
  22.    
  23.     //Jeito antigo
  24.     public String md5(){
  25.         MessageDigest m=MessageDigest.getInstance("MD5");
  26.         m.update(s.getBytes(),0,s.length());
  27.         BigInteger a = new BigInteger(1,m.digest());
  28.         return String.format("%1$032X", a).toLowerCase();
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement