定制 yaman-shahbander-dev/totp-generator 二次开发

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

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

yaman-shahbander-dev/totp-generator

最新稳定版本:v1.1.1

Composer 安装命令:

composer require yaman-shahbander-dev/totp-generator

包简介

A PHP implementation of Time-Based One-Time Password (TOTP), using Base32 encoding and HMAC hashing. Generates and verifies OTPs for secure authentication.

README 文档

README

A Laravel package for generating and verifying Time-Based One-Time Passwords (TOTP) compatible with RFC 6238. Provides both facade and contract-based implementations.

Features

  • Generate TOTP codes
  • Verify TOTP codes with configurable window
  • Base32 encoding/decoding utilities
  • Configurable parameters (period, digits, verification window)
  • Laravel facade and service provider integration

Installation

  • Install via Composer:
composer require yaman-shahbander-dev/laravel-totp
  • Publish the configuration file (optional):
php artisan vendor:publish --provider="TotpGenerator\Providers\TotpServiceProvider" --tag="config"

Configuration

Default values in config/totp.php:

'default_period' => 30,      // Time step in seconds
'default_digits' => 6,       // Number of digits in OTP
'verification_window' => 1,  // Number of periods to check before/after

Usage

Using the Facade

use TotpGenerator\Facades\Totp;

// Generate a random secret (store this securely)
$secret = Str::random(16);
$base32Secret = Totp::encodeBase32($secret);

// Generate current TOTP
$code = Totp::generate($base32Secret);

// Verify a code
$isValid = Totp::verify($userCode, $base32Secret);

// With custom window
$isValid = Totp::verify($userCode, $base32Secret, 2);

Using Dependency Injection

use TotpGenerator\Contracts\TotpGeneratorContract;

class AuthController {
    public function __construct(
        protected TotpGeneratorContract $totp
    ) {}

    public function verifyCode(Request $request)
    {
        $isValid = $this->totp->verify(
            $request->code,
            $user->totp_secret
        );
    }
}

Direct Usage

$totp = app(TotpGeneratorContract::class);
$code = $totp->generate($base32Secret);

Base32 Utilities

// Encode binary to Base32
$base32Secret = Totp::encodeBase32(random_bytes(16));

// Decode Base32 to binary
$binarySecret = Totp::decodeBase32($base32Secret);

Security Considerations

  • Always store secrets securely (encrypted at rest)
  • Use secure random bytes for secret generation
  • Consider rate limiting verification attempts
  • The package uses SHA-1 by default (compatible with most authenticator apps)

License

MIT License (see LICENSE file)

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-15