northrook/hasher
Composer 安装命令:
composer require northrook/hasher
包简介
Crockford Base32 hashing utilities for short, URL-safe identifiers, checksums, and time-sortable prefixes
README 文档
README
Crockford Base32 hashing utilities for short, URL-safe identifiers, checksums, and time-sortable prefixes.
Requires PHP 8.4+. No runtime Composer dependencies.
Requirements
PHP>=8.4ext-hash(xxHash32 viahash('xxh32', …))ext-random(Random\Randomizerforcrypto())
Installation
composer require northrook/hasher
Quick start
use Northrook\Hash;
Hash::checksum('hello'); // '03XG0XZS'
Hash::time(10, seed: 1_700_000_000_000); // '01HF7YAT00'
Hash::crypto(); // 16-char CSPRNG token
API reference
| Method | Default length | Range | Purpose |
|---|---|---|---|
xxh32(string $value) | n/a | n/a | Raw 8-char lowercase hex digest |
checksum(string $value) | 8 (fixed) | n/a | Canonical Crockford Base32 checksum |
value(string $value, int $length = 8) | 8 | 4–8 | Truncated Crockford Base32 digest (LSB end) |
time(int $length = 10, null\|int\|float $seed = null) | 10 | 1–16 | Millisecond timestamp as sortable Base32 |
fast(int $length = 8) | 8 | 1–32 | Fast random (mt_rand); not for secrets |
crypto(int $length = 16) | 16 | 8–32 | CSPRNG random (Random\Randomizer) |
time() seed: null → current time (ms); int → milliseconds since epoch; float → seconds (fractional part included). Length 10 gives full millisecond sortability.
Examples
Checksums and digests
Hash::xxh32('hello'); // 'fb0077f9'
Hash::checksum('hello'); // '03XG0XZS'
Hash::value('hello', 4); // '0XZS'
Hash::checksum('café'); // '00ZXTEE1'
Time-sortable prefixes
Hash::time(10, seed: 1_700_000_000_000); // '01HF7YAT00'
Hash::time(8, seed: 1_700_000_000_000); // 'HF7YAT00' — suffix of the 10-char form
Random tokens
Hash::fast(8); // non-cryptographic — UI keys, low-stakes nonces
Hash::crypto(16); // secrets, session tokens, API keys
Composite IDs
$id = Hash::time(10) . Hash::crypto(16);
checksum(), value(), xxh32(), and fast() are not for security-sensitive use.
Use crypto() for secrets and unguessable tokens.
Development
composer test
composer phpstan
License
BSD-3-Clause. See LICENSE.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2026-07-09