zdpgo_captcha

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2022 License: MIT Imports: 26 Imported by: 0

README

zgo_captcha

快速实现验证码功能的基础组件

项目地址:https://github.com/zhangdapeng520/zdpgo_captcha

版本历史

  • v0.1.0 2022/04/02 新增:基本功能
  • v0.1.1 2022/07/16 新增:数学、字符串、中文、数字校验

使用案例

请查看 examples 示例

Documentation

Index

Constants

View Source
const (
	TxtNumbers          = "012346789"                                    // 数字字符串
	TxtAlphabet         = "ABCDEFGHJKMNOQRSTUVXYZabcdefghjkmnoqrstuvxyz" // 英文字符
	TxtSimpleCharaters  = "13467ertyiadfhjkxcvbnERTYADFGHJKXCVBN"        // 简单的数字和英文字符串
	TxtChineseCharaters = "的一是在不了有和人这中大为上个国我以要他" +
		"时来用们生到作地于出就分对成会可主发年动" +
		"同工也能下过子说产种面而方后多定行学法所" +
		"民得经十三之进着等部度家电力里如水化高自" +
		"二理起小物现实加量都两体制机当使点从业本" +
		"去把性好应开它合还因由其些然前外天政四日" +
		"那社义事平形相全表间样与关各重新线内数正" +
		"心反你明看原又么利比或但质气第向道命此变" +
		"条只没结解问意建月公无系军很情者最立代想" +
		"已通并提直题党程展五果料象员革位入常文总" +
		"次品式活设及管特件长求老头基资边流路级少" +
		"图山统接知较将组见计别她手角期根论运农指" +
		"几九区强放决西被干做必战先回则任取据处队" +
		"南给色光门即保治北造百规热领七海口东导器" +
		"压志世金增争济阶油思术极交受联什认六共权" +
		"收证改清己美再采转更单风切打白教速花带安" +
		"场身车例真务具万每目至达走积示议声报斗完" +
		"类八离华名确才科张信马节话米整空元况今集" +
		"温传土许步群广石记需段研界拉林律叫且究观" +
		"越织装影算低持音众书布复容儿须际商非验连" +
		"断深难近矿千周委素技备半办青省列习响约支" +
		"般史感劳便团往酸历市克何除消构府称太准精" +
		"值号率族维划选标写存候毛亲快效斯院查江型" +
		"眼王按格养易置派层片始却专状育厂京识适属" +
		"圆包火住调满县局照参红细引听该铁价严龙飞"

	MimeTypeAudio = "audio/wav" // 音频格式
	MimeTypeImage = "image/png" // 图片格式
	Emoji         = ""          // emoji表情
	/* 268-byte string literal not displayed */
)
View Source
const (
	OptionShowHollowLine = 2 // hollow线条数量
	OptionShowSlimeLine  = 4 // slime线条数量
	OptionShowSineLine   = 8 // sine线条数量
)

Variables

View Source
var (
	GCLimitNumber   = 10240                                     // 创建的验证码数量,用于触发默认存储区使用的垃圾收集。
	Expiration      = 10 * time.Minute                          // 默认过期时间
	DefaultMemStore = NewMemoryStore(GCLimitNumber, Expiration) // 默认内存存储器
)
View Source
var DefaultDriverDigit = NewDriverDigit(CaptchaConfig{})

DefaultDriverDigit 默认的验证码数字驱动

View Source
var DefaultEmbeddedFonts = NewEmbeddedFontsStorage(defaultEmbeddedFontsFS)

Functions

func RandColor added in v0.1.1

func RandColor() color.RGBA

RandColor 生成随机颜色.

func RandDeepColor added in v0.1.1

func RandDeepColor() color.RGBA

RandDeepColor 随机生成深色系.

func RandLightColor added in v0.1.1

func RandLightColor() color.RGBA

RandLightColor 随机生成浅色.

func RandText added in v0.1.1

func RandText(size int, sourceChars string) string

RandText 创建随机的文本

func RandomId added in v0.1.1

func RandomId() string

RandomId 然后一个随机的ID

Types

type BackgroundColor added in v0.1.1

type BackgroundColor struct {
	R uint8 `yaml:"r" json:"r"`
	G uint8 `yaml:"g" json:"g"`
	B uint8 `yaml:"b" json:"b"`
	A uint8 `yaml:"a" json:"a"`
}

