Java SHA1 sha1(File f)

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

Description

sha

License

Apache License

Declaration

public static String sha1(File f) throws IOException 

Method Source Code


//package com.java2s;
/*--------------------------------------------------------------------------
 *  Copyright 2010 Taro L. Saito//from  www  .  j  av  a2  s  .com
 *
 *  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 {
    private final static char[] HEX = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C',
            'D', 'E', 'F' };

    public static String sha1(File f) throws IOException {
        return toHexString(sha1hash(f));
    }

    public static String sha1(InputStream in) throws IOException {
        return toHexString(sha1hash(in));
    }

    private static String toHexString(byte[] value) {
        StringBuilder s = new StringBuilder(value.length);
        for (int i = 0; i < value.length; ++i) {
            byte v = value[i];
            s.append(HEX[(v >>> 4) & 0x0F]);
            s.append(HEX[v & 0x0F]);
        }
        return s.toString();
    }

    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. sha1(byte[] input)
  2. sha1(byte[] input)
  3. sha1(byte[] message)
  4. SHA1(byte[] src)
  5. SHA1(ByteBuffer buf, int offset, int size)
  6. sha1(File file)
  7. sha1(File file)
  8. sha1(File sourceFile)
  9. sha1(final byte[] bytes)