Java MD5 String md5String(String key)

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

Description

md String

License

Apache License

Declaration

public static String md5String(String key) 

Method Source Code


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

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static String md5String(String key) {
        byte[] messageDigest = md5(key);
        return toHex(messageDigest);
    }/*from w  w w .j a v a  2  s  .  com*/

    public static byte[] md5(String key) {
        MessageDigest algorithm;
        try {
            algorithm = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException ex) {
            throw new RuntimeException(ex);
        }

        algorithm.reset();
        try {
            algorithm.update(key.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException ex) {
            throw new RuntimeException(ex);
        }

        return algorithm.digest();
    }

    public static String toHex(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(0xFF & bytes[i]);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        return hexString.toString();
    }
}

Related

  1. md5raw(String data)
  2. md5Representation(String data)
  3. md5Signature(TreeMap params, String secret)
  4. md5Signature(TreeMap params, String secret)
  5. md5String(String input)
  6. md5String(String paramString)
  7. md5String(String s)
  8. md5String(String s)
  9. MD5String(String str)