Share on Google+Share on Google+

Java One way Hashing

Advertisement
In this tutorial you will learn about the One Way Hashing in Java. It is one of the major tool of java which is used in cryptography.

Java One way Hashing

One way hash functions are a major tool in cryptography. It is used to create digital signatures, which in turn identify and authenticate the message. It can have other practical applications as well, such as storing passwords in a user database securely or creating a file identification system. Here we have used md5 algorithm for hashing.

Here is the code:

import java.util.*;
import java.security.*;

public class Hashing {
	public static String convert(String st) throws Exception {
		byte[] by = st.getBytes();
		MessageDigest md = MessageDigest.getInstance("MD5");
		md.reset();
		md.update(by);
		byte messageDigest[] = md.digest();
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < messageDigest.length; i++) {
			buffer.append(Integer.toHexString(0xFF & messageDigest[i]));
		}
		return buffer.toString();
	}

	public static void main(String[] args) throws Exception {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter text: ");
		String text = input.nextLine();
		String st = Hashing.convert(text);
		System.out.println(st);
	}
}

Advertisements

Posted on: July 17, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.