Advertisement
top_wizard

HashMD5 class

Dec 11th, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. package javaapplication1;
  2. import java.security.MessageDigest;
  3. import java.security.NoSuchAlgorithmException;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. /**
  8.  *
  9.  * @author Suwandaru
  10.  */
  11. public class HashMD5 {
  12.  
  13. public String hash(String data){
  14.     String hashed="";
  15.     try{
  16.         MessageDigest md5;
  17.         md5=MessageDigest.getInstance("MD5");
  18.         md5.update(data.getBytes());
  19.         byte byteData[]=md5.digest();
  20.         //convert byte to hex
  21.         StringBuilder sb = new StringBuilder();
  22.         for (int i=0;i<byteData.length;i++){
  23.             sb.append(Integer.toString((byteData[i]&0xff)+0x100,16).substring(1));
  24.         }
  25.         hashed=sb.toString();
  26.     }
  27.     catch (NoSuchAlgorithmException e)
  28.     {
  29.         System.out.println(e.getMessage());
  30.     }
  31.     return hashed;
  32. }
  33. public String hashFile (String file) {
  34.     String hashed = "";
  35.     try {
  36.         MessageDigest md=MessageDigest.getInstance("MD5");
  37.         FileInputStream fs = new FileInputStream(file);
  38.         byte[] dataBytes = new byte [1024];
  39.         int nread=0;
  40.         while ((nread=fs.read(dataBytes))!=-1){
  41.             md.update(dataBytes,0,nread);
  42.         }
  43.         byte[] mdbytes=md.digest();
  44.         //convert byte to hex
  45.         StringBuilder sb=new StringBuilder();
  46.         for (int i=0;i<mdbytes.length;i++){
  47.         sb.append(Integer.toString((mdbytes[i]&0xff)+0x100,16).substring(1));
  48.     }
  49.         hashed=sb.toString();
  50.     }catch (NoSuchAlgorithmException e)
  51.     {
  52.         System.out.println(e.getMessage());
  53.     } catch (FileNotFoundException e){
  54.         System.out.println(e.getMessage());
  55.     } catch (IOException e){
  56.         System.out.println(e.getMessage());
  57.     }
  58.     return hashed;
  59. }
  60.     /**
  61.      * @param args the command line arguments
  62.      */
  63.     public static void main(String[] args) {
  64.         //using hash method to hash string
  65.         HashMD5 md5 = new HashMD5();
  66.         String msg = "Selamat datang di Java Message Digest";
  67.         String hashed = md5.hash(msg);
  68.         System.out.println("Message: "+msg);
  69.         System.out.println("Hashing: "+hashed);
  70.        
  71.        //using hashFile method to hash file
  72.         HashMD5 md52 = new HashMD5();
  73.         String file = "C:/Users/Suwandaru/Downloads/Schedule IT RSC Project 2014 141124.xlsx";
  74.         String fileHashed = md52.hashFile(file);
  75.         System.out.println("File hashing: "+file);
  76.         System.out.println("Hashing: "+fileHashed);
  77.        
  78.     }
  79.    
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement