Generates md5 hash from input string - Android java.security

Android examples for java.security:MD5

Description

Generates md5 hash from input string

Demo Code

import android.text.TextUtils;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.regex.Pattern;

public class Main{

    /**/*w  ww  .  ja va  2  s. c o m*/
     * Generates md5 hash from input string.

     * @param input
     *
     * @return md5 hash
     */
    public static String getMd5Hash(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            BigInteger number = new BigInteger(1, messageDigest);
            String md5 = number.toString(16);

            while (md5.length() < 32) {
                md5 = "0" + md5;
            }

            return md5;
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
    }

}

Related Tutorials