Git Product home page Git Product logo

php-crc32's Introduction

php-crc32

Build Status

by Andrew Brampton

Deprecated: Since PHP 8.0, the crc32 algorithms within PHP are using hardware accelerated instructions, and are surprisingly fast. Thus this extension is not needed anymore!

CRC32 implementations, that support all crc32 polynomials, as well as (if you install the pecl extension) hardware accelerated versions of CRC32C (Castagnoli).

Supports PHP 7.4 though PHP 8.2. Older PHP versions are supported with "v0.1.0".

Usage

require 'vendor/autoload.php';

use Google\CRC32\CRC32;

$crc = CRC32::create(CRC32::CASTAGNOLI);
$crc->update('hello');
echo $crc->hash();

Depending on the environment and the polynomial, CRC32::create will choose the fastest available version, and return one of the following classes:

  • Google\CRC32\PHP - A pure PHP implementation.
  • Google\CRC32\Builtin - A PHP Hash framework implementation.
  • Google\CRC32\Google - A hardware accelerated implementation (using google/crc32c).

When reading 1M byte chunks, using CRC32::CASTAGNOLI with PHP 7.4 on a 2014 Macbook Pro we get the following performance (higher is better):

Google\CRC32\PHP           12.27 MB/s
Google\CRC32\Builtin       468.74 MB/s (available since PHP 7.4)
Google\CRC32\Google        24,684.46 MB/s (using crc32c.so)

Install

$ composer require google/crc32

crc32c.so

To use the hardware accelerated, a custom PHP extension must be installed. This makes use of google/crc32c which provides a highly optomised CRC32C (Castagnoli) implementation using the SSE 4.2 instruction set of Intel CPUs.

The extension can be installed from pecl, or compiled from stratch.

TODO pecl install crc32c

Once installed or compiled, you'll need to add extension=crc32c.so to your php.ini file.

Compile (Linux / Mac)

Ensure that composer, build tools (e.g build-essential, cmake, etc), and php dev headers (e.g php-dev) are installed.

Simple (using Makefile):

make test

Alternatively (manually):

cd ext

# Install the google/crc32c library
./install_crc32c.sh # From source (recommended)

# or use your favorite package manager, e.g.
# brew install crc32c

# Prepare the build environment
phpize
./configure

# or if using a custom crc32c
# ./configure --with-crc32c=$(brew --prefix crc32c)

## Build and test
make test

The extension will now be at ext/modules/crc32c.so. This file should be copied to your extension directory and reference in your php.ini.

# php.ini
extension=crc32c.so

Testing

make test will test with the current PHP. make test_all will search for available PHP installs, and test with all of them.

Benchmark

To compare the performance of the different CRC32C implementations, run make benchmark.

Related

TODO

Licence (Apache 2)

This is not an official Google product (experimental or otherwise), it is just code that happens to be owned by Google.

Copyright 2023 Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

php-crc32's People

Contributors

bramp avatar jdpedrie avatar juan-morales avatar katrinleinweber avatar lvsantos1 avatar shyim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

php-crc32's Issues

v0.1.0 deprecation warnings for PHP 8.2

Current tagged version throws:

Deprecated: Creation of dynamic property Google\CRC32\Builtin::$algo is deprecated

I see master branch contains fixes already for it, could new version be tagged?

Different outputs for different implementations

When $raw_output is set to true, hash() outputs vary between implementations.

$input = random_bytes(10000);

$crcG = new Google(CRC32::CASTAGNOLI);
$crcG->update($input);

$crcP = new PHP(CRC32::CASTAGNOLI);
$crcP->update($input);

var_dump($crcG->hash(true) === $crcP->hash(true)); // false

I tested all four implementations against an upload to Cloud Storage against PHP 7.2 and PHP 7.3.

Case: Google\CRC32\Builtin
SUCCESS

Case: Google\CRC32\Google
SUCCESS

Case: Google\CRC32\PHP
FAIL

Case: Google\CRC32\PHPSlicedBy4
FAIL
use Google\CRC32\Builtin;
use Google\CRC32\CRC32;
use Google\CRC32\Google;
use Google\CRC32\PHP;
use Google\CRC32\PHPSlicedBy4;

$content = random_bytes(1000);

$classes = [
    Builtin::class,
    Google::class,
    PHP::class,
    PHPSlicedBy4::class
];

foreach ($classes as $class) {
    $crc = new $class(CRC32::CASTAGNOLI);
    $crc->update($content);
    print('Case: ' . $class . PHP_EOL);
    try {
        $bucket->upload($content, [
            'name' => uniqid(),
            'metadata' => [
                'crc32c' => base64_encode($crc->hash(true))
            ]
        ]);
        print('SUCCESS' . PHP_EOL);
    } catch (\Exception $e) {
        print('FAIL' . PHP_EOL);
    }
    print('' . PHP_EOL);
}

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.