Java SHA1 sha1(File file)

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

Description

sha

License

Open Source License

Declaration

public static final String sha1(File file) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
import java.util.Formatter;

public class Main {
    public static final String sha1(File file) {
        try {//from   w  w  w  .ja  v a 2  s . c  o  m
            MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
            try (InputStream stream = new BufferedInputStream(new FileInputStream(file))) {
                byte[] buffer = new byte[8192];

                int read = 0;
                while ((read = stream.read(buffer)) != -1) {
                    messageDigest.update(buffer, 0, read);
                }
            }

            try (Formatter formatter = new Formatter()) {
                for (byte b : messageDigest.digest()) {
                    formatter.format("%02x", b);
                }

                return formatter.toString();
            }
        } catch (Exception ex) {
        }
        return null;
    }
}

Related

  1. SHA1(byte[] input)
  2. sha1(byte[] message)
  3. SHA1(byte[] src)
  4. SHA1(ByteBuffer buf, int offset, int size)
  5. sha1(File f)
  6. sha1(File file)
  7. sha1(File sourceFile)
  8. sha1(final byte[] bytes)
  9. sha1(final File file)