-
Notifications
You must be signed in to change notification settings - Fork 6
/
CRC16CCIT.php
42 lines (38 loc) · 926 Bytes
/
CRC16CCIT.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
namespace Crc16CCIT;
/**
* Calculate CRC16-CCIT checksum
* Code ported to PHP from Java
*
* Oryginal source:
* http://introcs.cs.princeton.edu/java/51data/CRC16CCITT.java.html
* CRC16-CCIT for 123456789 = 29b1
*
* Online calculator for testing:
* http://zorc.breitbandkatze.de/crc.html
*/
class Crc16CCIT
{
/**
* @param string $data
* @return string
*/
public static function calculate($data)
{
$crc = 0xFFFF;
$polynomial = 0x1021;
foreach (str_split($data, 1) as $char) {
$byte = ord($char);
for ($i = 0; $i < 8; $i++) {
$bit = (($byte >> (7 - $i) & 1) == 1);
$c15 = (($crc >> 15 & 1) == 1);
$crc <<= 1;
if ($c15 ^ $bit) {
$crc ^= $polynomial;
}
}
}
$crc &= 0xffff;
return dechex($crc);
}
}