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.
--------------------------------------------------------------------------------------------------------------------------
NOTE ; Very soon I will be posting a GUI version of this. So stay connected.
--------------------------------------------------------------------------------------------------------------------------
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 the source from Mediafire
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
Labels:Security
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
0 comments:
Post a Comment