Java MD5 String md5(String data)

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

Description

md

License

Apache License

Declaration

public static String md5(String data) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
 * Copyright (c) 2011- kotemaru@kotemaru.org
 ******************************************************************************/

import java.security.MessageDigest;

public class Main {
    private final static String MD = "MD5";
    private final static String UTF8 = "UTF-8";
    private final static char[] HEX_MAP = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
            'E', 'F' };

    public static String md5(String data) {
        try {/*from   w w  w  .  j a  v a  2  s  .c o  m*/
            MessageDigest md = MessageDigest.getInstance(MD);
            md.update(data.getBytes(UTF8));
            return encodeHex(md.digest());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String encodeHex(String str) {
        try {
            return encodeHex(str.getBytes(UTF8));
        } catch (java.io.UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static String encodeHex(byte[] bytes) {
        char[] buff = new char[bytes.length * 2];
        for (int i = 0; i < bytes.length; i++) {
            int h0 = (bytes[i] >> 4) & 0x0f;
            int h1 = bytes[i] & 0x0f;
            buff[i * 2] = HEX_MAP[h0];
            buff[i * 2 + 1] = HEX_MAP[h1];
        }
        return new String(buff);
    }
}

Related

  1. md5(String content)
  2. md5(String content)
  3. md5(String d)
  4. md5(String data)
  5. md5(String data)
  6. md5(String data)
  7. md5(String data)
  8. md5(String encryptStr)
  9. MD5(String input)