hash Password using MD5 - Java Security

Java examples for Security:MD5

Description

hash Password using MD5

Demo Code


//package com.java2s;
import java.security.MessageDigest;

public class Main {
    public static void main(String[] argv) throws Exception {
        String password = "java2s.com";
        System.out.println(hashPassword(password));
    }//w w  w  . j a  v a2  s.  co m

    public static String hashPassword(String password) {
        String hash = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(password.getBytes());
            byte byteData[] = md.digest();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < byteData.length; i++) {
                sb.append(Integer
                        .toString((byteData[i] & 0xff) + 0x100, 16)
                        .substring(1));
            }
            hash = sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hash;
    }
}

Related Tutorials