captcha

package
v1.59.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package captcha 提供图形验证码:生成 PNG + 答案存储与校验。 底层基于 base64Captcha(Go 生态主流);答案默认内存存储(带 TTL 与限次), 可注入自定义 Store(如 Redis 分布式校验)。 场景:登录/注册防刷、短信发送前置验证。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator 验证码生成器。

func NewGenerator

func NewGenerator() *Generator

NewGenerator 创建生成器(默认数字 4 位 + 内存存储 + 5 分钟 TTL)。

func (*Generator) Exists

func (g *Generator) Exists(id string) bool

Exists 判断验证码 id 是否仍有效(不消费)。

func (*Generator) Generate

func (g *Generator) Generate() (id, base64PNG string, err error)

Generate 生成验证码,返回 (id, base64 PNG 数据, 错误)。 前端:<img src="data:image/png;base64,<data>">。

func (*Generator) Verify

func (g *Generator) Verify(id, answer string) bool

Verify 校验答案(一次性消费:验证失败也会销毁,防暴力破解)。 大小写不敏感;空白自动去除。

func (*Generator) WithStore

func (g *Generator) WithStore(store Store) *Generator

WithStore 注入自定义存储(如 Redis 实现)。

func (*Generator) WithTTL

func (g *Generator) WithTTL(ttl time.Duration) *Generator

WithTTL 设置答案有效期。

type Store

type Store interface {
	// Set 保存答案,ttl 过期。
	Set(id, answer string, ttl time.Duration) error
	// Get 读取并消费(一次性:校验后即失效)。
	Get(id string) (string, error)
	// Exists 判断是否存在(不消费)。
	Exists(id string) bool
}

Store 验证码答案存储接口(内存实现内置;分布式场景注入 Redis 实现)。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL