Java SHA1 sha1sum(File file)

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

Description

shasum

License

Apache License

Declaration

public static String sha1sum(File file) throws FileNotFoundException 

Method Source Code

//package com.java2s;
/**//from  w w  w. j  av a  2 s  .c o m
 * Copyright 2012-2013 France T?l?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.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;
import java.security.DigestInputStream;
import java.security.MessageDigest;

public class Main {
    public static String sha1sum(File file) throws FileNotFoundException {
        String localSha1Sum = null;
        if (file.exists() && file.isFile() && file.canRead()) {
            DigestInputStream dis = null;
            try {
                MessageDigest md = MessageDigest.getInstance("SHA-1");
                dis = new DigestInputStream(new FileInputStream(file), md);
                dis.on(true);

                while (dis.read() != -1) {
                    // do nothing, but loop !
                }
                byte[] b = md.digest();
                localSha1Sum = getHexString(b);
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                if (dis != null) {
                    try {
                        dis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } else {
            throw new FileNotFoundException("file not found " + file.getName());
        }
        return localSha1Sum;
    }

    private static String getHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder(bytes.length * 2);
        for (byte b : bytes) {
            if (b <= 0x0F && b >= 0x00)
                sb.append('0');
            sb.append(String.format("%x", b));
        }
        return sb.toString();
    }
}

Related

  1. sha1hex(String source)
  2. sha1Java(String password)
  3. sha1Sum(byte[] ba)
  4. sha1sum(byte[] data, Integer startIdxInc, Integer stopIdxExc)
  5. sha1sum(byte[] input, int length)
  6. sha1sum(File file)
  7. SHA1Sum(final @Nonnull byte[] bytes)
  8. sha1sum(final String data)
  9. sha1sum(InputStream file)