定制 tourze/proof-of-work-challenge-bundle 二次开发

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

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

tourze/proof-of-work-challenge-bundle

Composer 安装命令:

composer require tourze/proof-of-work-challenge-bundle

包简介

README 文档

README

English | 中文

A Symfony bundle providing Proof of Work (PoW) challenge system to defend against automated attacks, brute force attempts, and bot activities. This bundle implements the Hashcash algorithm with SHA-256 for web-optimized performance.

Features

  • Hashcash Algorithm: SHA-256 based proof-of-work with adjustable difficulty
  • Adaptive Difficulty: Dynamic difficulty adjustment based on threat levels
  • Storage Abstraction: Flexible storage backend (Cache/Redis) support
  • Security Integration: Built-in challenge expiration and replay protection
  • Performance Optimized: Sub-millisecond server-side validation

Installation

composer require tourze/proof-of-work-challenge-bundle

Configuration

Add the bundle to config/bundles.php:

return [
    // ...
    Tourze\ProofOfWorkChallengeBundle\ProofOfWorkChallengeBundle::class => ['all' => true],
];

Usage

1. Issue Challenge

use Tourze\ProofOfWorkChallengeBundle\Procedure\IssueChallengeHandler;

// Inject the handler in your service
public function __construct(
    private IssueChallengeHandler $issueChallengeHandler
) {}

// Issue a challenge for resource protection
$result = ($this->issueChallengeHandler)('login', $clientId);

// Response format:
[
    'success' => true,
    'challenge' => [
        'id' => 'challenge-id',
        'type' => 'hashcash',
        'challenge' => 'challenge-string',
        'difficulty' => 6,
        'expires_at' => 1234567890,
        'resource' => 'login'
    ]
]

2. Verify Challenge

use Tourze\ProofOfWorkChallengeBundle\Procedure\VerifyChallengeHandler;

// Inject the handler in your service
public function __construct(
    private VerifyChallengeHandler $verifyChallengeHandler
) {}

// Verify the submitted proof
$result = ($this->verifyChallengeHandler)($challengeId, $proof);

// Success response:
[
    'success' => true,
    'resource' => 'login',
    'client_id' => 'client-id',
    'metadata' => []
]

// Failure response:
[
    'success' => false,
    'error' => 'Invalid proof',
    'code' => 'INVALID_PROOF'
]

Algorithm Implementation

Hashcash Algorithm

The bundle uses the modern Hashcash algorithm where the client must find a nonce such that:

SHA256(challenge + ':' + nonce)

produces a hash with the required number of leading zero bits based on difficulty level.

Client-side JavaScript Implementation

async function solveChallenge(challenge, difficulty) {
    let nonce = 0;
    while (true) {
        const attempt = challenge + ':' + nonce;
        const hash = await sha256(attempt);
        
        if (countLeadingZeroBits(hash) >= difficulty) {
            return nonce.toString();
        }
        nonce++;
    }
}

async function sha256(message) {
    const msgBuffer = new TextEncoder().encode(message);
    const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
    const hashArray = Array.from(new Uint8Array(hashBuffer));
    return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
}

function countLeadingZeroBits(hexHash) {
    let zeroBits = 0;
    for (let i = 0; i < hexHash.length; i++) {
        const nibble = parseInt(hexHash[i], 16);
        if (nibble === 0) {
            zeroBits += 4;
        } else {
            zeroBits += Math.clz32(nibble) - 28;
            break;
        }
    }
    return zeroBits;
}

Adaptive Difficulty

The bundle automatically adjusts difficulty based on:

  • Base Difficulty: Default level of 4-6 bits
  • Resource Type: Higher difficulty for sensitive resources (login, payment)
  • Client Behavior: Dynamic adjustment based on recent attempt patterns
    • 5-10 attempts: 1.2x multiplier
    • 10-20 attempts: 1.5x multiplier
    • 20-50 attempts: 2.0x multiplier
    • 50-100 attempts: 2.5x multiplier
    • 100+ attempts: 3.0x multiplier

Security Features

  • Time-bound Challenges: 5-minute expiration by default
  • Anti-replay Protection: Each challenge can only be used once
  • Replay Detection: Challenge marking and validation
  • Threat Escalation: Progressive difficulty increase
  • Storage Abstraction: Secure challenge persistence

Performance Characteristics

  • 4-bit difficulty: Average < 0.1 seconds
  • 8-bit difficulty: Average ~1 second
  • 12-bit difficulty: Average ~10 seconds
  • 16-bit difficulty: Average ~1 minute

The implementation uses adaptive difficulty to balance security and user experience.

License

MIT License

tourze/proof-of-work-challenge-bundle 适用场景与选型建议

tourze/proof-of-work-challenge-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 11 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 tourze/proof-of-work-challenge-bundle 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-12