Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximal SHA-1 Hash Performance Tips in Java

I'm writing a Java library that needs to compute SHA-1 hashes. During a common task, the JVM spends about 70% of its time in sun.security.provider.SHA.implCompress, 10% in java.util.zip.Inflater.inflate, and 2% in sun.security.provider.ByteArrayAccess.b2iBig64. (According to NetBeans profiler.)

I can't seem to get the Google search keywords right to get relevant results. I'm not very familiar with the SHA-1 hash algorithm. How can I get the most performance out of an SHA-1 MessageDigest? Is there a certain chunk size I should be digesting, or multiples of certain sizes I should try?

To answer some questions you're thinking about asking:

  • Yes, I'm digesting as I read the files (MessageDigest.update), so bytes are only digested once.
  • The SHA-1 digests are being used as checksums, usually for files that need to be zlib/inflated.
  • No, I can't use a different hash.
  • Yes, I know zlib already uses checksums, but external requirements specify the use of SHA-1 hashes on top of that. I can't come up with a good reason why (+1 if you can) :-)
like image 211
Mutant Platypus Avatar asked Mar 14 '12 21:03

Mutant Platypus


People also ask

How many buffers are used in SHA-1?

According to SHA-1 standard, a message digest is evaluated utilizing padded message. The evaluation utilizes two buffers, each comprises of five 32 bit words and a sequence of eighty 32 bit words.

Is SHA-1 slow?

While the speed of SHA1 is slow in comparison of MD5's speed.

Is SHA-1 faster than SHA256?

SHA-256 is 15.5% slower than SHA-1 for short strings and 23.4% for longer strings. SHA-512 is 51.7% slower that SHA-1 for short strings and 20% for longer.

How long is SHA-1 hash?

The hash size for the SHA1 algorithm is 160 bits.


1 Answers

Maybe you can call out to native code written in C. There must be a ton of super optimized SHA1 libraries available.

like image 190
usr Avatar answered Sep 28 '22 10:09

usr