定制 laragear/fingerprint 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

laragear/fingerprint

Composer 安装命令:

composer require laragear/fingerprint

包简介

Ridiculously fast non-cryptographic hashes in your application or Eloquent Models

README 文档

README

Latest Version on Packagist Latest stable test run Codecov coverage Maintainability Sonarcloud Status Laravel Octane Compatibility

Ridiculously fast non-cryptographic hashes in your application or Eloquent Models.

use Laragear\Fingerprint\Fingerprinter;

$fingerprint = Fingerprinter::of('some-string-or-object');

return $fingerprint->hash();

Become a sponsor

Your support allows me to keep this package free, up-to-date and maintainable.

Requirements

  • PHP 8.3 or later
  • Laravel 12 or later

Installation

You can install the package via Composer.

composer require laragear/fingerprint

How does this work?

This adds the Fingerprint service in your service container to conveniently create non-cryptographic fingerprint hashes of strings, Stringable instance, resources, or any object in a memory-efficient way.

It leverages the power of hash_init() and json_encode to hash anything, including Eloquent Models or Collections, and uses the fastest hash algorithm around, xxHash by default.

Usage

To create fingerprints, you may use the Fingerprint facade, and any of its methods to create a hash.

use Laragear\Fingerprint\Facades\Fingerprint;

// Make a Base64 encoded hash
Fingerprint::make($hashable);
Fingerprint::base64($hashable);

// Make a Base64 URL-safe hash to transmit over the network.
Fingerprint::base64Url($hashable);

// Make a "raw" binary hash.
Fingerprint::binary($hashable);

// Make a hexadecimal hash.
Fingerprint::hex($hashable);

// Compare two hashes.
Fingerprint::is($expected, $string);
Fingerprint::isNot($expected, $string);

Algorithms

By default, Fingerprints are hashed using the xxh3 algorithm, available since PHP 8.1, which is the fastest non-cryptographic algorithm around and returns short hashes.

You may change it using the second parameter when creating fingerprints, and custom options as third parameter to be passed to hash()|hash_init().

use Laragear\Fingerprint\Facades\Fingerprint;

$hash = Fingerprint::base64($value, 'xxh128', [
    'seed' => 33
]);

Note

The algorithms available will depend on your environment. You may check them using hash_algos().

Changing the default algorithm

You may change the default format using the Laragear\Fingerprint\Fingerprinter::$algorithm with the one you want to be passed down to hash()|hash_init() methods. You may do this while your application boots in your App\Providers\AppServiceProvider or bootstrap\app.php.

use Illuminate\Foundation\Application;
use Laragear\Fingerprint\Fingerprinter;

return Application::configure(basePath: dirname(__DIR__))
    ->booted(function () {
        Fingerprinter::$algorithm = 'sha256';
    })
    ->create();

Eloquent Models

You can use the Laragear\Fingerprint\HasFingerprints trait in your models to automatically calculate fingerprints in your model when saving them into the database.

Set the trait and use the updateFingerprints() method to return an array of all the fingerprints to calculate before persistence.

use Illuminate\Database\Eloquent\Model;
use Laragear\Fingerprint\HasFingerprints;
use Laragear\Fingerprint\Fingerprinter;

/**
 * @property string $body 
 */
class Article extends Model
{
    use HasFingerprints;
    
    /**
     * Receive a Fingerprinter instance and returns an array of attributes with the fingerprints.
     *
     * @return array<string, string>
     */
    public function updateFingerprints(Fingerprinter $fingerprinter): array
    {
        return [
            'fingerprint' => $fingerprinter->make($this->body)
        ];
    }
}

Since you receive a Fingerprinter instance, you can create a fingerprint anyway you want.

use Laragear\Fingerprint\Fingerprinter;

public function updateFingerprints(Fingerprinter $fingerprinter): array
{
    return [
        'fingerprint' => $fingerprinter->base64Url($this->body)
    ];
}

If you want to programmatically disable (or enable) the fingerprint update, use the shouldUpdateFingerprints() method.

/**
 * Checks if the model should update its fingerprints before persisting into storage.
 */
public function shouldUpdateFingerprints(): bool
{
    return $this->isPublished();
}

Laravel Octane compatibility

  • There are no singletons using a stale app instance.
  • There are no singletons using a stale config instance.
  • There are no singletons using a stale request instance.
  • There are no static properties written during a request.

There should be no problems using this package with Laravel Octane.

Security

If you discover any security-related issues, issue a Security Advisor

License

This specific package version is licensed under the terms of the MIT License, at the time of publishing.

Laravel is a Trademark of Taylor Otwell. Copyright © 2011–2026 Laravel LLC.

laragear/fingerprint 适用场景与选型建议

laragear/fingerprint 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 617 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 07 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 laragear/fingerprint 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 laragear/fingerprint 我们能提供哪些服务?
定制开发 / 二次开发

基于 laragear/fingerprint 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 617
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 24
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-22