Java MD5 String md5(final String input)

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

Description

Returns the MD5 of the given string

License

Open Source License

Parameter

Parameter Description
input the string

Return

the MD5 of the given string

Declaration

public static String md5(final String input) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;

public class Main {
    public static final String DELIMITER = ", ";

    /**//from   w w w .  j ava2  s .  c  om
     * Returns the MD5 of the given string
     *
     * @param input the string
     * @return the MD5 of the given string
     */
    public static String md5(final String input) {
        if (null == input) {
            return null;
        }

        try {
            final MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(input.getBytes(), 0, input.length());
            return new BigInteger(1, digest.digest()).toString(16);

        } catch (final NoSuchAlgorithmException e) {
            System.out.println("Could not create MD5! NoSuchAlgorithm: " + e.getMessage());
            return null;
        }
    }

    /**
     * Returns the list of strings as a single string
     * @param list the list of strings
     * @param delimiter the delimiter
     * @return the list as string
     */
    public static String toString(final Collection<String> list, final String delimiter) {
        final StringBuilder sb = new StringBuilder();
        for (final String string : list) {
            sb.append(string).append(delimiter);
        }
        sb.setLength(sb.length() - delimiter.length());
        return sb.toString();
    }

    /**
     * Returns the list of strings as a single string
     * @param list the list of strings
     * @return the list as string
     */
    public static String toString(final Collection<String> list) {
        return toString(list, DELIMITER);
    }
}

Related

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