Java MD5 String md5(String s)

Here you can find the source of md5(String s)

Description

md

License

LGPL

Declaration

public static String md5(String s) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.io.UnsupportedEncodingException;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    private static final int BYTE_MASK = 0xFF;

    public static String md5(String s) {
        try {//from   ww w . j av  a  2s.com
            return md5(s.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String md5(byte[] data) {
        try {
            // Create MD5 Hash
            MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
            digest.update(data);
            byte messageDigest[] = digest.digest();

            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < messageDigest.length; i++) {
                String s = Integer.toHexString(BYTE_MASK & messageDigest[i]);
                if (s.length() < 2)
                    s = "0" + s;
                hexString.append(s);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
}

Related

  1. md5(String s)
  2. md5(String s)
  3. md5(String s)
  4. md5(String s)
  5. md5(String s)
  6. md5(String s)
  7. md5(String s)
  8. md5(String sInput)
  9. md5(String source)