« MD5() :: MySQL Function of the Day | Main | MySQL Function of the Day: Week Six »
CRC32 :: MySQL Function of the Day
By Carsten | April 18, 2008
Function name: CRC32
Aliases: -
Function type: Numerical function
Purpose: Return a Cyclic Redundancy Check value of a string
Description: CRC32() computes a “cyclic redundancy check” value of the input string. It may be used in many of the same places as MD5() (which we discussed yesterday). Historically, CRC has been used to detect noise in transmission signals, whereas MD5 is used more in the area of encryption and authentication of large-size data. Both may be used for hashing, and while not as strong, CRC32 has a couple of advantages over MD5:
- The calculated hash value is much smaller: 32 bits rather than MD5s 128 bit. In addition, CRC32 values are generally treated as integers, whereas MD5 values are most often used in their 32-char hexadecimal representation, which takes up 256 bits. By using CRC32 in place of you’ll be reducing the storage requirements per field by 12 to 28 bytes.
- CRC32 calculations are much faster than calculating the MD5 value of a string. Using BENCHMARK() (which, not quite coincidentally, was the subject of FotD two days ago), CRC32 on my systems appears to be some 40 times faster than corresponding MD5 calculations.
Of course there are disadvantages, too:
- Collisions are more likely to occur, mainly because of the smaller hash value, but also because of the algorithm design.
- CRC32 should never be used for authentication or encryption, as it’s very easy to forge a message to have the same CRC32 value as another.
If hash values are all you want, then you should definitely prefer CRC32 over MD5.
MySQL Function of the Day is a small series of concise information regarding most of the functions and operators available in MySQL. Inspired by PHPs funcaday, it’s meant to provide a daily dose of something in-between “Aha!” and “Ho-hum, I knew that already”, depending on your level of experience with MySQL. You can access the entire series with your browser here or pick up the RSS feed here.
Topics: MySQL Function of the day |