Android MD5 Encode stringToMD5(String key)

Here you can find the source of stringToMD5(String key)

Description

string To MD

License

Apache License

Declaration

public static String stringToMD5(String key) 

Method Source Code

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

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

public class Main {
    public static String stringToMD5(String key) {
        String cacheKey;//  www.  j av  a2s .c  o m
        try {
            final MessageDigest mDigest = MessageDigest.getInstance("MD5");
            mDigest.update(key.getBytes());
            cacheKey = bytesToHexString(mDigest.digest());
        } catch (NoSuchAlgorithmException e) {
            cacheKey = String.valueOf(key.hashCode());
        }
        return cacheKey;
    }

    public static String bytesToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte aByte : bytes) {
            String hex = Integer.toHexString(0xFF & aByte);
            if (hex.length() == 1) {
                sb.append('0');
            }
            sb.append(hex);
        }
        return sb.toString();
    }
}

Related

  1. createHMACWithMD5(String source, String key)
  2. md5(String text)
  3. encryptByUsingMd5(String passwd)
  4. md5(String input)
  5. isValidMD5String(String md5String)
  6. toMD5(final String toEncrypt)