Home Tutorial Java Core Java One way Hashing

 
 

Share on Google+Share on Google+
Java One way Hashing
Posted on: July 17, 2010 at 12:00 AM
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);
	}
}
Advertisement

Related Tags for Java One way Hashing:


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.

Posted on: July 17, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com