Java MD5 String md5(String str)

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

Description

md

License

LGPL

Declaration

public final static String md5(String str) 

Method Source Code

//package com.java2s;
/**/*w  w  w  . j av a2 s  .  c  o  m*/
 * Core-level framework class: String and Date basic utility methods.
 * <br><br>
 * Encapsulates utility methods for everyday programming tasks
 * with Strings, Dates and other common stuff.
 * <br>
 * Creation date: 18/09/2003<br>
 * Last Update: 18/09/2003<br>
 * (c) 2003 Martin Cordova<br>
 * This code is released under the LGPL license<br>
 * @author Martin Cordova (some code written by Carlos Pineda)
 */

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

public class Main {

    public final static String md5(String str) {
        if (str == null || str.length() == 0) {
            return "";
        } else {
            StringBuffer sb = new StringBuffer();
            try {
                MessageDigest algorithm = MessageDigest.getInstance("MD5");
                algorithm.reset();
                algorithm.update(str.getBytes());

                byte[] md5 = algorithm.digest();
                String singleByteHex = "";
                for (int i = 0; i < md5.length; i++) {
                    singleByteHex = Integer.toHexString(0xFF & md5[i]);
                    if (singleByteHex.length() == 1) {
                        sb.append("0");
                    }
                    sb.append(singleByteHex.toUpperCase());
                }
            } catch (NoSuchAlgorithmException ex) {
                ex.printStackTrace();
            }
            return sb.toString();
        }
    }
}

Related

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