Java MD5 String md5(String str)

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

Description

md

License

LGPL

Return

32-character string.

Declaration

public static String md5(String str) 

Method Source Code

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

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

public class Main {
    /**//from w  w w.j  a v  a  2  s . c  o m
     * @return 32-character string.
     */
    public static String md5(String str) {
        MessageDigest algorithm;
        try {
            algorithm = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("No M5 messagedigest!", e);
        }

        byte[] defaultBytes = str.getBytes();
        algorithm.reset();
        algorithm.update(defaultBytes);
        byte messageDigest[] = algorithm.digest();
        StringBuilder hexString = new StringBuilder();

        for (byte aMessageDigest : messageDigest) {
            String hex = Integer.toHexString(0xFF & aMessageDigest);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        return hexString.toString();
    }
}

Related

  1. MD5(String src)
  2. md5(String src)
  3. md5(String src)
  4. md5(String src)
  5. md5(String srcStr)
  6. md5(String str)
  7. MD5(String str)
  8. md5(String str)
  9. md5(String str)