phpCrc16 - a PHP code snippet to calculate CRC16/CCITT (download)



2002-11-28: Updated to use a faster 16 bit lookup table, thanks to Ashley

<?php 

/*************************************************************************
 * phpCrc16 v1.1 -- CRC16/CCITT implementation 
 *
 * By Matteo Beccati <matteo AT beccati DOT com>
 * 
 * Original code by:
 * Ashley Roll
 * Digital Nemesis Pty Ltd
 * www.digitalnemesis.com
 * ash AT digitalnemesis DOT com 
 *
 * Test Vector: "123456789" (character string, no quotes)
 * Generated CRC: 0x29B1
 * 
 *************************************************************************/



/*
 * Returns CRC16 of a string as int value
 */
function CRC16($str)
{
    static 
$CRC16_Lookup = array(
            
0x00000x10210x20420x30630x40840x50A50x60C60x70E7
            
0x81080x91290xA14A0xB16B0xC18C0xD1AD0xE1CE0xF1EF
            
0x12310x02100x32730x22520x52B50x42940x72F70x62D6
            
0x93390x83180xB37B0xA35A0xD3BD0xC39C0xF3FF0xE3DE
            
0x24620x34430x04200x14010x64E60x74C70x44A40x5485
            
0xA56A0xB54B0x85280x95090xE5EE0xF5CF0xC5AC0xD58D
            
0x36530x26720x16110x06300x76D70x66F60x56950x46B4
            
0xB75B0xA77A0x97190x87380xF7DF0xE7FE0xD79D0xC7BC
            
0x48C40x58E50x68860x78A70x08400x18610x28020x3823
            
0xC9CC0xD9ED0xE98E0xF9AF0x89480x99690xA90A0xB92B
            
0x5AF50x4AD40x7AB70x6A960x1A710x0A500x3A330x2A12
            
0xDBFD0xCBDC0xFBBF0xEB9E0x9B790x8B580xBB3B0xAB1A
            
0x6CA60x7C870x4CE40x5CC50x2C220x3C030x0C600x1C41
            
0xEDAE0xFD8F0xCDEC0xDDCD0xAD2A0xBD0B0x8D680x9D49
            
0x7E970x6EB60x5ED50x4EF40x3E130x2E320x1E510x0E70
            
0xFF9F0xEFBE0xDFDD0xCFFC0xBF1B0xAF3A0x9F590x8F78
            
0x91880x81A90xB1CA0xA1EB0xD10C0xC12D0xF14E0xE16F
            
0x10800x00A10x30C20x20E30x50040x40250x70460x6067
            
0x83B90x93980xA3FB0xB3DA0xC33D0xD31C0xE37F0xF35E
            
0x02B10x12900x22F30x32D20x42350x52140x62770x7256
            
0xB5EA0xA5CB0x95A80x85890xF56E0xE54F0xD52C0xC50D
            
0x34E20x24C30x14A00x04810x74660x64470x54240x4405
            
0xA7DB0xB7FA0x87990x97B80xE75F0xF77E0xC71D0xD73C
            
0x26D30x36F20x06910x16B00x66570x76760x46150x5634
            
0xD94C0xC96D0xF90E0xE92F0x99C80x89E90xB98A0xA9AB
            
0x58440x48650x78060x68270x18C00x08E10x38820x28A3
            
0xCB7D0xDB5C0xEB3F0xFB1E0x8BF90x9BD80xABBB0xBB9A
            
0x4A750x5A540x6A370x7A160x0AF10x1AD00x2AB30x3A92
            
0xFD2E0xED0F0xDD6C0xCD4D0xBDAA0xAD8B0x9DE80x8DC9
            
0x7C260x6C070x5C640x4C450x3CA20x2C830x1CE00x0CC1
            
0xEF1F0xFF3E0xCF5D0xDF7C0xAF9B0xBFBA0x8FD90x9FF8
            
0x6E170x7E360x4E550x5E740x2E930x3EB20x0ED10x1EF0
    
);
    
    
$crc16 0xFFFF// the CRC
    
$len strlen($str);
    
    for(
$i 0$i $len$i++ )
    {
        
$t = ($crc16 >> 8) ^ ord($str[$i]); // High byte Xor Message Byte to get index
        
$crc16 = (($crc16 << 8) & 0xffff) ^ $CRC16_Lookup[$t]; // Update the CRC from table
    
}
    
    
// crc16 now contains the CRC value
    
return $crc16;
}

/*
 * Returns CRC16 of a string as hexadecimal string
 */
function CRC16HexDigest($str)
{
    return 
sprintf('%04X'crc16($str));
}

?>