Java-实现文件sha1编码获取

代码示例

/**
 * 获取文件的sha1值。
 */
public static String sha1(File file) {
    FileInputStream in = null;
    try {
        in = new FileInputStream(file);
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        byte[] buffer = new byte[1024 * 1024 * 10];
        int len = 0;
        while ((len = in.read(buffer)) > 0) {
            digest.update(buffer, 0, len);
        }
        String sha1 = new BigInteger(1, digest.digest()).toString(16);
        int length = 40 - sha1.length();
        if (length > 0) {
            for (int i = 0; i < length; i++) {
                sha1 = "0" + sha1;
            }
        }
        return sha1;
    } 
    catch (Exception e) { } 
    finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException e) { }
    }
    return null;
}
上一篇:Java-实现文件的MD5编码获取


下一篇:算法-快速排序算法