Social Icons

Monday, March 29, 2010

How to Hash a given String using Java


Here I use images of the coding to be more cohesive , click on the images to get it's original size, all the explanations of the code have been done as comments. complete code is available at the end of the post.

There are three methods(ways) to get the Hash value of a Given String . first let's import required classes






MessageDigest class :This MessageDigest class provides applications the functionality of a message digest
algorithm, such as MD5 or SHA. message digest is the hash value we are going create. make sure you refer to the Documentation.

Define the class name as you want and now the first method is as follows.


 

Here are the MessageDigest Algorithems' names which can be used to create an instances of MessageDigest class.















The second method , here we use a salt (byte array) to improve the final outcome of the hashing process.






The Third Method. Here we use a salt and a number of iterations to do the hashing process to that given
number of times. this method is the best one out of these 3. because it's more protected against intrusions.



 

Complete code goes here.. Indentation is messed up due to the width available for posts

__________________________________________________________________________________
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.io.UnsupportedEncodingException;

class HashMe {
    
    
 public byte[] getHash(String password) 
throws NoSuchAlgorithmException , UnsupportedEncodingException {
       /* invoke MessageDigest class's static methos getInstance to
implement the specified
          hashing algoeithems , note that the arguments 
passed in this method are case insensitive
        */
       MessageDigest digest = MessageDigest.getInstance("SHA-1");
       /* Reset the Digest in case The digest currently holds 
a value in it*/
       digest.reset();
       /* MessageDigest.digest method finalizes the hasing 
process as returns
the Hash value as a byte array*/
       byte[] input = digest.digest(password.getBytes("UTF-8"));
       return input;
 }

 public byte[] getHash(String password, byte[] salt) throws 

NoSuchAlgorithmException,UnsupportedEncodingException {
       /* We use a different hasing Algorithem this time*/
       MessageDigest digest = MessageDigest.getInstance("SHA-256");
       digest.reset();
       /* MessageDigest.update method updates the current 
digest with the supplied salt value
          It doesnt finish the hasing process we sill have 
to invoke digest method */
       digest.update(salt);
       return digest.digest(password.getBytes("UTF-8"));
 }

 public byte[] getHash(int iterationNb, String password, byte[] salt) 
throws NoSuchAlgorithmException , 

UnsupportedEncodingException{
       MessageDigest digest = MessageDigest.getInstance("SHA-1");
       digest.reset();
       digest.update(salt);
       byte[] input = digest.digest(password.getBytes("UTF-8"));
       /* Up to this point we have now a hash value for the given 
string but further improvise
          the Invulnerbility of the hash value we repeat the hasing 
process for number of times which
          the method has recived as a parameter*/
       for (int i = 0; i < iterationNb; i++) {
           digest.reset();
           input = digest.digest(input);
       }
       return input;
   }
    
}

_______________________________________________________________________________
 
 
Blogger Templates http://slots.to/