Java SHA1 sha1hash(File f)

Here you can find the source of sha1hash(File f)

Description

shahash

License

Apache License

Declaration

public static byte[] sha1hash(File f) throws IOException 

Method Source Code


//package com.java2s;
/*--------------------------------------------------------------------------
 *  Copyright 2010 Taro L. Saito/* ww w  . j  a  v  a2  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 byte[] sha1hash(File f) throws IOException {
        return sha1hash(new BufferedInputStream(new FileInputStream(f)));
    }

    public static byte[] sha1hash(InputStream input) throws IOException {
        final int SHA1_LENGTH = 20;
        try {
            MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1");
            DigestInputStream digestInputStream = new DigestInputStream(input, digest);
            for (; digestInputStream.read() >= 0;) {

            }
            ByteArrayOutputStream sha1out = new ByteArrayOutputStream(SHA1_LENGTH);
            sha1out.write(digest.digest());
            return sha1out.toByteArray();
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException("SHA-1 algorithm is not available: " + e);
        }
    }
}

Related

  1. SHA1Encode(String sourceString)
  2. sha1Hash()
  3. sha1hash(byte[] blob)
  4. sha1Hash(byte[] data)
  5. sha1hash(Collection nquads)
  6. sha1Hash(File file)
  7. sha1Hash(final String tohash)
  8. sha1Hash(InputStream in)
  9. sha1Hash(String content)