Java MD5 String md5(String message)

Here you can find the source of md5(String message)

Description

Encrypt the passed String using MD5.

License

Open Source License

Parameter

Parameter Description
message the message to encrypt

Return

the MD5 hash.

Declaration

public static String md5(String message) 

Method Source Code


//package com.java2s;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    /**//w  w  w .j a v a  2 s  .com
     * Encrypt the passed String using MD5.
     * 
     * @param message the message to encrypt
     * 
     * @return the MD5 hash.
     */
    public static String md5(String message) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");

            return (hex(md.digest(message.getBytes())));
        } catch (NoSuchAlgorithmException e) {
        }

        return (null);
    }

    private static String hex(byte[] array) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < array.length; ++i) {
            String hexString = Integer.toHexString((array[i] & 0xFF) | 0x100);

            for (int j = 1; j < 3; j++)
                sb.append(Character.toUpperCase(hexString.charAt(j)));
        }

        return (sb.toString());
    }
}

Related

  1. md5(String inputText)
  2. md5(String inStr)
  3. md5(String key)
  4. md5(String md5)
  5. MD5(String md5)
  6. md5(String message)
  7. MD5(String message)
  8. md5(String message)
  9. MD5(String message)