Java MD5 Sum md5sum(File file)

Here you can find the source of md5sum(File file)

Description

mdsum

License

Apache License

Declaration

public static String md5sum(File file) throws IOException 

Method Source Code


//package com.java2s;
/*--------------------------------------------------------------------------
 *  Copyright 2010 Taro L. Saito//from  w w  w .  ja va  2 s.c  o m
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *--------------------------------------------------------------------------*/

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

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

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

public class Main {
    public static String md5sum(File file) throws IOException {
        return md5sum(new BufferedInputStream(new FileInputStream(file)));
    }

    /**
     * Computes the MD5 value of the input stream
     * 
     * @param input
     * @return
     * @throws IOException
     * @throws NoSuchAlgorithmException
     */
    public static String md5sum(InputStream in) throws IOException {

        try {
            MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
            DigestInputStream digestInputStream = new DigestInputStream(in, digest);
            for (; digestInputStream.read() >= 0;) {

            }
            ByteArrayOutputStream md5out = new ByteArrayOutputStream();
            md5out.write(digest.digest());
            return md5out.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException("MD5 algorithm is not available: " + e);
        } finally {
            in.close();
        }
    }
}

Related

  1. md5Sum(byte[] data)
  2. md5sum(byte[] input, int length)
  3. MD5Sum(byte[] par1Data)
  4. md5sum(File f)
  5. md5sum(File f, char[] cbuf, MessageDigest digest, byte[] bbuf)
  6. md5sum(File file)
  7. md5sum(File library)
  8. md5sum(InputStream file)
  9. md5sum(InputStream in)