Documentation ยถ
Overview ยถ
Package base64Captcha supports digits, numbers,alphabet, arithmetic, audio and digit-alphabet captcha. base64Captcha is used for fast development of RESTful APIs, web apps and backend services in Go. give a string identifier to the package and it returns with a base64-encoding-png-string
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 DriverDigit
- type DriverMath
- type DriverString
- type Item
- type ItemChar
- type ItemDigit
- type Store
- type StoreSyncMap
Constants ยถ
const ( //TxtNumbers chacters for numbers. TxtNumbers = "012346789" //TxtAlphabet characters for alphabet. TxtAlphabet = "ABCDEFGHJKMNOQRSTUVXYZabcdefghjkmnoqrstuvxyz" //TxtSimpleCharaters simple numbers and alphabet TxtSimpleCharaters = "13467ertyiadfhjkxcvbnERTYADFGHJKXCVBN" //MimeTypeImage output base64 mine-type. MimeTypeImage = "image/png" //Emoji is a source string for randTxt Emoji = "" /* 268-byte string literal not displayed */ )
const ( //OptionShowHollowLine shows hollow line OptionShowHollowLine = 2 //OptionShowSlimeLine shows slime line OptionShowSlimeLine = 4 //OptionShowSineLine shows sine line OptionShowSineLine = 8 )
Variables ยถ
var ( // GCLimitNumber The number of captchas created that triggers garbage collection used by default store. GCLimitNumber = 10240 // Expiration time of captchas used by default store. Expiration = 10 * time.Minute // DefaultMemStore is a shared storage for captchas, generated by New function. DefaultMemStore = NewMemoryStore(GCLimitNumber, Expiration) )
var DefaultDriverDigit = NewDriverDigit(80, 240, 5, 0.7, 80)
DefaultDriverDigit is a default driver of digit
Functions ยถ
func RandDeepColor ยถ
RandDeepColor get random deep color. ้ๆบ็ๆๆทฑ่ฒ็ณป.
func RandLightColor ยถ
RandLightColor get random ligth color. ้ๆบ็ๆๆต ่ฒ.
Types ยถ
type Captcha ยถ
Captcha captcha basic information.
func NewCaptcha ยถ
NewCaptcha creates a captcha instance from driver and store
type Driver ยถ
type Driver interface { //DrawCaptcha draws binary item DrawCaptcha(content string) (item Item, err error) //GenerateIdQuestionAnswer creates rand id, content and answer GenerateIdQuestionAnswer() (id, q, a string) }
Driver captcha interface for captcha engine to to write staff
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
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, 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 }
DriverChar 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, 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 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 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 Store ยถ
type Store interface { // Set sets the digits for the captcha id. Set(id string, value string) // Get returns stored digits for the captcha id. Clear indicates // whether the captcha must be deleted from the store. Get(id string, clear bool) string //Verify captcha's answer directly Verify(id, answer string, clear bool) bool }
Store An object implementing Store interface can be registered with SetCustomStore function to handle storage and retrieval of captcha ids and solutions for them, replacing the default memory store.
It is the responsibility of an object to delete expired and used captchas when necessary (for example, the default memory store collects them in Set method after the certain amount of captchas has been stored.)
func NewMemoryStore ยถ
NewMemoryStore returns a new standard memory store for captchas with the given collection threshold and expiration time (duration). The returned store must be registered with SetCustomStore to replace the default one.
type StoreSyncMap ยถ
type StoreSyncMap struct {
// contains filtered or unexported fields
}
StoreSyncMap use sync.Map as store
func NewStoreSyncMap ยถ
func NewStoreSyncMap(liveTime time.Duration) *StoreSyncMap
NewStoreSyncMap new a instance
func (StoreSyncMap) Get ยถ
func (s StoreSyncMap) Get(id string, clear bool) string
Set a string value
func (StoreSyncMap) Set ยถ
func (s StoreSyncMap) Set(id string, value string)
Get get a string value