How the CRC32() function works in Mariadb?

The CRC32() function is a string function that returns the cyclic redundancy check (CRC) value of a given string. The function returns an unsigned 32-bit integer value that represents the CRC-32 checksum of the string.

Posted on

The CRC32() function is a string function that returns the cyclic redundancy check (CRC) value of a given string. The function returns an unsigned 32-bit integer value that represents the CRC-32 checksum of the string. The function can be used to detect errors or changes in data.

Syntax

The syntax of the CRC32() function is as follows:

CRC32(str)

The function takes one argument, str, which is the string to be processed. The argument can be a string or an expression that evaluates to a string. The function returns NULL if the argument is NULL or invalid.

Examples

Example 1: Calculating the CRC32 value of a string

The following example uses the CRC32() function to calculate the CRC32 value of the string ‘Hello, world’.

SELECT CRC32('Hello, world');

The output is:

+---------------------+
| CRC32('Hello, world') |
+---------------------+
|          3964322768 |
+---------------------+

The output shows that the CRC32 value of the string ‘Hello, world’ is 3964322768.

Example 2: Calculating the CRC32 value of a binary string

The following example uses the CRC32() function to calculate the CRC32 value of the binary string ‘0b01010101’.

SELECT CRC32('0b01010101');

The output is:

+----------------------+
| CRC32('0b01010101') |
+----------------------+
|           2743272264 |
+----------------------+

The output shows that the CRC32 value of the binary string ‘0b01010101’ is 2743272264.

Example 3: Calculating the CRC32 value of a numeric string

The following example uses the CRC32() function to calculate the CRC32 value of the numeric string ‘123456789’.

SELECT CRC32('123456789');

The output is:

+---------------------+
| CRC32('123456789') |
+---------------------+
|          3421780262 |
+---------------------+

The output shows that the CRC32 value of the numeric string ‘123456789’ is 3421780262.

Example 4: Calculating the CRC32 value of an empty string

The following example uses the CRC32() function to calculate the CRC32 value of an empty string.

SELECT CRC32('');

The output is:

+----------+
| CRC32('') |
+----------+
|        0 |
+----------+

The output shows that the CRC32 value of an empty string is 0, as expected.

Example 5: Calculating the CRC32 value of an invalid string

The following example uses the CRC32() function to calculate the CRC32 value of an invalid string. The invalid string is NULL.

SELECT CRC32(NULL);

The output is:

+------------+
| CRC32(NULL) |
+------------+
|       NULL |
+------------+

The output shows that the function returns NULL, as expected. The function returns NULL if the argument is NULL or invalid.

There are some other functions that are related to the CRC32() function in Mariadb. They are:

  • MD5(): This function returns the MD5 hash value of a given string. The function returns a 32-character hexadecimal string that represents the MD5 checksum of the string. The function can be used to generate unique identifiers or verify data integrity.
  • SHA1(): This function returns the SHA-1 hash value of a given string. The function returns a 40-character hexadecimal string that represents the SHA-1 checksum of the string. The function can be used to generate secure hashes or digital signatures.
  • SHA2(): This function returns the SHA-2 hash value of a given string. The function returns a hexadecimal string that represents the SHA-2 checksum of the string. The function can be used to generate more secure hashes or digital signatures than SHA-1.
  • UNHEX(): This function converts a hexadecimal string to a binary string. The function returns a binary string that represents the hexadecimal string. The function can be used to decode hexadecimal strings or CRC32 values.

Conclusion

The CRC32() function is a useful function to calculate the CRC32 value of a given string. It returns an unsigned 32-bit integer value that represents the CRC-32 checksum of the string. The function can be used to detect errors or changes in data. It is similar to the MD5(), SHA1(), and SHA2() functions, but with a different hashing algorithm. It is also related to the UNHEX() function, which can convert a CRC32 value to a binary string.