Generate a SHA.1 Hash for a given file.

@param file the file to hash
@return the hash value as a String
@throws IOException if the file cannot be read
976261
public static String sha1Hash(File file) throws IOException {
        try {
            DigestInputStream inputStream = new DigestInputStream(
                    new FileInputStream(file), MessageDigest.getInstance("SHA-1"));
            try {
                byte[] buffer = new byte[4098];
                while (inputStream.read(buffer) != -1) {
                    // Read the entire stream
                }
                return bytesToHex(inputStream.getMessageDigest().digest());
            } finally {
                inputStream.close();
            }
        } catch (NoSuchAlgorithmException ex) {
            throw new IllegalStateException(ex);
        }
    }
572747
def read_sha1(
    file_path,
    buf_size = None,
    start_byte = 0,
    read_size = None,
    extra_hashers = [], # update(data) will be called on all of these
):
    '''
    
    '''
    read_size = read_size or os.stat(file_path).st_size
    buf_size = buf_size or DEFAULT_BUFFER_SIZE

    data_read = 0
    total_sha1 = hashlib.sha1()
    while data_read < read_size:
        with open( file_path, 'rb', buffering = 0 ) as f:
            f.seek( start_byte )
            data = f.read( min(buf_size, read_size - data_read) )
            assert( len(data) > 0 )
            total_sha1.update( data )
            for hasher in extra_hashers:
                hasher.update( data )
            data_read += len(data)
            start_byte += len(data)
    assert( data_read == read_size )

    return total_sha1
110265
function processFileBySha1Hash(stream, firstPass, basePath, file, cache) {
  // null cannot be hashed
  if (file.contents === null) {
    // if element is really a file, something weird happened, but it's safer
    // to assume it was changed (because we cannot said that it wasn't)
    // if it's not a file, we don't care, do we? does anybody transform directories?
    if (file.stat.isFile()) {
      stream.push(file);
    }
  } else {
    var newHash = crypto.createHash('sha1').update(file.contents).digest('hex');
    var filePath = basePath ? path.relative(basePath, file.path) : file.path;
    var currentHash = cache[filePath];

    cache[filePath] = newHash;

    if ((!currentHash && firstPass) || (currentHash && currentHash !== newHash)) {
      stream.push(file);
    }
  }
}
208924
public static String file2String(File file) throws IOException {
        if (file == null || !file.exists() || !file.isFile() || !file.canRead()) {
            return null;
        }
        FileReader reader = null;
        StringWriter writer = null;
        try {
            reader = new FileReader(file);
            writer = new StringWriter();
            char[] cbuf = new char[1024];
            int len = 0;
            while ((len = reader.read(cbuf)) != -1) {
                writer.write(cbuf, 0, len);
            }
            return writer.toString();
        } finally {
            IOUtils.closeQuietly(reader);
            IOUtils.closeQuietly(writer);
        }
    }
1095878
public Map<ChecksumType, String> calculateJavaScriptHashes(File file) throws WssHashException {
        Map<ChecksumType, String> checksums = new EnumMap<>(ChecksumType.class);
        try {
            long fileLength = file.length();
            if (fileLength >= FILE_MAX_SIZE_THRESHOLD){
                logger.debug("Ignore file {}, ({}): maximum file size  is 2GB", file.getName(), FileUtils.byteCountToDisplaySize(fileLength));
                return checksums;
            }
            checksums = calculateJavaScriptHashes(FileUtils.readFileToByteArray(file));
        } catch (Exception e) {
            throw new WssHashException("Error calculating JavaScript hash: "+ e.getMessage());
        }
        return checksums;
    }
1060068
public static String md5AsBase64(File file) throws FileNotFoundException, IOException {
        return Base64.encodeAsString(computeMD5Hash(file));
    }
1016358
def sha1sum(filename):
    
    sha1 = hashlib.sha1()
    with open(filename, 'rb') as f:
        for chunk in iter(lambda: f.read(128 * sha1.block_size), b''):
            sha1.update(chunk)
    return sha1.hexdigest()
258703
def hash_file(fileobj):
    
    hasher = hashlib.md5()
    buf = fileobj.read(65536)
    while len(buf) > 0:
        hasher.update(buf)
        buf = fileobj.read(65536)
    return hasher.hexdigest()
875277
def getHashForFile(f):
    
    hashVal = hashlib.sha1()
    while True:
        r = f.read(1024)
        if not r:
            break
        hashVal.update(r)
    f.seek(0)

    return hashVal.hexdigest()
203910
public static String readFile(File file) throws IOException {
        StringBuilder content = new StringBuilder();
        String line;
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            while ((line = reader.readLine()) != null) {
                content.append(line).append("\r\n");
            }
        }
        return content.toString();
    }
252134
def _filehash(filepath, blocksize=4096):
    
    sha = hashlib.sha256()
    with open(filepath, 'rb') as fp:
        while 1:
            data = fp.read(blocksize)
            if data:
                sha.update(data)
            else:
                break
    return sha
802005
def _hashfile(self,filename,blocksize=65536):
        """"""
        logger.debug("Hashing file %s"%(filename))
        hasher=hashlib.sha256()
        afile=open(filename,'rb')
        buf=afile.read(blocksize)
        while len(buf) > 0:
            hasher.update(buf)
            buf = afile.read(blocksize)
        return hasher.hexdigest()
362183
public static byte[] bytes(File file) throws IOException {
        InputStream in = null;
        try {
            in = openInputStream(file);
            return toByteArray(in, file.length());
        } finally {
            closeQuietly(in);
        }
    }
848891
def hash_file(filename):
   """""""

   # make a hash object
   h = hashlib.sha1()

   # open file for reading in binary mode
   with open(filename,'rb') as file:

       # loop till the end of the file
       chunk = 0
       while chunk != b'':
           # read only 1024 bytes at a time
           chunk = file.read(1024)
           h.update(chunk)

   # return the hex representation of digest
   return h.hexdigest()
71118
def sha1_of_file(filepath):
    
    import hashlib
    try:
        with open(filepath, 'rb') as file_to_hash:
            return hashlib.sha1(file_to_hash.read()).hexdigest()
    except:  # pylint: disable=bare-except
        return None
1352447
public static byte[] readFileAtOnce(final File file) throws IOException {
		final FileInputStream fIn = new FileInputStream(file);
		return readFileAtOnce(fIn);
	}
1074599
public static String fromFile(File file) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(file));
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            sb.append(line);
            line = br.readLine();
            if (line != null)
                sb.append(System.lineSeparator());
        }

        br.close();
        return sb.toString();
    }