Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigInteger;
- import java.io.FileInputStream;
- import java.security.MessageDigest;
- import java.io.IOException;
- import java.io.FileNotFoundException;
- import java.lang.NullPointerException;
- import java.security.NoSuchAlgorithmException;
- public class FileHasher{
- public static void main(String[] args){
- if(args.length == 2){
- String algorithm = args[0];
- String file = args[1];
- String output = "";
- if(!(algorithm.equalsIgnoreCase("MD5") || algorithm.equalsIgnoreCase("SHA-1"))){
- System.out.println("Hashing algorithm must be \"MD5\" or \"SHA-1\" only.");
- } else {
- FileHasher newFH = new FileHasher();
- try{
- output = newFH.getHash(file, algorithm);
- } catch (NullPointerException npe) {
- System.out.println("Oops ... Something hinkey happened: " + npe);
- }
- System.out.println(output + " " + file);
- }
- } else {
- System.out.println("Usage: bfilehasher hash_algorithm file_path ");
- }
- }
- public String getHash(String inFile, String hashingAlgorithm) throws NullPointerException {
- int base = 16;
- byte[] myByteArray = null;
- byte[] finalByteArray = null;
- String MDHash = hashingAlgorithm;
- String fileHash = "";
- FileInputStream myFIS = null;
- BigInteger newBI;
- MessageDigest myMD;
- try {
- myFIS = new FileInputStream(inFile);
- } catch (FileNotFoundException fnfe) {
- System.out.println("Oops ... Something hinkey happened: " + fnfe);
- }
- try {
- myByteArray = new byte[myFIS.available()];
- myFIS.read(myByteArray);
- } catch (IOException ioe) {
- System.out.println("Oops ... Something hinkey happened: " + ioe);
- }
- try {
- myMD = MessageDigest.getInstance(MDHash);
- myMD.update(myByteArray);
- finalByteArray = myMD.digest();
- } catch (NoSuchAlgorithmException nsae) {
- System.out.println("Oops ... Something hinkey happened: " + nsae);
- }
- newBI = new BigInteger(1, finalByteArray);
- fileHash = newBI.toString(base);
- return fileHash;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement