Wednesday, January 30, 2013
Many of you must have seen that a MD5/SHA-1/SHA-256/SHA-512 hash is given for every file that you download from internet. This is necessary to check whether the file downloaded has the same hash as that of original file and there is no error while downloading. This is also used in case of copyrighted objects. You must have thought how can you generate this hash. Today I am going to describe how you can very easily generate hash of a file. Here I will use java.security.MessageDigest and java.security.DigestInputStream class.
MessageDigest class : A MessageDigest object starts out initialized with defined hash algorithm. The data is processed through it using the update methods. At any point reset can be called to reset the digest. Once all the data to be updated has been updated, one of the digest methods should be called to complete the hash computation. The digest method can be called once for a given number of updates. After digest has been called, the MessageDigest object is reset to its initialized state.
DigestInputStream class : A transparent stream that updates the associated message digest using the bits going through the stream. To complete the message digest computation, call one of the digest methods on the associated message digest after your calls to one of this digest input stream's read methods.
--------------------------------------------------------------------------------------------------------------------------
Java Source Code
--------------------------------------------------------------------------------------------------------------------------
import java.io.InputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.DigestInputStream;
import java.security.NoSuchAlgorithmException;

public class HashFile {
    
    private String fname;
    private DigestInputStream dis;
    private MessageDigest md;
    
    public HashFile(String fname,String hashAlgo) {
     try{
        md=MessageDigest.getInstance(hashAlgo);  //initializing
        InputStream fis=getClass().getResourceAsStream(fname);
        dis=new DigestInputStream(fis,md);  //getting stream
     }catch(NoSuchAlgorithmException e){
      e.printStackTrace();
     }
    }
    
    public String generateHash()throws IOException{
     byte[] buf=new byte[4096];  //setting buffer
     while (dis.read()!=-1);  //reading and updating message digest
     byte[] output=md.digest();  //getting hash data in bytes
     BigInteger bi=new BigInteger(1,output);
     String hashText=bi.toString(16);  //getting hex value of hash
     dis.close();  //closing stream
     
     return hashText;
    }
    
    public static void main(String[] args) throws Exception{
     if(args.length<2){
       System.out.println("INVALID INPUT");
       System.exit(1);
     }
     HashFile obj=new HashFile(args[0],args[1]);
     System.out.println (obj.generateHash());  //printing hash
    }
}


NOTE ; Very soon I will be posting a GUI version of this. So stay connected.
--------------------------------------------------------------------------------------------------------------------------
Output
--------------------------------------------------------------------------------------------------------------------------
The input file contained text abcdefghi. The algorithm used here is Md5. To use any other algorithm, pass either SHA-1/SHA-256/SHA-512 as a command-line argument instead of Md5.
--------------------------------------------------------------------------------------------------------------------------
Download Links
--------------------------------------------------------------------------------------------------------------------------
DOWNLOAD the source from Mediafire

0 comments:

Post a Comment

Total Pageviews

Followers


Labels

Popular Posts

free counters