Android MD5 Encode computeHashMD5(final String text)

Here you can find the source of computeHashMD5(final String text)

Description

Returns a hash value for a string using MD5 algorithm

License

Apache License

Parameter

Parameter Description
text a string to compute a hash value for

Exception

Parameter Description
NoSuchAlgorithmException if hash algorithm is not available

Return

an uppercase hash value for the given text

Declaration

public static String computeHashMD5(final String text)
        throws NoSuchAlgorithmException 

Method Source Code

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

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

public class Main {
    /**//from  w  w  w.j a  va 2 s  . c  om
     * Returns a hash value for a string using MD5 algorithm
     *
     * @param text a string to compute a hash value for
     * @return an uppercase hash value for the given text
     * @throws NoSuchAlgorithmException if hash algorithm is not available
     */
    public static String computeHashMD5(final String text)
            throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(text.getBytes(), 0, text.length());

        // get md5 bytes
        byte hashData[] = md.digest();

        // create a hex string
        StringBuilder sb = new StringBuilder(hashData.length * 2);

        for (int i = 0; i < hashData.length; i++) {
            int b = (0xFF & hashData[i]);
            // if it is a single digit, make sure it have 0 in front (proper padding)
            if (b <= 0xF)
                sb.append('0');
            // add number to string
            sb.append(Integer.toHexString(b));
        }

        // hex string to uppercase
        return sb.toString().toUpperCase();
    }
}

Related

  1. md5(final String s)
  2. md5Byte(String in)
  3. md5Byte(byte[] in)
  4. md5File(File file)
  5. md5Hex(String str)
  6. getMD5String(File file)
  7. getMD5String(String str)
  8. getMd5(byte... values)
  9. checkDicMD5(String dicFile, final byte[] expected)