Android MD5 Encode MD5(String str)

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

Description

MD

Declaration

public static String MD5(String str) 

Method Source Code

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

public class Main {
    public static String MD5(String str) {
        return hash(str, "MD5");
    }// ww w.  j a v a  2  s  .  com

    public static String hash(String text, String algorithm) {

        try {

            MessageDigest md = MessageDigest.getInstance(algorithm);
            md.update(text.getBytes("iso-8859-1"), 0, text.length());
            byte[] hashBytes = md.digest();
            return convertToHex(hashBytes);

        } catch (Exception e) {
        }

        return "";

    }

    private static String convertToHex(byte[] data) {
        StringBuilder buf = new StringBuilder();
        for (byte b : data) {
            int halfbyte = (b >>> 4) & 0x0F;
            int two_halfs = 0;
            do {
                buf.append((0 <= halfbyte) && (halfbyte <= 9) ? (char) ('0' + halfbyte)
                        : (char) ('a' + (halfbyte - 10)));
                halfbyte = b & 0x0F;
            } while (two_halfs++ < 1);
        }
        return buf.toString();
    }
}

Related

  1. md5(String s)
  2. MD5(String s)
  3. MD5Crypto(String str)
  4. stringToMD5(String string)
  5. md5sum(String string)
  6. MD5Encode(String origin)
  7. getMD5Code(String value)
  8. getMD5(String s)
  9. MD5(String s)