Java MD5 String md5(String input)

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

Description

Create an MD5 hash of a string.

License

Apache License

Parameter

Parameter Description
input Input string.

Exception

Parameter Description
IllegalArgumentException if input is blank.

Return

Hash of input.

Declaration

static String md5(String input) 

Method Source Code


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

import java.security.MessageDigest;

public class Main {
    /**/*  w  w w .  j  ava  2s .c  o  m*/
     * Create an MD5 hash of a string.
     *
     * @param input Input string.
     * @return Hash of input.
     * @throws IllegalArgumentException if {@code input} is blank.
     */
    static String md5(String input) {
        if (input == null || input.length() == 0) {
            throw new IllegalArgumentException("Input string must not be blank.");
        }
        try {
            MessageDigest algorithm = MessageDigest.getInstance("MD5");
            algorithm.reset();
            algorithm.update(input.getBytes());
            byte[] messageDigest = algorithm.digest();

            StringBuilder hexString = new StringBuilder();
            for (byte messageByte : messageDigest) {
                hexString.append(Integer.toHexString((messageByte & 0xFF) | 0x100).substring(1, 3));
            }
            return hexString.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Related

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