Android MD5 Encode md5(String s)

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

Description

md5

Parameter

Parameter Description
str a parameter

Declaration

public static String md5(String s) 

Method Source Code

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

public class Main {
    /**/* w  ww  .j  a v  a2s. c om*/
     * md5
     * 
     * @param str
     * @return
     */
    public static String md5(String s) {
        return encrypt(s, "MD5");
    }

    /**
     * encrypt method
     * 
     * @param s
     * @param method
     *            encrypt type
     * @return
     */
    private static String encrypt(String s, String method) {
        try {
            MessageDigest digest = MessageDigest.getInstance(method);
            digest.update(s.getBytes());
            byte messageDigest[] = digest.digest();
            return toHexString(messageDigest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * to hex string
     * @param keyData
     * @return
     */
    private static String toHexString(byte[] keyData) {
        if (keyData == null) {
            return null;
        }
        int expectedStringLen = keyData.length * 2;
        StringBuilder sb = new StringBuilder(expectedStringLen);
        for (int i = 0; i < keyData.length; i++) {
            String hexStr = Integer.toString(keyData[i] & 0x00FF, 16);
            if (hexStr.length() == 1) {
                hexStr = "0" + hexStr;
            }
            sb.append(hexStr);
        }
        return sb.toString();

    }
}

Related

  1. MD5(String text)
  2. md5(File input)
  3. md5(String input)
  4. md5(String ori)
  5. md5(String ori)
  6. md5(String s)
  7. md5(String str)
  8. md5(final String s)
  9. md5(final String s)