Search code examples
javaalgorithmsecuritysha

How to decrease the size of SHA1?


I have a problem, maybe a silly question, I want to store data in a database after I hash with the SHA1 algorithm. However, at a future time, the size in database will increase because size words in SHA1 is big.

Can we decrease the size of SHA1 algorithm, maybe half the size. I'm sorry for my silly question, and for my bad English. Thanks. :D

I am using JAVA.


Solution

  • Is 20 bytes per hash(assuming binary storage) really too much? If you currently use hex encoding switching to binary saves you 20 bytes per hash. Base64 saves about 10 bytes compared to hex.

    If you simply truncate a cryptographic hash it is still a good cryptographic hash, but with a reduced output size. What output size you need depends on your application.

    Integrity checks against random changes can use a much shorter hash of 32-64 bits and don't need a cryptographic hash functions.

    If you need uniqueness you should have >>2*log_2(entries) bits in your hash (See birthday paradox). At around 120 bits it's similar to a GUID/UUID (There is a sha1 based generation mode for GUIDs)

    If you want cryptographic strength I'd avoid going below 128bits.