Calculate MD5 via String.format - Android java.security

Android examples for java.security:MD5

Description

Calculate MD5 via String.format

Demo Code

import android.text.TextUtils;
import java.security.MessageDigest;

public class Main{

    public static final String getMd5(final String toEncrypt) {
        try {/*from w  w  w .  j  a v a  2  s . c  om*/
            final MessageDigest digest = MessageDigest.getInstance("md5");
            digest.update(toEncrypt.getBytes());
            final byte[] bytes = digest.digest();
            final StringBuilder sb = new StringBuilder();
            for (int i = 0; i < bytes.length; i++) {
                sb.append(String.format("%02X", bytes[i]));
            }
            return sb.toString().toLowerCase();
        } catch (Exception exc) {
            return ""; // Impossibru!
        }
    }

}

Related Tutorials