php

PHP CRC32() Function

Data integrity is a very important factor when the data is transmitted from one location to another. Many built-in functions exist in PHP to maintain the data integrity. Some of them are crc32(), sha1(), md5(), etc. All these functions are used to generate a Hash value based on a particular algorithm. The crc32() function returns the 32-bit CRC (Cyclic Redundancy Checksum) polynomial value of the string to check the data integrity. It uses the CRC32 algorithm to confirm the data integrity by checking the source data and transmitted data. The “%u” specifier is required to print the output of the crc32() function. The different uses of the crc32() function and the difference between other hashing functions are shown in this tutorial.

Syntax:

int crc32(string $string)

The function has only one argument that is used to generate the crc32 checksum and returns an integer value.

Different Examples of CRC32() Function

The different uses of the crc32() function are shown in this part of the tutorial using multiple examples.

Example 1: Print CRC32 Checksum Value Without “%u”

The crc32() function generates a positive integer for the 64-bit computer and a negative integer for the 32-bit computer. Create a PHP file with the following script that prints the checksum value that is generated by the crc32() function and print the checksum value without any specifier.

<?php

//Define a string value
$string = "Welcome to LinuxHint";

echo "The original string: <b>$string.</b><br />";
//Calculate the checksum value
$checksum = crc32($string);

//Print the checksum value
echo "The checksum value of the string is <b>$checksum</b>.";

?>

Output:

The following output appears after executing the previous script. A positive integer value is generated for the string value:

Example 2: Print CRC32 Checksum Value With “%u”

The “%u” specifier is used in the sprintf() or printf() function to print the checksum value that is generated by crc32() in the decimal format. Create a PHP file with the following script that prints the checksum value that is generated by the crc32() function and the “%u” specifier is used in the printf() function.

<?php

//Define a string value
$string = "PHP Programming";

echo "The original string: <b>$string.</b><br/>";
//Calculate the checksum value
$checksum = crc32($string);

//Print the checksum value
printf("The checksum value of the string is <b>%u.</b>", $checksum);

?>

Output:

The following output appears after executing the previous script. A positive integer value is generated for the string value:

Example 3: Compare the Output of CRC32() and SHA1() Functions

The sha1() is a useful function of PHP to generate the Hash value of the particular string data. This function returns 40 characters long hexadecimal value of the string data if no optional argument of the sha1() function is used and it returns 20 characters long raw binary value if true is used in the optional argument. Create a PHP file with the following script that prints the checksum value that is generated by the crc32() function and the hash value that is generated by the sha1() function:

<?php

//Define a string value
$string = "PHP is a server-side scripting language.";

echo "The original string: <b>$string.</b><br/>";
//Calculate the checksum value
$checksum = crc32($string);

//Print the checksum value
echo "The crc32() value of the string is <b>$checksum</b>.<br/>";

//Generate the hash value
$hashvalue = sha1($string);

//Print the generated hash string
echo "The sha1() hash value of the string is <b>$hashvalue</b>.<br/>";

?>

Output:

The following output appears after executing the previous script. Here, a positive integer value is generated by the crc32() function and a large hexadecimal hash value is generated by the sha1() function for the same string value.

Example 4: Compare the Output of CRC32() and MD5() Functions

The md5() is another useful function of PHP to generate the Hash value of the particular string data. This function returns 32 characters long hexadecimal value of the string data if no optional argument of the md5() function is used and it returns 16 characters long raw binary value if true is used in the optional argument. Create a PHP file with the following script that prints the checksum value that is generated by the crc32() function and the Hash value that is generated by the md5() function for the same string value:

<?php

//Define a string value
$string = "PHP is a server-side scripting language.";

echo "The original string: <b>$string.</b><br/>";
//Calculate the checksum value
$checksum = crc32($string);

//Print the checksum value
echo "The crc32() value of the string is <b>$checksum</b>.<br/>";

//Generate the hash value
$hashvalue = md5($string);

//Print the generated hash string
echo "The md5() hash value of the string is <b>$hashvalue</b>.<br/>";

?>

Output:

The following output appears after executing the previous script. Here, a positive integer value is generated by the crc32() function and a large hexadecimal Hash value is generated by the md5() function for the same string value:

Conclusion

Different ways exist in PHP to modify the original data for data integrity. The crc32() is a simple built-in function of PHP to provide this facility. It returns an integer value as a checksum value of a string data. When it requires to transfer any sensitive data on the web, it is better to generate the checksum value of the data using the crc32() function before transmission. The way of using the crc32() function and the differences between the crc32() function with other functions that generate the hash value of the string are explained in this tutorial. The purpose of using the crc32() function is cleared after reading this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.