One way hashing to store passwords

One way hashing is for validating the integrity of content by detecting all modification thereof via obvious changes to the hash output. Hashing serves the purpose of ensuring integrity, i.e. making it so that if something is changed you can know that it’s changed. Technically, hashing takes arbitrary input and produces a fixed-length string that has the following attributes. Checksum calculation is a type of one way hashing.
    1.    The same input will always produce the same output.
    2.    Multiple disparate inputs should not produce the same output.
    3.    It should not be possible to go from the output to the input.
    4.    Any modification of a given input should result in drastic change to the hash.

We use one way hashing to store passwords so that we can not read it.
Once we get the password at the registration time we will create the fixed length string and store it, During the login time again we create the fixed length string using the same one way hashing technique and compare with the old one. We have different algorithms like MD5,SHA for this.

Example:
import java.security.MessageDigest;
public class Test {
    public static void main(String[] args) throws Exception {
        String str = "bhabani";
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes());
        String newStr = new String(md.digest());
        System.out.println(newStr);
        //This below string is created for the first time
        System.out.println(newStr.equals(" ÒÌw    ”ªÆYh«Ï(N0"));
    }
}



No comments:

Post a Comment