Java: Calculate SHA-256 hash of large file efficiently

前端 未结 7 1569
说谎
说谎 2020-12-13 07:33

I need to calculate a SHA-256 hash of a large file (or portion of it). My implementation works fine, but its much slower than the C++\'s CryptoPP calculation (25 Min. vs. 10

7条回答
  •  悲哀的现实
    2020-12-13 08:15

    Perhaps the first thing today is work out where you are spending the most time? Can you run it through a profiler and see where the most time is being spent.

    Possible improvements:

    1. Use NIO to read the file in the fastest possible way
    2. Update the Hash in a separate thread. This is actually rather hard to do and isn't for the faint hearted as it involves safe publishing between threads. But if your profiling shows a significant amount of time being spent in hash algorithm it may make better use of the disk.

提交回复
热议问题