What is Cryptographic One Way Hash
Function--
A
cryptographic hash function is a deterministic
procedure that takes plain text data
and returns a fixed-size bit string, the hash value. The data
to be encoded is often called the "message," and the hash
value is called the message digest or simply digest.
Every time on any platform a single algorithm will produce same
message digest for same message.
Here the "one way" means
that it's nearly impossible to derive the original text
(i.e.message) from the hash value i.e
digest.
Using CHF for Checking Data
Integrity --
By data integrity we generally mean
the validity of the data. That is if the data
that we are seeing is the original
data or some one has altered some or all of it. So here our need is
not that, no one should see the data except the targeted audience,
rather the need is no one should be able to alter the data and not
being caught.
Using CHF we can achieve this, first
calculate the hash value of the message and store it secretly,
every time while accessing the message again calculate the hash value
and check against the stored one to see if the original data is
altered.
Using CHF for Protecting
Password --
When Sony's site was hacked, the hackers listed passwords
of Sony users on web, which Sony had stored in plain text format
according to the hackers, for which many cyber security experts criticized Sony. As passwords are essential tool for access protection.
CHF is a good candidate for protecting passwords by calculating a
digest for the password and storing the digest instead of the plain
text password for future references. And this works like this,
while accepting password for the first time, use a hash function to
calculate a digest and store the digest in the database. Next time
when the user logs in, calculate the digest for the password given
at that time using same hash function and compare the now
calculated hash value/digest with the stored one. If the user
have provided the same password then the two must be same else
different.
Using MD5 in Java --
Massage Digest 5 is such a one way hash function algorithm. In below
example we will see
how to use it in java.
Being a java developer we are blessed with a rich set of security
features and inbuilt algorithm libraries. There are lots of
libraries in java.security package and its sub packages
that implement hash functions.
In this code we will use MessageDigest class to create a digest for
our supplied message and see the digested hash value as hex string .
package hashfunction;
import java.security.MessageDigest;
import
java.security.NoSuchAlgorithmException;
public class MD5Demo {
public String createDigest(String
pass){
String digest=null;
// Get bytes
byte[] bytePass2 =
pass.getBytes();
// Run the MD5 algorithm.
MessageDigest alg = null;
try {
alg =
MessageDigest.getInstance("MD5");
} catch
(NoSuchAlgorithmException e) {
return null;
}
alg.reset();
alg.update(bytePass2);
byte[] hash = alg.digest();
// Extract the has values into
a string.
StringBuilder sb = new
StringBuilder();
for (int i = 0; i <
hash.length; i++) {
String thehex =
Integer.toHexString(0xFF & hash[i]);
// Java doesn't print
leading zeros, so add one if needed.
if(thehex.length() == 1)
sb.append('0');
sb.append(thehex);
}
digest = sb.toString();
return digest;
}
public static void main(String
a[]){
System.out.println(new
MD5Demo().createDigest("mindfire"));
}
}
Here the out put is
6876f31c02ad2082dd2f7c3fb0b90b90 for the input mindfire for any
number of times on any platform.
However a small change like
mindfier instead of mindfire will produce
883f744275a404b2a2d4c3ab7d1a2518, atotaly different result .
You can search the library for more
functionalities, to make your program more secure.