When is CRC more appropriate to use than MD5/SHA1?

后端 未结 13 1500
走了就别回头了
走了就别回头了 2020-11-28 01:46

When is it appropriate to use CRC for error detection versus more modern hashing functions such as MD5 or SHA1? Is the former easier to implement on embedded hardware?

13条回答
  •  眼角桃花
    2020-11-28 02:01

    I ran every line of this PHP code in 1.000.000 loop. Results are in comments (#).

    hash('crc32', 'The quick brown fox jumped over the lazy dog.');#  750ms   8 chars
    hash('crc32b','The quick brown fox jumped over the lazy dog.');#  700ms   8 chars
    hash('md5',   'The quick brown fox jumped over the lazy dog.');#  770ms  32 chars
    hash('sha1',  'The quick brown fox jumped over the lazy dog.');#  880ms  40 chars
    hash('sha256','The quick brown fox jumped over the lazy dog.');# 1490ms  64 chars
    hash('sha384','The quick brown fox jumped over the lazy dog.');# 1830ms  96 chars
    hash('sha512','The quick brown fox jumped over the lazy dog.');# 1870ms 128 chars
    

    My conclusion:

    • Use "crc32b" when you need http://en.wikipedia.org/wiki/Cyclic_redundancy_check and you do not care about security.
    • Use "sha256" (or higher) when you need added security layer.

    • Do not use "md5" or "sha1" because they have:

      1. some security issues when you care about security
      2. longer hash string and are slower than "crc32b" when all you need is CRC

提交回复
热议问题