【问题标题】:Java: How to create SHA-1 for a file?Java:如何为文件创建 SHA-1?
【发布时间】:2011-09-11 17:32:24
【问题描述】:

在纯 Java6 中为非常大的文件创建 SHA-1 的最佳方法是什么?如何实现此方法:

public abstract String createSha1(java.io.File file);

【问题讨论】:

    标签: java cryptography sha


    【解决方案1】:

    使用MessageDigest 类并逐个提供数据。下面的示例忽略了将 byte[] 转换为字符串和关闭文件等细节,但应该给您一个大致的概念。

    public byte[] createSha1(File file) throws Exception  {
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        InputStream fis = new FileInputStream(file);
        int n = 0;
        byte[] buffer = new byte[8192];
        while (n != -1) {
            n = fis.read(buffer);
            if (n > 0) {
                digest.update(buffer, 0, n);
            }
        }
        return digest.digest();
    }
    

    【讨论】:

    • DigestInputStream 类更易于使用。实际上可能不是,但最好尝试将其作为替代方案并与此进行比较。
    • 使用 BufferedInputStream 而不是创建自己的缓冲区:InputStream fis = new BufferedInputStream(new FileInputStream(file));
    • @Jeff Foster 您如何确定byte[] buffer 的大小为8192
    • FIleInputStream 应该像其他响应一样关闭
    【解决方案2】:

    Op 请求函数返回 SHA1 的字符串,所以我接受了@jeffs 的回答并将缺少的转换添加到字符串:

    /**
     * Read the file and calculate the SHA-1 checksum
     * 
     * @param file
     *            the file to read
     * @return the hex representation of the SHA-1 using uppercase chars
     * @throws FileNotFoundException
     *             if the file does not exist, is a directory rather than a
     *             regular file, or for some other reason cannot be opened for
     *             reading
     * @throws IOException
     *             if an I/O error occurs
     * @throws NoSuchAlgorithmException
     *             should never happen
     */
    private static String calcSHA1(File file) throws FileNotFoundException,
            IOException, NoSuchAlgorithmException {
    
        MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
        try (InputStream input = new FileInputStream(file)) {
    
            byte[] buffer = new byte[8192];
            int len = input.read(buffer);
    
            while (len != -1) {
                sha1.update(buffer, 0, len);
                len = input.read(buffer);
            }
    
            return new HexBinaryAdapter().marshal(sha1.digest());
        }
    }
    

    【讨论】:

      【解决方案3】:
      public static String computeFileSHA1( File file ) throws IOException
      {
          String sha1 = null;
          MessageDigest digest;
          try
          {
              digest = MessageDigest.getInstance( "SHA-1" );
          }
          catch ( NoSuchAlgorithmException e1 )
          {
              throw new IOException( "Impossible to get SHA-1 digester", e1 );
          }
          try (InputStream input = new FileInputStream( file );
               DigestInputStream digestStream = new DigestInputStream( input, digest ) )
          {
              while(digestStream.read() != -1){
                  // read file stream without buffer
              }
              MessageDigest msgDigest = digestStream.getMessageDigest();
              sha1 = new HexBinaryAdapter().marshal( msgDigest.digest() );
          }
          return sha1;
      }
      

      【讨论】:

      • 尽管您的答案可能是正确的,但如果您可以添加一些解释原因的评论,这将对 OP 有所帮助。这样它就会对其他人有用。
      • 最初请求与 Java6 有关。您提供的代码不会在 Java6 下编译!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-07
      • 2015-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多