Java MD5 String md5(String str)

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

Description

md

License

Apache License

Declaration

public static String md5(String str) 

Method Source Code


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

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

public class Main {
    public static String md5(String str) {
        MessageDigest md = null;//  w ww .  j a  va 2  s  .com
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        md.update(str.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));
        }
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < byteData.length; i++) {
            String hex = Integer.toHexString(0xff & byteData[i]);
            if (hex.length() == 1)
                hexString.append('0');
            hexString.append(hex);
        }
        return hexString.toString();
    }
}

Related

  1. md5(String str)
  2. md5(String str)
  3. MD5(String str)
  4. md5(String str)
  5. md5(String str)
  6. md5(String str)
  7. md5(String str)
  8. MD5(String str)
  9. MD5(String str)