Java MD5 String md5(String text)

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

Description

md

License

Apache License

Declaration

public static String md5(String text) 

Method Source Code


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

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

public class Main {

    public static String md5(String text) {
        MessageDigest msgDigest = null;

        try {/* w  ww.  ja v  a2 s .  c  om*/
            msgDigest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException("System doesn't support MD5 algorithm.");
        }

        try {
            msgDigest.update(text.getBytes("UTF-8"));
        } catch (Exception e) {

        }

        byte[] bytes = msgDigest.digest();

        byte tb;
        char low;
        char high;
        char tmpChar;

        String md5Str = new String();

        for (int i = 0; i < bytes.length; i++) {
            tb = bytes[i];

            tmpChar = (char) ((tb >>> 4) & 0x000f);

            if (tmpChar >= 10) {
                high = (char) (('a' + tmpChar) - 10);
            } else {
                high = (char) ('0' + tmpChar);
            }

            md5Str += high;
            tmpChar = (char) (tb & 0x000f);

            if (tmpChar >= 10) {
                low = (char) (('a' + tmpChar) - 10);
            } else {
                low = (char) ('0' + tmpChar);
            }

            md5Str += low;
        }

        return md5Str;
    }
}

Related

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