Giter VIP home page Giter VIP logo

crc-php's Introduction

CRC-PHP

Flexible implementation of cyclic redundancy check (CRC) in PHP 8.0+.

Install

composer require bafs/crc-php

Usage

Basic Usage

Example using CRC24/OPENPGP

$encoder = CrcFactory::create(CrcFactory::CRC24_OPENPGP);
echo dechex($encoder->compute('test')); // f86ed0

Recommended Usage (using memoization)

It is recommended to use memoization to make the calculation faster (~10x, it varies from the input and the CRC used, see examples/benchmark.php for more details).

You can generate the table with Encoder::generateTable().

$encoder = CrcFactory::create(CrcFactory::CRC24_OPENPGP);

print_r($encoder->generateTable()); // [0x0, 0x864cfb, 0x8ad50d, 0xc99f6, 0x93e6e1, 0x15aa1a, ...]

You can see an example with a nicer output in examples/generate-table.php.

You should set this table during the bootstrapping of your application and/or in the service container.

// Bootstrap
$table = [0x0, 0x864cfb, 0x8ad50d, 0xc99f6, 0x93e6e1, 0x15aa1a, 0x1933ec, 0x9f7f17, 0xa18139, ...];
$encoder = CrcFactory::create(CrcFactory::CRC24_OPENPGP, $table);

// Encoder will use the table to compute the hash
echo dechex($encoder->compute('test')); // f86ed0

Advance Usage with Custom Parameters

It is possible to give custom parameters to the encoder to get any CRC.

$encoder = CrcFactory::create(new Parameters(width: 8, poly: 0x07, init: 0x00));
echo dechex($encoder->compute('test')); // b9

You can also create parameters from a string.

$encoder = CrcFactory::create(Parameters::createFromString('width=8 poly=0x07 init=0x00 refin=false refout=false xorout=0x00 check=0xf4 residue=0x00 name="CRC-8/SMBUS"'));
echo dechex($encoder->compute('test')); // b9

Tests

  • Unit tests: ./vendor/bin/phpunit --testdox --color tests
  • Code quality: ./vendor/bin/psalm

crc-php's People

Contributors

bafs avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.