Java MD5 Sum md5sum(InputStream in)

Here you can find the source of md5sum(InputStream in)

Description

mdsum

License

Open Source License

Declaration

public static String md5sum(InputStream in) throws IOException, NoSuchAlgorithmException 

Method Source Code


//package com.java2s;
/*/*  w ww  .  j a  v a  2 s .  c  o  m*/
 * Copyright (C) 2012 Joseph Areeda <joseph.areeda at ligo.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.ByteArrayOutputStream;

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

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

public class Main {
    public static String md5sum(InputStream in) throws IOException, NoSuchAlgorithmException {
        in.reset();
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] dataBytes = new byte[1024];

        int nread;
        while ((nread = in.read(dataBytes)) != -1) {
            md.update(dataBytes, 0, nread);
        }
        return getMd5String(md);
    }

    public static String md5sum(byte[] data) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(data, 0, data.length);
        return getMd5String(md);
    }

    public static String md5sum(float[] data) {
        String ret = "error";
        try {
            byte[] b = float2ByteArray(data);
            ret = md5sum(b);
        } catch (Exception ex) {
            String ermsg = "Calculating md5sum of data: " + ex.getClass().getSimpleName() + " - "
                    + ex.getLocalizedMessage();
        }
        return ret;
    }

    public static String getMd5String(MessageDigest md) {
        byte[] mdbytes = md.digest();

        //convert the byte to hex format method 1
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mdbytes.length; i++) {
            sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
        }
        return sb.toString();
    }

    /**
     * 
     * @param data
     * @return 
     */
    public static byte[] float2ByteArray(float[] data) throws IOException {
        byte[] ret;
        int len = data.length;
        ByteArrayOutputStream bos = new ByteArrayOutputStream(len * Float.SIZE / 8);
        DataOutputStream dos = new DataOutputStream(bos);
        for (int in = 0; in < len; in++) {
            dos.writeFloat(data[in]);
        }
        ret = bos.toByteArray();

        return ret;
    }
}

Related

  1. md5sum(File f, char[] cbuf, MessageDigest digest, byte[] bbuf)
  2. md5sum(File file)
  3. md5sum(File file)
  4. md5sum(File library)
  5. md5sum(InputStream file)
  6. md5sum(String data)
  7. md5sum(String input)
  8. md5sum(String inString)
  9. md5sum(String message)