Java examples for java.lang:String Hash
Write code to calculate MD5 Hash for a String
//package com.java2s; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Main { public static void main(String[] argv) { String s = "java2s.com"; System.out.println(md5(s)); }/*from w ww .j ava 2 s. c om*/ public static String md5(String s) { try { // Create MD5 Hash MessageDigest digest = MessageDigest.getInstance("MD5"); digest.update(s.getBytes()); byte messageDigest[] = digest.digest(); // Create Hex String StringBuffer hexString = new StringBuffer(); for (int i = 0; i < messageDigest.length; i++) hexString.append(Integer .toHexString(0xFF & messageDigest[i])); return hexString.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return ""; } }