How the CRC32C() function works in Mariadb?

The CRC32C() function is a string function that returns the cyclic redundancy check (CRC) value of a given string, using the Castagnoli polynomial.

Posted on

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

Syntax

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

CRC32C(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 CRC32C value of a string

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

SELECT CRC32C('Hello, world');

The output is:

+------------------------+
| CRC32C('Hello, world') |
+------------------------+
|             1539970105 |
+------------------------+

The output shows that the CRC32C value of the string ‘Hello, world’ is 3957769958.

Example 2: Calculating the CRC32C value of a binary string

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

SELECT CRC32C('0b01010101');

The output is:

+----------------------+
| CRC32C('0b01010101') |
+----------------------+
|           2275276270 |
+----------------------+

The output shows that the CRC32C value of the binary string ‘0b01010101’ is 1776707584.

Example 3: Calculating the CRC32C value of a numeric string

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

SELECT CRC32C('123456789');

The output is:

+---------------------+
| CRC32C('123456789') |
+---------------------+
|          3808858755 |
+---------------------+

The output shows that the CRC32C value of the numeric string ‘123456789’ is 1630418576.

Example 4: Calculating the CRC32C value of an empty string

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

SELECT CRC32C('');

The output is:

+------------+
| CRC32C('') |
+------------+
|          0 |
+------------+

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

Example 5: Calculating the CRC32C value of an invalid string

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

SELECT CRC32C(NULL);

The output is:

+--------------+
| CRC32C(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 CRC32C() function in Mariadb. They are:

  • CRC32(): This function returns the CRC value of a given string, using the IEEE polynomial. 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.
  • 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 CRC32C values.

Conclusion

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