Java MD5 String md5(final String str)

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

Description

This function transform a string into its md5 representation.

License

Apache License

Parameter

Parameter Description
str The string to encrypt.

Exception

Parameter Description
NoSuchAlgorithmException If the md5 encryptor is not found.

Return

The encrypted string.

Declaration

public static String md5(final String str) throws NoSuchAlgorithmException 

Method Source Code


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

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

public class Main {
    /**/*w  ww . j av a2  s  .  co  m*/
     * This function transform a string into its md5 representation.
     *
     * @param str
     *            The string to encrypt.
     * @return The encrypted string.
     * @throws NoSuchAlgorithmException
     *             If the md5 encryptor is not found.
     */
    public static String md5(final String str) throws NoSuchAlgorithmException {
        final byte[] hash = MessageDigest.getInstance("MD5").digest(str.getBytes());
        final StringBuilder res = new StringBuilder();

        for (final byte element : hash) {
            final String hex = Integer.toHexString(element);
            if (hex.length() == 1)
                res.append('0').append(hex.charAt(hex.length() - 1));
            else
                res.append(hex.substring(hex.length() - 2));
        }
        return (res.toString());
    }
}

Related

  1. md5(final String input)
  2. md5(final String input)
  3. md5(final String message)
  4. md5(final String s)
  5. md5(final String s)
  6. md5(final String string)
  7. md5(final String string)
  8. md5(final String text)
  9. md5(InputStream input)