Java MD5 String md5(String str)

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

Description

md

License

Open Source License

Declaration

public static String md5(String str) throws NoSuchAlgorithmException 

Method Source Code

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

import java.io.IOException;
import java.io.InputStream;

import java.net.MalformedURLException;
import java.net.URI;

import java.security.*;

public class Main {
    private static final char[] binMap = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
            'C', 'D', 'E', 'F' };

    public static String md5(String str) throws NoSuchAlgorithmException {
        return hexEncode(md5(str.getBytes()));
    }/*from  www .  j a va2 s . co m*/

    public static byte[] md5(byte[] bytes) throws NoSuchAlgorithmException {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        return md5.digest(bytes);
    }

    public static byte[] md5(URI uri) throws NoSuchAlgorithmException, MalformedURLException, IOException {
        byte[] bytes = new byte[8192];
        int read = 0;
        MessageDigest md5 = MessageDigest.getInstance("MD5");

        InputStream in = uri.toURL().openStream();
        while ((read = in.read(bytes)) != -1) {
            md5.update(bytes, 0, read);
        }
        in.close();

        return md5.digest();
    }

    public static String hexEncode(byte[] bytes) {
        StringBuilder buff = new StringBuilder(bytes.length * 2);
        for (int i = 0; i < bytes.length; ++i) {
            byte b = bytes[i];
            int b1 = (b >> 4) & 0xF;
            int b2 = (b) & 0xF;
            buff.append(binMap[b1]);
            buff.append(binMap[b2]);
        }

        return buff.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 str)
  8. MD5(String str)
  9. md5(String str)