Advertisement
top_wizard

HashSHA class

Dec 11th, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 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. //using SHA to hash string
  8. public class HashSHA {
  9.     public String hash(String data,String hashAlgorithm){
  10.             String hashed="";
  11.             try {
  12.                 MessageDigest sha = MessageDigest.getInstance(hashAlgorithm);
  13.                 sha.update(data.getBytes());
  14.                 byte byteData []=sha.digest();
  15.                 //convert byte to hex
  16.                 StringBuilder sb = new StringBuilder();
  17.                 for (int i = 0;i<byteData.length;i++){
  18.                 sb.append(Integer.toString((byteData[i]&0xff)+0x100,16).substring(1));  
  19.             }
  20.                 hashed=sb.toString();
  21.             }catch (NoSuchAlgorithmException e){
  22.                 System.out.println(e.getMessage());
  23.             }
  24.             return hashed;
  25.         }
  26. //using SHA to hash file
  27.     public String hashFile(String file, String hashAlgorithm){
  28.         String hashed = "";
  29.         try {
  30.             MessageDigest sha=MessageDigest.getInstance(hashAlgorithm);
  31.             FileInputStream fs =new FileInputStream(file);
  32.             byte [] dataBytes=new byte [1024];
  33.             int nread=0;
  34.             while((nread=fs.read(dataBytes))!=-1){
  35.                 sha.update(dataBytes,0,nread);                
  36.             }
  37.             byte[] mdbytes=sha.digest();
  38.             //convert byte to hex
  39.             StringBuilder sb=new StringBuilder();
  40.             for(int i=0;i<mdbytes.length;i++){
  41.                 sb.append(Integer.toString((mdbytes[i]&0xff)+0x100,16).substring(1));                
  42.             }
  43.             hashed=sb.toString();
  44.         }catch(NoSuchAlgorithmException e){
  45.             System.out.println(e.getMessage());            
  46.         }catch (FileNotFoundException e){
  47.             System.out.println(e.getMessage());
  48.         }catch (IOException e){
  49.             System.out.println(e.getMessage());
  50.         }
  51.         return hashed;
  52.     }
  53.     public static void main(String[] args) {
  54.         //using HashSHA method to hash string
  55.         HashSHA sha = new HashSHA();
  56.         String msg = "Selamat datang di JAVA Message Digest";
  57.         System.out.println("Message: "+msg);
  58.         String hashed = sha.hash(msg,"SHA-1");
  59.         System.out.println("Hash SHA-1: "+hashed);
  60.         hashed=sha.hash(msg,"SHA-256");
  61.         System.out.println("Hash SHA-256: "+hashed);
  62.         hashed=sha.hash(msg,"SHA-512");
  63.         System.out.println("Hash SHA-512: "+hashed);
  64.         //using hashFile method to hash file
  65.         HashSHA sha2=new HashSHA();
  66.         String file = "C:/Users/Suwandaru/Downloads/Schedule IT RSC Project 2014 141124.xlsx";
  67.         System.out.println("File hashing: "+file);
  68.         String fileHashed=sha2.hashFile(file,"SHA-1");
  69.         System.out.println("Hash SHA-1: "+fileHashed);
  70.         fileHashed=sha2.hashFile(file,"SHA-256");
  71.         System.out.println("Hash SHA-256: "+fileHashed);
  72.         fileHashed=sha2.hashFile(file,"SHA-512");
  73.         System.out.println("Hash SHA-512: "+fileHashed);
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement