Java MD5 String md5(String target)

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

Description

MD5

License

Open Source License

Parameter

Parameter Description
target a parameter

Exception

Parameter Description
NoSuchAlgorithmException an exception

Return

String

Declaration

public static String md5(String target) throws NoSuchAlgorithmException, UnsupportedEncodingException 

Method Source Code


//package com.java2s;

import java.io.*;

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

public class Main {
    /**/*  ww w  .  j  av  a 2s.  c o m*/
     * MD5
     *
     * @param target
     * @return String
     * @throws NoSuchAlgorithmException
     */
    public static String md5(String target) throws NoSuchAlgorithmException, UnsupportedEncodingException {

        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(target.getBytes("utf-8"));
        byte[] md5Bytes = digest.digest();
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < md5Bytes.length; i++) {
            sb.append(Character.forDigit(md5Bytes[i] >>> 4 & 0xf, 16));
            sb.append(Character.forDigit(md5Bytes[i] & 0xf, 16));
        }

        return sb.toString();
    }

    public static byte[] md5(byte[] src) throws NoSuchAlgorithmException {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(src);
        byte[] result = digest.digest();
        return result;
    }
}

Related

  1. md5(String string)
  2. md5(String string)
  3. md5(String string)
  4. MD5(String string)
  5. md5(String strs)
  6. md5(String text)
  7. MD5(String text)
  8. MD5(String text)
  9. md5(String text)