Java MD5 String md5(String text)

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

Description

md

License

Open Source License

Declaration

public static String md5(String text) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static String md5(String text) {
        try {//from  w  w w .  j av a2 s  . c  o m
            return hash("MD5", text);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String hash(String algorithm, String text) throws NoSuchAlgorithmException {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithm.toUpperCase());
            byte[] hash = md.digest(text.getBytes("UTF-8"));
            StringBuilder sb = new StringBuilder(2 * hash.length);
            for (byte b : hash) {
                sb.append(String.format("%02x", b & 0xff));
            }
            return sb.toString();
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. MD5(String text)
  2. md5(String text)
  3. md5(String text)
  4. MD5(String text)
  5. md5(String text)
  6. md5(String text)
  7. md5(String text, String charset)
  8. md5(String text, String key)
  9. md5(String txt)