bafs/crc-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bafs/crc-php

最新稳定版本:v0.3.1

Composer 安装命令:

composer require bafs/crc-php

包简介

README 文档

README

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

Install

composer require bafs/crc-php

Usage

Basic Usage

Example using CRC24/OPENPGP

$encoder = CrcFactory::create(CrcType::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(CrcType::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(CrcType::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

统计信息

  • 总下载量: 9.71k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-04-03