BackgroundColor 背景颜色

type Captcha

type Captcha struct {
	Driver Driver
	Store  Store
}

Captcha 验证码基本信息

func NewCaptcha added in v0.1.1

func NewCaptcha(driver Driver, store Store) *Captcha

NewCaptcha 创建验证码对象

func (*Captcha) Generate

func (c *Captcha) Generate() (id, b64s string, err error)

Generate 生成随机的ID,验证码图片的base64编码和错误

func (*Captcha) Verify

func (c *Captcha) Verify(id, answer string, clear bool) (match bool)

Verify 通过给定的id键,并删除存储中的验证码值,返回一个布尔值 如果您有多个验证码实例共享同一个存储,可以调用 `store.Verify` 方法

type CaptchaConfig added in v0.1.1

type CaptchaConfig struct {
	StoreType       string          `yaml:"store_type" json:"store_type"`   // 存储方式
	DriverType      string          `yaml:"driver_type" json:"driver_type"` // 驱动类型
	Width           int             `yaml:"width" json:"width"`             // 验证码宽度
	Height          int             `yaml:"height" json:"height"`           // 验证码高度
	Length          int             `yaml:"length" json:"length"`           // 验证码长度
	MaxSkew         float64         `yaml:"max_skew" json:"max_skew"`
	DotCount        int             `yaml:"dot_count" json:"dot_count"` // 点的数量
	NoiseCount      int             `yaml:"noise_count" json:"noise_count"`
	ShowLineOptions int             `yaml:"show_line_options" json:"show_line_options"`
	BgColor         BackgroundColor `yaml:"background_color" json:"background_color"` // 图片背景颜色
}

CaptchaConfig 验证码配置

type Driver added in v0.1.1

type Driver interface {
	// DrawCaptcha 绘制二进制图片
	DrawCaptcha(content string) (item Item, err error)

	// GenerateIdQuestionAnswer 生成ID,问题和答案
	GenerateIdQuestionAnswer() (id, q, a string)
}

Driver 驱动器接口

type DriverChinese added in v0.1.1

type DriverChinese struct {
	Height          int         // png图片像素高度
	Width           int         // png图片像素宽度
	NoiseCount      int         // 文本noise数量
	ShowLineOptions int         // 线条数量
	Length          int         // 字符长度
	Source          string      // 字符串源
	BgColor         *color.RGBA // 背景颜色

	Fonts []string // 字体列表
	// contains filtered or unexported fields
}

DriverChinese 中文驱动

func NewDriverChinese added in v0.1.1

func NewDriverChinese(cfg CaptchaConfig) *DriverChinese

