Java MD5 String md5(String src)

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

Description

Calculates the MD5 digest and returns the value as a 32 character hex string.

License

Apache License

Parameter

Parameter Description
src a string to digest

Return

MD5 digest as a hex string

Declaration

public static String md5(String src) 

Method Source Code


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

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    private static MessageDigest md;

    /**//w  w w  .  j av a 2  s .  co m
     * Calculates the MD5 digest and returns the value as a 32 character hex
     * string.
     * 
     * @param src
     *            a string to digest
     * @return MD5 digest as a hex string
     */
    public static String md5(String src) {
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        md.update(src.getBytes(StandardCharsets.UTF_8));

        return String.format("%032x", new BigInteger(1, md.digest()));
    }
}

Related

  1. md5(String source)
  2. md5(String source)
  3. md5(String source, int bit)
  4. MD5(String sourceStr)
  5. MD5(String src)
  6. md5(String src)
  7. md5(String src)
  8. md5(String srcStr)
  9. md5(String str)