获取整个文件的SHA1

@param fileInputStream
文件的输入流
@return 文件对应的SHA1值
@throws Exception
965388
public static String getEntireFileSha1(String filePath) throws Exception {
		InputStream fileInputStream = null;
		try {
			fileInputStream = CommonFileUtils.getFileInputStream(filePath);
			String sha1Digest = DigestUtils.sha1Hex(fileInputStream);
			return sha1Digest;
		} catch (Exception e) {
			String errMsg = "getFileSha1 occur a exception, file:" + filePath + ", exception:" + e.toString();
			LOG.error(errMsg);
			throw new Exception(errMsg);
		} finally {
			try {
				CommonFileUtils.closeFileStream(fileInputStream, filePath);
			} catch (Exception e) {
				throw e;
			}
		}
	}
5957
public File writeFromStream(InputStream in) throws IORuntimeException {
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(FileUtil.touch(file));
			IoUtil.copy(in, out);
		}catch (IOException e) {
			throw new IORuntimeException(e);
		} finally {
			IoUtil.close(out);
		}
		return file;
	}
576738
public InputStream getAsStream() throws CacheException {
		try {
			if(file.exists() && file.isFile()) {
				logger.debug("retrieving file as a stream");
				return new FileInputStream(file);			
			}
		} catch(IOException e) {
			logger.error("file '{}' does not exist or is not a file", file.getAbsolutePath());
			throw new CacheException("File " + file.getAbsolutePath() + " does not exist or is not a regular file", e);
		}		
		return null;
	}
1323972
public FileInputStream getFileInputStream(final File file) throws FileNotFoundException {
        if (System.getSecurityManager() == null)
            return new FileInputStream(file);
        try {
            return AccessController.doPrivileged(new PrivilegedExceptionAction<FileInputStream>() {
                @Override
                public FileInputStream run() throws FileNotFoundException {
                    return new FileInputStream(file);
                }
            }, controlContext);
        } catch (PrivilegedActionException e) {
            if (e.getException() instanceof FileNotFoundException)
                throw (FileNotFoundException) e.getException();
            throw (RuntimeException) e.getException();
        }
    }
1261513
public static InputStream fileAsStream(String filename) {
    File classpathFile = getClasspathFile(filename);
    return fileAsStream(classpathFile);
  }
533477
public static FileInputStream getFileInputStream(String filePath) throws Exception {
        FileInputStream localFileInputStream = new FileInputStream(filePath);
        return localFileInputStream;
    }
911541
public static byte[] md5(File file) throws IOException {
		try (InputStream in = new FileInputStream(file)) {
			return md5(in);
		}
	}
1262150
public Long gitHash(File input) throws IOException {
        String path = getLogicalPath(input);
        String blob = "blob " + path.length() + "\0" + path;
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA-1");
            byte[] bytes = md.digest(blob.getBytes());
            String h = byteArrayToHexString(bytes).substring(0, 7);
            return Long.parseLong(h, 16);
        }
        catch (NoSuchAlgorithmException ex) {
            throw new IOException(ex.getMessage(), ex);
        }
    }
825086
public String sha1() {
        try {
            final SecretKeySpec keySpec = new SecretKeySpec(secret.getPlainText().getBytes(UTF_8), HMAC_SHA1_ALGORITHM);
            final Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
            mac.init(keySpec);
            final byte[] rawHMACBytes = mac.doFinal(payload.getBytes(UTF_8));

            return Hex.encodeHexString(rawHMACBytes);
        } catch (Exception e) {
            LOGGER.error("", e);
            return INVALID_SIGNATURE;
        }
    }
736007
private static InputStream getFileAsInputStream(String file) throws InitializationException {
		if (file == null) {
			throw new NullPointerException("File is null");
		}
		try {
			return new FileInputStream(new File(file));
		} catch (FileNotFoundException e) {
			throw new InitializationException("File not found: " + file);
		}
	}
985587
def sha1(filename):
    """"""
    filename = unmap_file(filename)
    if filename not in file_cache:
        cache_file(filename)
        if filename not in file_cache:
            return None
        pass
    if file_cache[filename].sha1:
        return file_cache[filename].sha1.hexdigest()
    sha1 = hashlib.sha1()
    for line in file_cache[filename].lines['plain']:
        sha1.update(line.encode('utf-8'))
        pass
    file_cache[filename].sha1 = sha1
    return sha1.hexdigest()
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);
    }
  }
}
909831
public byte[] getFileBytes(File file) throws IORuntimeException {
		byte[] bytes = cache.get(file);
		if (bytes != null) {
			return bytes;
		}

		// add file
		bytes = FileUtil.readBytes(file);

		if ((maxFileSize != 0) && (file.length() > maxFileSize)) {
			//大于缓存空间，不缓存，直接返回
			return bytes;
		}

		usedSize += bytes.length;

		//文件放入缓存，如果usedSize > capacity，purge()方法将被调用
		cache.put(file, bytes);

		return bytes;
	}
915779
public static String fileAsString(File file) {
    try {
      return inputStreamAsString(new FileInputStream(file));
    } catch(FileNotFoundException e) {
      throw LOG.fileNotFoundException(file.getAbsolutePath(), e);
    }
  }
1272886
def get_file_sha1(filename_or_io):
    '''
    
    '''

    file_data = get_file_io(filename_or_io)
    cache_key = file_data.cache_key

    if cache_key and cache_key in FILE_SHAS:
        return FILE_SHAS[cache_key]

    with file_data as file_io:
        hasher = sha1()
        buff = file_io.read(BLOCKSIZE)

        while len(buff) > 0:
            if isinstance(buff, six.text_type):
                buff = buff.encode('utf-8')

            hasher.update(buff)
            buff = file_io.read(BLOCKSIZE)

    digest = hasher.hexdigest()

    if cache_key:
        FILE_SHAS[cache_key] = digest

    return digest
118536
public InputStream getInputStream(String fileName, boolean create) throws MalformedURLException, IOException {
        try {
            GetKeyStoreInputStreamAction action = new GetKeyStoreInputStreamAction(fileName, create);
            return AccessController.doPrivileged(action);
        } catch (PrivilegedActionException e) {
            Exception ex = e.getException();
            FFDCFilter.processException(e, getClass().getName(), "getInputStream", new Object[] { fileName, Boolean.valueOf(create), this });
            if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled())
                Tr.debug(tc, "Exception opening keystore; " + ex);

            if (ex instanceof MalformedURLException)
                throw (MalformedURLException) ex;
            else if (ex instanceof IOException)
                throw (IOException) ex;

            throw new IOException(ex.getMessage());
        }
    }
751083
public static String hash(InputStream in) {
        try {
            MessageDigest digest = MessageDigest.getInstance("sha1");

            ByteArrayPool.withByteArray(buffer -> {
                try {
                    int length;
                    while ((length = in.read(buffer)) != -1) {
                        digest.update(buffer, 0, length);
                    }
                } catch (IOException e) {
                    throw Exceptions.propagate(e);
                }
            });

            return String.format("%040x", new BigInteger(1, digest.digest()));
        } catch (NoSuchAlgorithmException e) {
            throw Exceptions.propagate(e);
        }
    }