NewDriverChinese 创建中文验证码驱动 func NewDriverChinese(height int, width int, noiseCount int, showLineOptions int, length int, source string, bgColor *color.RGBA, fontsStorage FontsStorage, fonts []string) *DriverChinese {

func (*DriverChinese) ConvertFonts added in v0.1.1

func (d *DriverChinese) ConvertFonts() *DriverChinese

ConvertFonts 加载字体

func (*DriverChinese) DrawCaptcha added in v0.1.1

func (d *DriverChinese) DrawCaptcha(content string) (item Item, err error)

DrawCaptcha 生成验证码

func (*DriverChinese) GenerateIdQuestionAnswer added in v0.1.1

func (d *DriverChinese) GenerateIdQuestionAnswer() (id, content, answer string)

GenerateIdQuestionAnswer 生成验证码ID,问题和答案

type DriverDigit added in v0.1.1

type DriverDigit struct {
	Height   int     // 图片高度
	Width    int     // 图片宽度
	Length   int     // 图片长度
	MaxSkew  float64 // skew最大
	DotCount int     // 点的数量
}

DriverDigit 数字验证码驱动

func NewDriverDigit added in v0.1.1

func NewDriverDigit(cfg CaptchaConfig) *DriverDigit

NewDriverDigit 创建验证码数字驱动

func (*DriverDigit) DrawCaptcha added in v0.1.1

func (d *DriverDigit) DrawCaptcha(content string) (item Item, err error)

DrawCaptcha 创建数字验证码对象

func (*DriverDigit) GenerateIdQuestionAnswer added in v0.1.1

func (d *DriverDigit) GenerateIdQuestionAnswer() (id, q, a string)

GenerateIdQuestionAnswer 生成验证码的ID,问题和答案

type DriverLanguage added in v0.1.1

type DriverLanguage struct {
	Height          int         // 图片高度
	Width           int         // 图片宽度
	NoiseCount      int         // noise数量
	ShowLineOptions int         // 线条
	Length          int         // 验证码长度
	BgColor         *color.RGBA // 背景颜色

	Fonts        []*truetype.Font // 字体列表
	LanguageCode string
	// contains filtered or unexported fields
}

DriverLanguage 语言驱动

func NewDriverLanguage added in v0.1.1

func NewDriverLanguage(height int, width int, noiseCount int, showLineOptions int, length int, bgColor *color.RGBA, fontsStorage FontsStorage, fonts []*truetype.Font, languageCode string) *DriverLanguage

NewDriverLanguage 创建一个语言驱动

func (*DriverLanguage) DrawCaptcha added in v0.1.1

func (d *DriverLanguage) DrawCaptcha(content string) (item Item, err error)

DrawCaptcha 绘制验证码

func (*DriverLanguage) GenerateIdQuestionAnswer added in v0.1.1

func (d *DriverLanguage) GenerateIdQuestionAnswer() (id, content, answer string)

GenerateIdQuestionAnswer 创建ID,问题和答案

type DriverMath added in v0.1.1

type DriverMath struct {
	Height          int         // 图片高度
	Width           int         // 图片宽度
	NoiseCount      int         // noise数量
	ShowLineOptions int         // 线条选项
	BgColor         *color.RGBA // 背景颜色

	Fonts []string
	// contains filtered or unexported fields
}

DriverMath 数学验证码

func NewDriverMath added in v0.1.1

func NewDriverMath(cfg CaptchaConfig) *DriverMath

NewDriverMath 创建一个数学驱动 func NewDriverMath(height int, width int, noiseCount int, showLineOptions int, bgColor *color.RGBA, fontsStorage FontsStorage, fonts []string) *DriverMath {

func (*DriverMath) ConvertFonts added in v0.1.1

func (d *DriverMath) ConvertFonts() *DriverMath

ConvertFonts 加载字体

func (*DriverMath) DrawCaptcha added in v0.1.1

func (d *DriverMath) DrawCaptcha(question string) (item Item, err error)

DrawCaptcha 生成验证码

func (*DriverMath) GenerateIdQuestionAnswer added in v0.1.1

func (d *DriverMath) GenerateIdQuestionAnswer() (id, question, answer string)

GenerateIdQuestionAnswer 创建ID,问题和答案

type DriverString added in v0.1.1

type DriverString struct {
	Height          int         // 图片高度
	Width           int         // 图片宽度
	NoiseCount      int         // noise数量
	ShowLineOptions int         // 线条数量
	Length          int         // 字符串长度
	Source          string      // 源
	BgColor         *color.RGBA // 背景颜色

	Fonts []string
	// contains filtered or unexported fields
}

DriverString 字符串驱动

func NewDriverString added in v0.1.1

func NewDriverString(height int, width int, noiseCount int, showLineOptions int, length int, source string, bgColor *color.RGBA, fontsStorage FontsStorage, fonts []string) *DriverString

NewDriverString 创建字符串驱动

func (*DriverString) ConvertFonts added in v0.1.1

func (d *DriverString) ConvertFonts() *DriverString

ConvertFonts 加载字体

func (*DriverString) DrawCaptcha added in v0.1.1

func (d *DriverString) DrawCaptcha(content string) (item Item, err error)

DrawCaptcha 创建验证码

func (*DriverString) GenerateIdQuestionAnswer added in v0.1.1

func (d *DriverString) GenerateIdQuestionAnswer() (id, content, answer string)

GenerateIdQuestionAnswer 创建ID,问题和答案

type EmbeddedFontsStorage added in v0.1.1

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

func NewEmbeddedFontsStorage added in v0.1.1

func NewEmbeddedFontsStorage(fs embed.FS) *EmbeddedFontsStorage

NewEmbeddedFontsStorage 创建字体存储器

func (*EmbeddedFontsStorage) LoadFontByName added in v0.1.1

func (s *EmbeddedFontsStorage) LoadFontByName(name string) *truetype.Font

LoadFontByName 根据名字加载字体

func (*EmbeddedFontsStorage) LoadFontsByNames added in v0.1.1

func (s *EmbeddedFontsStorage) LoadFontsByNames(assetFontNames []string) []*truetype.Font

LoadFontsByNames 根据名称列表加载字体列表

type FontsStorage added in v0.1.1

type FontsStorage interface {
	// LoadFontByName 根据名字加载字体
	LoadFontByName(name string) *truetype.Font

	// LoadFontsByNames 根据名称列表加载字体列表
	LoadFontsByNames(assetFontNames []string) []*truetype.Font
}

FontsStorage 字体接口

type Item added in v0.1.1

type Item interface {
	//WriteTo 写入到一个输出对象
	WriteTo(w io.Writer) (n int64, err error)

	// EncodeB64string 转换为base64字符串
	EncodeB64string() string
}

Item 二进制验证码接口

type ItemChar added in v0.1.1

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

ItemChar 字符串验证码对象

func NewItemChar added in v0.1.1

func NewItemChar(w int, h int, bgColor color.RGBA) *ItemChar

NewItemChar 创建字符验证码对象

func (*ItemChar) BinaryEncoding added in v0.1.1

func (item *ItemChar) BinaryEncoding() []byte

BinaryEncoding 返回图片的字节数组

func (*ItemChar) EncodeB64string added in v0.1.1

func (item *ItemChar) EncodeB64string() string

EncodeB64string 将图片转化为base64编码的字符串

func (*ItemChar) WriteTo added in v0.1.1

func (item *ItemChar) WriteTo(w io.Writer) (int64, error)

WriteTo 将数据写入到输出流

type ItemDigit added in v0.1.1

type ItemDigit struct {
	*image.Paletted
	// contains filtered or unexported fields
}

ItemDigit 数字验证码

func NewItemDigit added in v0.1.1

func NewItemDigit(width int, height int, dotCount int, maxSkew float64) *ItemDigit

NewItemDigit 创建数字验证码实例

func (*ItemDigit) EncodeB64string added in v0.1.1

func (m *ItemDigit) EncodeB64string() string

EncodeB64string 将图片转换为base64编码

func (*ItemDigit) EncodeBinary added in v0.1.1

func (m *ItemDigit) EncodeBinary() []byte

EncodeBinary 获取图片的二进制字节数组

func (*ItemDigit) WriteTo added in v0.1.1

func (m *ItemDigit) WriteTo(w io.Writer) (int64, error)

WriteTo 将数据写入到输出流

type Store added in v0.1.1

type Store interface {
	// Set 设置验证码答案
	Set(id string, value string) error

	// Get 获取验证码答案
	Get(id string, clear bool) string

	//Verify 验证验证码答案
	Verify(id, answer string, clear bool) bool
}

Store 存储器接口

func NewMemoryStore added in v0.1.1

func NewMemoryStore(collectNum int, expiration time.Duration) Store

NewMemoryStore 创建内存存储对象

type StoreSyncMap added in v0.1.1

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

StoreSyncMap 使用线程安全的map存储

func NewStoreSyncMap added in v0.1.1

func NewStoreSyncMap(liveTime time.Duration) *StoreSyncMap

NewStoreSyncMap 创建新的实例

func (StoreSyncMap) Get added in v0.1.1

func (s StoreSyncMap) Get(id string, clear bool) string

Get 获取验证码答案

func (StoreSyncMap) Set added in v0.1.1

func (s StoreSyncMap) Set(id string, value string)

Set 设置验证码答案

func (StoreSyncMap) Verify added in v0.1.1

func (s StoreSyncMap) Verify(id, answer string, clear bool) bool

Verify 校验验证码答案

type ZCaptcha added in v0.1.1

type ZCaptcha struct {
	Config *CaptchaConfig // 配置对象

	// 方法区
	Generate func() (id, b64s string, err error)
	Verify   func(id, answer string, clear bool) bool
	// contains filtered or unexported fields
}

func New

func New() *ZCaptcha

New 创建新的验证码对象

func NewChinese added in v0.1.1

func NewChinese() *ZCaptcha

func NewMath added in v0.1.1

func NewMath() *ZCaptcha

func NewStr added in v0.1.1

func NewStr() *ZCaptcha

func NewWitchConfig added in v0.1.1

func NewWitchConfig(config CaptchaConfig) *ZCaptcha

NewWitchConfig 使用配置对象创建captcha

Directories

Path Synopsis
examples
str

Jump to

Keyboard shortcuts

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