Java MD5 Hash md5Hash(String buf)

Here you can find the source of md5Hash(String buf)

Description

md Hash

License

Open Source License

Declaration

public static String md5Hash(String buf) 

Method Source Code

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

import java.io.BufferedInputStream;

import java.io.File;
import java.io.FileInputStream;

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

import java.io.UnsupportedEncodingException;

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

public class Main {
    public static final String US_ASCII = "US-ASCII";

    public static String md5Hash(String buf) {
        try {/*from ww w.  j  av a2  s.  com*/
            return md5Hash(buf.getBytes(US_ASCII));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static String md5Hash(File in) throws IOException {
        return hash(new BufferedInputStream(new FileInputStream(in)), "MD5");
    }

    public static String md5Hash(InputStream in) throws IOException {
        return hash(in, "MD5");
    }

    public static String md5Hash(byte[] buf) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(buf);
            return convertToHex(md.digest());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public static String hash(File f, String algo) throws IOException {
        return hash(new BufferedInputStream(new FileInputStream(f), 65536),
                algo);
    }

    public static String hash(File f, MessageDigest md) throws IOException {
        return hash(new BufferedInputStream(new FileInputStream(f), 65536),
                md);
    }

    public static String hash(InputStream in, String algo)
            throws IOException {
        try {
            MessageDigest md = MessageDigest.getInstance(algo);
            return hash(in, md);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public final static String hash(final InputStream in,
            final MessageDigest md) throws IOException {
        try {
            boolean done = false;
            final byte[] buf = new byte[65536];
            while (!done) {
                final int length = in.read(buf);
                if (length <= 0) {
                    done = true;
                } else {
                    md.update(buf, 0, length);
                }
            }
            return convertToHex(md.digest());
        } finally {
            in.close();
        }
    }

    public static String convertToHex(final byte[] buf) {
        if (buf == null) {
            return null;
        }
        final StringBuffer out = new StringBuffer();
        final int n = buf.length;
        for (int i = 0; i < n; i++) {
            out.append(convertLowerBitsToHex((buf[i] >> 4) & 0x0f));
            out.append(convertLowerBitsToHex(buf[i] & 0x0f));
        }
        return out.toString();
    }

    private static char convertLowerBitsToHex(int b) {
        switch (b) {
        case 0:
            return '0';
        case 1:
            return '1';
        case 2:
            return '2';
        case 3:
            return '3';
        case 4:
            return '4';
        case 5:
            return '5';
        case 6:
            return '6';
        case 7:
            return '7';
        case 8:
            return '8';
        case 9:
            return '9';
        case 10:
            return 'a';
        case 11:
            return 'b';
        case 12:
            return 'c';
        case 13:
            return 'd';
        case 14:
            return 'e';
        case 15:
            return 'f';
        }
        throw new IllegalArgumentException(
                "can't convert to hex character: " + b);
    }
}

Related

  1. md5Hash(byte[] data)
  2. md5Hash(byte[] data)
  3. md5hash(byte[] input)
  4. md5Hash(byte[] key)
  5. md5Hash(String algorithm, String input)
  6. md5Hash(String content)
  7. md5Hash(String in)
  8. md5Hash(String input)
  9. md5hash(String key)