Java MD5 String md5(String srcStr)

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

Description

md

License

Open Source License

Declaration

public static String md5(String srcStr) 

Method Source Code

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

public class Main {
    private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4',
            '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    public static String md5(String srcStr) {
        return hash("MD5", srcStr);
    }//from   w w  w  .j  a  v a2 s . co m

    public static String hash(String algorithm, String srcStr) {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithm);
            byte[] bytes = md.digest(srcStr.getBytes("utf-8"));
            return toHex(bytes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static String toHex(byte[] bytes) {
        StringBuilder ret = new StringBuilder(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            ret.append(HEX_DIGITS[(bytes[i] >> 4) & 0x0f]);
            ret.append(HEX_DIGITS[bytes[i] & 0x0f]);
        }
        return ret.toString();
    }
}

Related

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