Advertisement
NoSalt

FileHasher.java

Aug 17th, 2012
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.98 KB | None | 0 0
  1. import java.math.BigInteger;
  2. import java.io.FileInputStream;
  3. import java.security.MessageDigest;
  4.  
  5. import java.io.IOException;
  6. import java.io.FileNotFoundException;
  7. import java.lang.NullPointerException;
  8. import java.security.NoSuchAlgorithmException;
  9.  
  10.  
  11. public class FileHasher{
  12.     public static void main(String[] args){
  13.         if(args.length == 2){
  14.             String algorithm = args[0];
  15.             String file = args[1];
  16.             String output = "";
  17.             if(!(algorithm.equalsIgnoreCase("MD5") || algorithm.equalsIgnoreCase("SHA-1"))){
  18.                 System.out.println("Hashing algorithm must be \"MD5\" or \"SHA-1\" only.");
  19.             } else {
  20.                 FileHasher newFH = new FileHasher();
  21.                 try{
  22.                     output = newFH.getHash(file, algorithm);
  23.                 } catch (NullPointerException npe) {
  24.                     System.out.println("Oops ... Something hinkey happened: " + npe);
  25.                 }
  26.                 System.out.println(output + "  " + file);
  27.             }
  28.         } else {
  29.             System.out.println("Usage: bfilehasher hash_algorithm file_path ");
  30.         }
  31.     }
  32.  
  33.     public String getHash(String inFile, String hashingAlgorithm) throws NullPointerException {
  34.         int base = 16;
  35.         byte[] myByteArray = null;
  36.         byte[] finalByteArray = null;
  37.         String MDHash = hashingAlgorithm;
  38.         String fileHash = "";
  39.         FileInputStream myFIS = null;
  40.         BigInteger newBI;
  41.         MessageDigest myMD;
  42.        
  43.  
  44.         try {
  45.             myFIS = new FileInputStream(inFile);
  46.         } catch (FileNotFoundException fnfe) {
  47.             System.out.println("Oops ... Something hinkey happened: " + fnfe);
  48.         }
  49.  
  50.         try {
  51.             myByteArray = new byte[myFIS.available()];
  52.             myFIS.read(myByteArray);
  53.         } catch (IOException ioe) {
  54.             System.out.println("Oops ... Something hinkey happened: " + ioe);
  55.         }
  56.  
  57.         try {
  58.             myMD = MessageDigest.getInstance(MDHash);
  59.             myMD.update(myByteArray);
  60.             finalByteArray = myMD.digest();
  61.         } catch (NoSuchAlgorithmException nsae) {
  62.             System.out.println("Oops ... Something hinkey happened: " + nsae);
  63.         }
  64.  
  65.         newBI = new BigInteger(1, finalByteArray);
  66.         fileHash = newBI.toString(base);
  67.  
  68.         return fileHash;
  69.  
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement