Documentation
¶
Overview ¶
Package base64Captcha 验证码包 支持数字、字母、算术、音频和数字字母混合验证码 用于快速开发Go语言的RESTful API、Web应用和后端服务 给定一个字符串标识符,返回base64编码的PNG图片字符串
Index ¶
- Constants
- Variables
- func RandColor() color.RGBA
- func RandDeepColor() color.RGBA
- func RandLightColor() color.RGBA
- func RandText(size int, sourceChars string) string
- func RandomId() string
- type Captcha
- type Driver
- type DriverAudio
- type DriverChinese
- type DriverDigit
- type DriverLanguage
- type DriverMath
- type DriverString
- type EmbeddedFontsStorage
- type FontsStorage
- type Item
- type ItemAudio
- type ItemChar
- type ItemDigit
- type RedisConfig
- type Store
- type StoreRedis
- func (s *StoreRedis) Close() error
- func (s *StoreRedis) Get(id string, clear bool) string
- func (s *StoreRedis) GetClient() redis.UniversalClient
- func (s *StoreRedis) Ping() error
- func (s *StoreRedis) Set(id string, value string) error
- func (s *StoreRedis) SetContext(ctx context.Context)
- func (s *StoreRedis) Verify(id, answer string, clear bool) bool
- type StoreSyncMap
Constants ¶
const ( // TxtNumbers 数字字符集 TxtNumbers = "012346789" // TxtAlphabet 字母字符集 TxtAlphabet = "ABCDEFGHJKMNOQRSTUVXYZabcdefghjkmnoqrstuvxyz" // TxtSimpleCharaters 简单的数字和字母字符集 TxtSimpleCharaters = "13467ertyiadfhjkxcvbnERTYADFGHJKXCVBN" // TxtChineseCharaters 中文汉字字符集 TxtChineseCharaters = "的一是在不了有和人这中大为上个国我以要他" + "时来用们生到作地于出就分对成会可主发年动" + "同工也能下过子说产种面而方后多定行学法所" + "民得经十三之进着等部度家电力里如水化高自" + "二理起小物现实加量都两体制机当使点从业本" + "去把性好应开它合还因由其些然前外天政四日" + "那社义事平形相全表间样与关各重新线内数正" + "心反你明看原又么利比或但质气第向道命此变" + "条只没结解问意建月公无系军很情者最立代想" + "已通并提直题党程展五果料象员革位入常文总" + "次品式活设及管特件长求老头基资边流路级少" + "图山统接知较将组见计别她手角期根论运农指" + "几九区强放决西被干做必战先回则任取据处队" + "南给色光门即保治北造百规热领七海口东导器" + "压志世金增争济阶油思术极交受联什认六共权" + "收证改清己美再采转更单风切打白教速花带安" + "场身车例真务具万每目至达走积示议声报斗完" + "类八离华名确才科张信马节话米整空元况今集" + "温传土许步群广石记需段研界拉林律叫且究观" + "越织装影算低持音众书布复容儿须际商非验连" + "断深难近矿千周委素技备半办青省列习响约支" + "般史感劳便团往酸历市克何除消构府称太准精" + "值号率族维划选标写存候毛亲快效斯院查江型" + "眼王按格养易置派层片始却专状育厂京识适属" + "圆包火住调满县局照参红细引听该铁价严龙飞" // MimeTypeAudio 音频验证码的base64 MIME类型 MimeTypeAudio = "audio/wav" // MimeTypeImage 图片验证码的base64 MIME类型 MimeTypeImage = "image/png" // Emoji 表情符号字符集 Emoji = "" /* 268-byte string literal not displayed */ )
const ( // OptionShowHollowLine 显示空心线选项 OptionShowHollowLine = 2 // OptionShowSlimeLine 显示细线选项 OptionShowSlimeLine = 4 // OptionShowSineLine 显示正弦曲线选项 OptionShowSineLine = 8 )
Variables ¶
var ( // GCLimitNumber 默认存储触发垃圾回收的验证码数量阈值 GCLimitNumber = 10240 // Expiration 默认存储的验证码过期时间 Expiration = 10 * time.Minute // DefaultMemStore 默认的共享验证码存储实例 DefaultMemStore = NewMemoryStore(GCLimitNumber, Expiration) )
var DefaultDriverAudio = NewDriverAudio(6, "en")
DefaultDriverAudio is a default audio driver
var DefaultDriverDigit = NewDriverDigit(80, 240, 5, 0.7, 80)
DefaultDriverDigit is a default driver of digit
var DefaultEmbeddedFonts = NewEmbeddedFontsStorage(defaultEmbeddedFontsFS)
Functions ¶
func RandLightColor ¶
RandLightColor get random ligth color. 随机生成浅色.
Types ¶
type Captcha ¶
Captcha 验证码基本信息结构体
func NewCaptcha ¶
NewCaptcha 创建验证码实例 driver: 验证码驱动(数字、字符串、数学等) store: 存储后端(内存、Redis等)
type Driver ¶
type Driver interface {
// DrawCaptcha 绘制验证码图像/音频
DrawCaptcha(content string) (item Item, err error)
// GenerateIdQuestionAnswer 生成随机的验证码ID、问题内容和答案
GenerateIdQuestionAnswer() (id, q, a string)
}
Driver 验证码驱动接口 用于验证码引擎生成验证码内容
type DriverAudio ¶
type DriverAudio struct {
// Length Default number of digits in captcha solution.
Length int
// Language possible values for lang are "en", "ja", "ru", "zh".
Language string
}
DriverAudio captcha config for captcha-engine-audio.
func NewDriverAudio ¶
func NewDriverAudio(length int, language string) *DriverAudio
NewDriverAudio creates a driver of audio
func (*DriverAudio) DrawCaptcha ¶
func (d *DriverAudio) DrawCaptcha(content string) (item Item, err error)
DrawCaptcha creates audio captcha item
func (*DriverAudio) GenerateIdQuestionAnswer ¶
func (d *DriverAudio) GenerateIdQuestionAnswer() (id, q, a string)
GenerateIdQuestionAnswer creates id,captcha content and answer
type DriverChinese ¶
type DriverChinese struct {
//Height png height in pixel.
Height int
//Width Captcha png width in pixel.
Width int
//NoiseCount text noise count.
NoiseCount int
//ShowLineOptions := OptionShowHollowLine | OptionShowSlimeLine | OptionShowSineLine .
ShowLineOptions int
//Length random string length.
Length int
//Source is a unicode which is the rand string from.
Source string
//BgColor captcha image background color (optional)
BgColor *color.RGBA
//Fonts loads by name see fonts.go's comment
Fonts []string
// contains filtered or unexported fields
}
DriverChinese is a driver of unicode Chinese characters.
func NewDriverChinese ¶
func NewDriverChinese(height int, width int, noiseCount int, showLineOptions int, length int, source string, bgColor *color.RGBA, fontsStorage FontsStorage, fonts []string) *DriverChinese
NewDriverChinese creates a driver of Chinese characters
func (*DriverChinese) ConvertFonts ¶
func (d *DriverChinese) ConvertFonts() *DriverChinese
ConvertFonts loads fonts by names
func (*DriverChinese) DrawCaptcha ¶
func (d *DriverChinese) DrawCaptcha(content string) (item Item, err error)
DrawCaptcha generates captcha item(image)
func (*DriverChinese) GenerateIdQuestionAnswer ¶
func (d *DriverChinese) GenerateIdQuestionAnswer() (id, content, answer string)
GenerateIdQuestionAnswer generates captcha content and its answer
type DriverDigit ¶
type DriverDigit struct {
// Height png height in pixel.
Height int
// Width Captcha png width in pixel.
Width int
// DefaultLen Default number of digits in captcha solution.
Length int
// MaxSkew max absolute skew factor of a single digit.
MaxSkew float64
// DotCount Number of background circles.
DotCount int
}
DriverDigit config for captcha-engine-digit.
func NewDriverDigit ¶
NewDriverDigit creates a driver of digit
func (*DriverDigit) DrawCaptcha ¶
func (d *DriverDigit) DrawCaptcha(content string) (item Item, err error)
DrawCaptcha creates digit captcha item
func (*DriverDigit) GenerateIdQuestionAnswer ¶
func (d *DriverDigit) GenerateIdQuestionAnswer() (id, q, a string)
GenerateIdQuestionAnswer creates captcha content and answer
func (*DriverDigit) GenerateSpecificIdQuestionAnswer ¶
func (d *DriverDigit) GenerateSpecificIdQuestionAnswer(mId string) (id, q, a string)
GenerateIdQuestionAnswer creates captcha content and answer
type DriverLanguage ¶
type DriverLanguage struct {
// Height png height in pixel.
Height int
// Width Captcha png width in pixel.
Width int
//NoiseCount text noise count.
NoiseCount int
//ShowLineOptions := OptionShowHollowLine | OptionShowSlimeLine | OptionShowSineLine .
ShowLineOptions int
//Length random string length.
Length int
//BgColor captcha image background color (optional)
BgColor *color.RGBA
//Fonts loads by name see fonts.go's comment
Fonts []*truetype.Font
LanguageCode string
// contains filtered or unexported fields
}
DriverLanguage generates language unicode by lanuage
func NewDriverLanguage ¶
func NewDriverLanguage(height int, width int, noiseCount int, showLineOptions int, length int, bgColor *color.RGBA, fontsStorage FontsStorage, fonts []*truetype.Font, languageCode string) *DriverLanguage
NewDriverLanguage creates a driver
func (*DriverLanguage) DrawCaptcha ¶
func (d *DriverLanguage) DrawCaptcha(content string) (item Item, err error)
DrawCaptcha creates item
func (*DriverLanguage) GenerateIdQuestionAnswer ¶
func (d *DriverLanguage) GenerateIdQuestionAnswer() (id, content, answer string)
GenerateIdQuestionAnswer creates content and answer
type DriverMath ¶
type DriverMath struct {
//Height png height in pixel.
Height int
// Width Captcha png width in pixel.
Width int
//NoiseCount text noise count.
NoiseCount int
//ShowLineOptions := OptionShowHollowLine | OptionShowSlimeLine | OptionShowSineLine .
ShowLineOptions int
//BgColor captcha image background color (optional)
BgColor *color.RGBA
//Fonts loads by name see fonts.go's comment
Fonts []string
// contains filtered or unexported fields
}
DriverMath captcha config for captcha math
func NewDriverMath ¶
func NewDriverMath(height int, width int, noiseCount int, showLineOptions int, bgColor *color.RGBA, fontsStorage FontsStorage, fonts []string) *DriverMath
NewDriverMath creates a driver of math
func (*DriverMath) ConvertFonts ¶
func (d *DriverMath) ConvertFonts() *DriverMath
ConvertFonts loads fonts from names
func (*DriverMath) DrawCaptcha ¶
func (d *DriverMath) DrawCaptcha(question string) (item Item, err error)
DrawCaptcha creates math captcha item
func (*DriverMath) GenerateIdQuestionAnswer ¶
func (d *DriverMath) GenerateIdQuestionAnswer() (id, question, answer string)
GenerateIdQuestionAnswer creates id,captcha content and answer
type DriverString ¶
type DriverString struct {
// Height png height in pixel.
Height int
// Width Captcha png width in pixel.
Width int
//NoiseCount text noise count.
NoiseCount int
//ShowLineOptions := OptionShowHollowLine | OptionShowSlimeLine | OptionShowSineLine .
ShowLineOptions int
//Length random string length.
Length int
//Source is a unicode which is the rand string from.
Source string
//BgColor captcha image background color (optional)
BgColor *color.RGBA
//Fonts loads by name see fonts.go's comment
Fonts []string
// contains filtered or unexported fields
}
DriverString captcha config for captcha-engine-characters.
func NewDriverString ¶
func NewDriverString(height int, width int, noiseCount int, showLineOptions int, length int, source string, bgColor *color.RGBA, fontsStorage FontsStorage, fonts []string) *DriverString
NewDriverString creates driver
func (*DriverString) ConvertFonts ¶
func (d *DriverString) ConvertFonts() *DriverString
ConvertFonts loads fonts by names
func (*DriverString) DrawCaptcha ¶
func (d *DriverString) DrawCaptcha(content string) (item Item, err error)
DrawCaptcha draws captcha item
func (*DriverString) GenerateIdQuestionAnswer ¶
func (d *DriverString) GenerateIdQuestionAnswer() (id, content, answer string)
GenerateIdQuestionAnswer creates id,content and answer
type EmbeddedFontsStorage ¶
type EmbeddedFontsStorage struct {
// contains filtered or unexported fields
}
func NewEmbeddedFontsStorage ¶
func NewEmbeddedFontsStorage(fs embed.FS) *EmbeddedFontsStorage
func (*EmbeddedFontsStorage) LoadFontByName ¶
func (s *EmbeddedFontsStorage) LoadFontByName(name string) *truetype.Font
func (*EmbeddedFontsStorage) LoadFontsByNames ¶
func (s *EmbeddedFontsStorage) LoadFontsByNames(assetFontNames []string) []*truetype.Font
LoadFontsByNames import fonts from dir. make the simple-font(RitaSmith.ttf) the first font of trueTypeFonts.
type FontsStorage ¶
type FontsStorage interface {
// LoadFontByName returns the font from the storage
LoadFontByName(name string) *truetype.Font
// LoadFontsByNames returns multiple fonts from storage
LoadFontsByNames(assetFontNames []string) []*truetype.Font
}
FontsStorage interface for working with fonts
type Item ¶
type Item interface {
//WriteTo writes to a writer
WriteTo(w io.Writer) (n int64, err error)
//EncodeB64string encodes as base64 string
EncodeB64string() string
}
Item is captcha item interface
type ItemAudio ¶
type ItemAudio struct {
// contains filtered or unexported fields
}
ItemAudio captcha-audio-engine return type.
func (*ItemAudio) EncodeB64string ¶
EncodeB64string encodes a sound to base64 string
type ItemChar ¶
type ItemChar struct {
// contains filtered or unexported fields
}
ItemChar captcha item of unicode characters
func NewItemChar ¶
NewItemChar creates a captcha item of characters
func (*ItemChar) BinaryEncoding ¶
BinaryEncoding encodes an image to PNG and returns a byte slice.
func (*ItemChar) EncodeB64string ¶
EncodeB64string encodes an image to base64 string
type ItemDigit ¶
ItemDigit digits captcha Struct
func NewItemDigit ¶
NewItemDigit create a instance of item-digit
func (*ItemDigit) EncodeB64string ¶
EncodeB64string encodes an image to base64 string
func (*ItemDigit) EncodeBinary ¶
EncodeBinary encodes an image to PNG and returns a byte slice.
type RedisConfig ¶
type RedisConfig struct {
// Addr Redis地址,单机模式使用,如 "localhost:6379"
Addr string
// Addrs Redis集群地址列表,集群模式使用
Addrs []string
// Password Redis密码
Password string
// DB Redis数据库索引(0-15),集群模式不支持
DB int
// PoolSize 连接池大小
PoolSize int
// KeyPrefix Redis键前缀,默认为 "captcha:"
KeyPrefix string
// Expiration 验证码过期时间,默认10分钟
Expiration time.Duration
// MasterName 哨兵模式的主节点名称
MasterName string
}
RedisConfig Redis配置
type Store ¶
type Store interface {
// Set 为验证码ID设置答案值
Set(id string, value string) error
// Get 返回验证码ID对应的存储值
// clear 参数指示是否需要从存储中删除该验证码
Get(id string, clear bool) string
// Verify 直接验证验证码答案
Verify(id, answer string, clear bool) bool
}
Store 存储接口 实现 Store 接口的对象可以用于处理验证码ID和答案的存储与检索, 替换默认的内存存储。
对象有责任在必要时删除过期和已使用的验证码 (例如,默认内存存储在存储一定数量的验证码后会在 Set 方法中收集它们)
type StoreRedis ¶
type StoreRedis struct {
// contains filtered or unexported fields
}
StoreRedis Redis存储后端 使用Redis作为验证码的存储介质,支持分布式部署
func NewStoreRedis ¶
func NewStoreRedis(config RedisConfig) *StoreRedis
NewStoreRedis 创建Redis存储实例 config: Redis配置
func NewStoreRedisWithClient ¶
func NewStoreRedisWithClient(client redis.UniversalClient, expiration time.Duration, keyPrefix string) *StoreRedis
NewStoreRedisWithClient 使用现有Redis客户端创建存储实例 适用于已有Redis连接的场景
func (*StoreRedis) Get ¶
func (s *StoreRedis) Get(id string, clear bool) string
Get 获取验证码答案 id: 验证码ID clear: 是否在获取后删除
func (*StoreRedis) GetClient ¶
func (s *StoreRedis) GetClient() redis.UniversalClient
GetClient 获取Redis客户端 可用于执行其他Redis操作
func (*StoreRedis) Set ¶
func (s *StoreRedis) Set(id string, value string) error
Set 存储验证码ID和答案 id: 验证码ID value: 验证码答案
func (*StoreRedis) SetContext ¶
func (s *StoreRedis) SetContext(ctx context.Context)
SetContext 设置上下文
type StoreSyncMap ¶
type StoreSyncMap struct {
// contains filtered or unexported fields
}
StoreSyncMap 使用 sync.Map 作为存储后端
func NewStoreSyncMap ¶
func NewStoreSyncMap(liveTime time.Duration) *StoreSyncMap
NewStoreSyncMap 创建 StoreSyncMap 实例 liveTime: 验证码存活时间
Source Files
¶
- 2fa.go
- captcha.go
- const.go
- driver_audio.go
- driver_chinese.go
- driver_digit.go
- driver_language.go
- driver_math.go
- driver_string.go
- fonts.go
- fonts_embedded.go
- fonts_embedded_default.go
- interface_driver.go
- interface_fonts.go
- interface_item.go
- interface_store.go
- item_audio.go
- item_char.go
- item_digit.go
- random_math.go
- sounds.go
- store_memory.go
- store_memory_default.go
- store_redis.go
- store_sync_map.go
- util.go
- util_audio.go