Java SHA1 sha1(final File file)

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

Description

sha

License

Open Source License

Declaration

public static String sha1(final File file) throws NoSuchAlgorithmException, IOException 

Method Source Code

//package com.java2s;
/**********/*from www.  j a v  a2s.  c om*/
Copyright ? 2013-2014 Olanto Foundation Geneva
    
   This file is part of myTERM.
    
   myCAT is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
    
myCAT is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Affero General Public License for more details.
    
You should have received a copy of the GNU Affero General Public License
along with myCAT.  If not, see <http://www.gnu.org/licenses/>.
    
**********/

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

public class Main {
    public static String sha1(final File file) throws NoSuchAlgorithmException, IOException {
        final MessageDigest messageDigest = MessageDigest.getInstance("SHA1");

        try (InputStream is = new BufferedInputStream(new FileInputStream(file))) {
            final byte[] buffer = new byte[1024];
            for (int read = 0; (read = is.read(buffer)) != -1;) {
                messageDigest.update(buffer, 0, read);
            }
        }

        return sha12String(messageDigest);

    }

    public static String sha1(String content) throws NoSuchAlgorithmException, IOException {
        final MessageDigest messageDigest = MessageDigest.getInstance("SHA1");

        try (InputStream is = new ByteArrayInputStream(content.getBytes("UTF-8"))) {
            final byte[] buffer = new byte[1024];
            for (int read = 0; (read = is.read(buffer)) != -1;) {
                messageDigest.update(buffer, 0, read);
            }
        }

        return sha12String(messageDigest);

    }

    public static String sha12String(MessageDigest messageDigest) {
        // Conversion des bytes en   format hex
        try (Formatter formatter = new Formatter()) {
            for (final byte b : messageDigest.digest()) {
                formatter.format("%02x", b);
            }
            return formatter.toString();
        }
    }
}

Related

  1. sha1(File f)
  2. sha1(File file)
  3. sha1(File file)
  4. sha1(File sourceFile)
  5. sha1(final byte[] bytes)
  6. sha1(final String data)
  7. sha1(final String str)
  8. sha1(final String string)
  9. SHA1(final String text)