Java MD5 String md5(final String input)

Here you can find the source of md5(final String input)

Description

md

License

Apache License

Declaration

public static String md5(final String input) 

Method Source Code

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

import java.security.MessageDigest;

public class Main {

    public static String md5(final String input) {
        try {//from  w w w .j  a  v  a  2  s. c o m
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(input.getBytes());
            byte[] output = md.digest();
            return bytesToHex(output);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return input;
    }

    public static String bytesToHex(byte[] b) {
        char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        StringBuffer buf = new StringBuffer();
        for (int j = 0; j < b.length; j++) {
            buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
            buf.append(hexDigit[b[j] & 0x0f]);
        }

        return buf.toString();
    }

    public static String toString(Object obj) {
        if (null == obj)
            return null;

        return String.valueOf(obj);
    }
}

Related

  1. md5(File gcdZipFile)
  2. md5(final File file)
  3. md5(final InputStream in)
  4. md5(final String data)
  5. md5(final String inData)
  6. md5(final String input)
  7. md5(final String message)
  8. md5(final String s)
  9. md5(final String s)