CRC32(3) 1 CRC32(3)crc32 - Calculates the crc32 polynomial of a stringSYNOPSIS
int crc32 (string $str)
DESCRIPTION
Generates the cyclic redundancy checksum polynomial of 32-bit lengths of the $str. This is usually used to validate the integrity of data
being transmitted.
Warning
Because PHP's integer type is signed many crc32 checksums will result in negative integers on 32bit platforms. On 64bit installa-
tions all crc32(3) results will be positive integers though.
So you need to use the "%u" formatter of sprintf(3) or printf(3) to get the string representation of the unsigned crc32(3) checksum
in decimal format.
For a hexadecimal representation of the checksum you can either use the "%x" formatter of sprintf(3) or printf(3) or the dechex(3)
conversion functions, both of these also take care of converting the crc32(3) result to an unsigned integer.
Having 64bit installations also return negative integers for higher result values was considered but would break the hexadecimal
conversion as negatives would get an extra 0xFFFFFFFF######## offset then. As hexadecimal representation seems to be the most common
use case we decided to not break this even if it breaks direct decimal comparisons in about 50% of the cases when moving from 32 to
64bits.
In retrospect having the function return an integer maybe wasn't the best idea and returning a hex string representation right away
(as e.g. md5(3) does) might have been a better plan to begin with.
For a more portable solution you may also consider the generic hash(3). hash("crc32b", $str) will return the same string as
dechex(crc32($str)).
PARAMETERS
o $str
- The data.
RETURN VALUES
Returns the crc32 checksum of $str as an integer.
EXAMPLES
Example #1
Displaying a crc32 checksum
This example shows how to print a converted checksum with the printf(3) function:
<?php
$checksum = crc32("The quick brown fox jumped over the lazy dog.");
printf("%u
", $checksum);
?>
SEE ALSO hash(3), md5(3), sha1(3).
PHP Documentation Group CRC32(3)
Check Out this Related Man Page
crc16(3tcl) Cyclic Redundancy Checks crc16(3tcl)__________________________________________________________________________________________________________________________________________________NAME
crc16 - Perform a 16bit Cyclic Redundancy Check
SYNOPSIS
package require Tcl 8.2
package require crc16 ?1.1.1?
::crc::crc16 ?-format format? ?-seed value? ?-implementation procname? message
::crc::crc16 ?-format format? ?-seed value? ?-implementation procname? -filename file
::crc::crc-ccitt ?-format format? ?-seed value? ?-implementation procname? message
::crc::crc-ccitt ?-format format? ?-seed value? ?-implementation procname? -filename file
::crc::xmodem ?-format format? ?-seed value? ?-implementation procname? message
::crc::xmodem ?-format format? ?-seed value? ?-implementation procname? -filename file
_________________________________________________________________DESCRIPTION
This package provides a Tcl-only implementation of the CRC algorithms based upon information provided at http://www.microconsul-
tants.com/tips/crc/crc.txt There are a number of permutations available for calculating CRC checksums and this package can handle all of
them. Defaults are set up for the most common cases.
COMMANDS
::crc::crc16 ?-format format? ?-seed value? ?-implementation procname? message
::crc::crc16 ?-format format? ?-seed value? ?-implementation procname? -filename file
::crc::crc-ccitt ?-format format? ?-seed value? ?-implementation procname? message
::crc::crc-ccitt ?-format format? ?-seed value? ?-implementation procname? -filename file
::crc::xmodem ?-format format? ?-seed value? ?-implementation procname? message
::crc::xmodem ?-format format? ?-seed value? ?-implementation procname? -filename file
The command takes either string data or a file name and returns a checksum value calculated using the CRC algorithm. The command
used sets up the CRC polynomial, initial value and bit ordering for the desired standard checksum calculation. The result is format-
ted using the format(3tcl) specifier provided or as an unsigned integer (%u) by default.
A number of common polynomials are in use with the CRC algorithm and the most commonly used of these are included in this package.
For convenience each of these has a command alias in the crc namespace.
It is possible to implement the CRC-32 checksum using this crc16 package as the implementation is sufficiently generic to extend to
32 bit checksums. As an example this has been done already - however this is not the fastest method to implement this algorithm in
Tcl and a separate crc32 package is available.
OPTIONS -filename name
Return a checksum for the file contents instead of for parameter data.
-format string
Return the checksum using an alternative format template.
-seed value
Select an alternative seed value for the CRC calculation. The default is 0 for the CRC16 calculation and 0xFFFF for the CCITT ver-
sion. This can be useful for calculating the CRC for data structures without first converting the whole structure into a string.
The CRC of the previous member can be used as the seed for calculating the CRC of the next member. It is also used for accumulating
a checksum from fragments of a large message (or file)
-implementation procname
This hook is provided to allow users to provide their own implementation (perhaps a C compiled extension). The procedure specfied is
called with two parameters. The first is the data to be checksummed and the second is the seed value. An integer is expected as the
result.
The package provides some implementations of standard CRC polynomials for the XMODEM, CCITT and the usual CRC-16 checksum. For con-
venience, additional commands have been provided that make use of these implementations.
EXAMPLES
% crc::crc16 "Hello, World!"
64077
% crc::crc-ccitt "Hello, World!"
26586
% crc::crc16 -format 0x%X "Hello, World!"
0xFA4D
% crc::crc16 -file crc16.tcl
51675
AUTHORS
Pat Thoyts
BUGS, IDEAS, FEEDBACK
This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category crc of
the Tcllib SF Trackers [http://sourceforge.net/tracker/?group_id=12883]. Please also report any ideas for enhancements you may have for
either package and/or documentation.
SEE ALSO cksum(3tcl), crc32(3tcl), sum(3tcl)KEYWORDS
checksum, cksum, crc, crc16, crc32, cyclic redundancy check, data integrity, security
CATEGORY
Hashes, checksums, and encryption
COPYRIGHT
Copyright (c) 2002, Pat Thoyts
crc 1.1.1 crc16(3tcl)