john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

hash sha1 md5 byte array to hex formatter

import java.io.*;
import java.security.MessageDigest;
import java.security.DigestInputStream;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;

public class CalculateHash
{
    public static void main( String[] args )
    {
        checkCommandlineArguments( args );

        MessageDigest algorithm = null;
        String requestedalgorithm = args[1];
        String result = null;

        try{
            if( requestedalgorithm.contentEquals( "SHA1") )
            {       algorithm = MessageDigest.getInstance( "SHA1" );
            }
            else if( requestedalgorithm.contentEquals( "MD5") )
            {       algorithm = MessageDigest.getInstance("MD5");
            }
            else
            {   System.out.println( "Unknown hash algorithm." );
                System.exit( 1 );
            }
        }
      catch( NoSuchAlgorithmException e )
      {     System.out.println( "Error initializing hash algorithm: " + e );
            System.exit( 1 );
      }
        catch( Exception e )
        {       System.out.println( "Error initializing hash algorithm: " + e );
            e.printStackTrace();
            System.exit( 1 );
        }

        result = calculateHash( args[0] , algorithm );
        System.out.println( "Hash of file = " + result );
    } //end main


    public static String calculateHash( String filename , MessageDigest algorithm )
    {
        byte[] hash = null;
        try {
            FileInputStream filestream = new FileInputStream( filename );
            BufferedInputStream bufferedstream = new BufferedInputStream( filestream );
      DigestInputStream digeststream = new DigestInputStream( bufferedstream , algorithm );

      while( digeststream.read() != -1 )
      {
      }
      hash = algorithm.digest();
        digeststream.close();

        }catch( IOException ie )
      {     System.out.println( "Error reading file: " + ie );
            System.exit( 1 );
      }
        catch( Exception e )
        {       System.out.println( "Error while reading file/calculating hash: " + e );
            e.printStackTrace();
            System.exit( 1 );
        }

        String hashstring = byteArrayToHex( hash );
        return hashstring;
    } // end calculateHash()

    private static String byteArrayToHex( byte[] array )
    {
    Formatter formatter = new Formatter();
    for( byte b : array )
    {   formatter.format("%02x", b);
    }
    return formatter.toString();
    }

    private static void checkCommandlineArguments( String[] args )
    {
        if( args.length != 2 )
        {   System.out.println( args.length + " does not equal the 2 required arguments.");
            System.out.println( "version 0.1: java -jar calculatehash.jar filename SHA1" );
            System.out.println( " or for MD5: java -jar calculatehash.jar filename MD5" );
            System.exit( 1 );
        }
    }

} //end class

  • « atmos get
  • atmos upload »

Published

Nov 28, 2011

Category

java

~206 words

Tags

  • array 16
  • byte 2
  • formatter 1
  • hash 7
  • hex 3
  • java 252
  • md5 2
  • sha1 6
  • to 63