Java MD5 String md5digest(String key)

Here you can find the source of md5digest(String key)

Description

mddigest

License

Apache License

Declaration

public static byte[] md5digest(String key) 

Method Source Code


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

import java.security.MessageDigest;

public class Main {
    private static MessageDigest md5Digest = null;

    public static byte[] md5digest(String key) {
        if (key == null) {
            return null;
        }/*from   w  ww.jav a2s  .  c  om*/
        MessageDigest md5;
        try {
            md5 = (MessageDigest) md5Digest.clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException("clone of MD5 not supported", e);
        }
        md5.update(key.getBytes());
        return md5.digest();
    }

    private static String digest(String key) {
        byte[] bs = md5digest(key);
        return byte2hex(bs);
    }

    private static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }
        return hs.toUpperCase();
    }
}

Related

  1. md5Base64(String str)
  2. md5ByHex(String src)
  3. md5crypt(String s)
  4. md5Digest(final InputStream data)
  5. md5Digest(final String message)
  6. md5Digest(String s)
  7. md5Digest(String text)
  8. md5Digest(String word)
  9. md5DigestPassword(String password)