Java One way Hashing


 

Java One way Hashing

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.

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);
	}
}

Ads