Overview
Zero-dependency checksum and non-cryptographic hash utilities for HolyPHP: CRC-32, Adler-32, FNV-1a (32-bit), DJB2, Luhn check-digit generation & validation, hex formatting, and algorithm verification.
bundled — works offline
zero dependencies
crc32
adler32
Quick start
import "checksum";
$crc = checksum_crc32("123456789");
println(checksum_hex($crc)); // cbf43926
$adler = checksum_adler32("Wikipedia");
println(checksum_hex($adler)); // 11e60398
$fnv = checksum_fnv1a32("foobar");
println(checksum_verify("fnv1a32", "foobar", $fnv)); // true
$digit = checksum_luhn_digit("7992739871"); // 3
println(checksum_luhn_valid("7992739871" . $digit)); // true
Installation
In a project
Inside a project (a folder with hphp.yaml, see Projects & packages):
hphp install checksum
This adds the library to hphp.yaml, installs it into .hphp/packages/checksum/ and pins the exact version in hphp.lock:
require:
checksum: "^1.0"
Use it
import "checksum";
checksum is bundled with the compiler, so import "checksum"; also works in a single file without installing anything, and hphp install works offline.
Outside a project
hphp install checksum # for your user, shared by all scripts
hphp create myapp # or start a project, then: hphp install checksum
Documentation
API Reference
checksum_crc32(string $data): int
Computes the standard IEEE 802.3 CRC-32 checksum (polynomial 0xEDB88320) as an unsigned 32-bit integer.
checksum_adler32(string $data): int
Computes the RFC 1950 Adler-32 checksum as an unsigned 32-bit integer.
checksum_fnv1a32(string $data): int
Computes the 32-bit FNV-1a non-cryptographic hash (offset basis 0x811C9DC5, prime 0x01000193) as an unsigned 32-bit integer.
checksum_djb2(string $data): int
Computes the 32-bit DJB2 hash (hash = hash * 33 + byte, seed 5381) as an unsigned 32-bit integer.
checksum_luhn_digit(string $payload): int
Computes the Luhn check digit (0–9) to append to a numeric payload string.
checksum_luhn_valid(string $number): bool
Validates whether a numeric string (at least 2 digits) satisfies the Luhn checksum.
checksum_verify(string $algo, string $data, int $expected): bool
Verifies $data against $expected using the specified algorithm ("crc32", "adler32", "fnv1a32", or "djb2", case-insensitive).
checksum_hex(int $value): string
Formats a 32-bit integer checksum as an 8-character lowercase hexadecimal string.
Error Handling
All validation errors throw an Exception prefixed with checksum::
checksum_luhn_digit("") → checksum: checksum_luhn_digit requires a non-empty string of ASCII digits (0-9)
checksum_luhn_digit("123a5") → checksum: checksum_luhn_digit received non-digit character 'a' at index 3
checksum_luhn_valid("7") → checksum: checksum_luhn_valid requires at least 2 digits
checksum_verify("md5", "hi", 0) → checksum: unsupported algorithm 'md5' (expected one of: crc32, adler32, fnv1a32, djb2)
Examples
01Quick start
Bundled with the compiler — import "checksum"; works offline in any file, or add it to a project with hphp install checksum.
import "checksum";
$crc = checksum_crc32("123456789");
println(checksum_hex($crc)); // cbf43926
$adler = checksum_adler32("Wikipedia");
println(checksum_hex($adler)); // 11e60398
$fnv = checksum_fnv1a32("foobar");
println(checksum_verify("fnv1a32", "foobar", $fnv)); // true
$digit = checksum_luhn_digit("7992739871"); // 3
println(checksum_luhn_valid("7992739871" . $digit)); // true
Try ithphp install checksum
hphp run main.hphp
Covered by the official test suite in tests/positive/lib_checksum.hphp.