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 DriverAudio
- type DriverChinese
- type DriverDigit
- type DriverLanguage
- type DriverMath
- type DriverString
- type EmbeddedFontsStorage
- type FontsStorage
- type Item
- type ItemAudio
- 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" //TxtChineseCharaters makes characters in chinese TxtChineseCharaters = "็ไธๆฏๅจไธไบๆๅไบบ่ฟไธญๅคงไธบไธไธชๅฝๆไปฅ่ฆไป" + "ๆถๆฅ็จไปฌ็ๅฐไฝๅฐไบๅบๅฐฑๅๅฏนๆไผๅฏไธปๅๅนดๅจ" + "ๅๅทฅไน่ฝไธ่ฟๅญ่ฏดไบง็ง้ข่ๆนๅๅคๅฎ่กๅญฆๆณๆ" + "ๆฐๅพ็ปๅไธไน่ฟ็็ญ้จๅบฆๅฎถ็ตๅ้ๅฆๆฐดๅ้ซ่ช" + "ไบ็่ตทๅฐ็ฉ็ฐๅฎๅ ้้ฝไธคไฝๅถๆบๅฝไฝฟ็นไปไธๆฌ" + "ๅปๆๆงๅฅฝๅบๅผๅฎๅ่ฟๅ ็ฑๅ ถไบ็ถๅๅคๅคฉๆฟๅๆฅ" + "้ฃ็คพไนไบๅนณๅฝข็ธๅ จ่กจ้ดๆ ทไธๅ ณๅ้ๆฐ็บฟๅ ๆฐๆญฃ" + "ๅฟๅไฝ ๆ็ๅๅไนๅฉๆฏๆไฝ่ดจๆฐ็ฌฌๅ้ๅฝๆญคๅ" + "ๆกๅชๆฒก็ป่งฃ้ฎๆๅปบๆๅ ฌๆ ็ณปๅๅพๆ ่ ๆ็ซไปฃๆณ" + "ๅทฒ้ๅนถๆ็ด้ขๅ ็จๅฑไบๆๆ่ฑกๅ้ฉไฝๅ ฅๅธธๆๆป" + "ๆฌกๅๅผๆดป่ฎพๅ็ฎก็นไปถ้ฟๆฑ่ๅคดๅบ่ต่พนๆต่ทฏ็บงๅฐ" + "ๅพๅฑฑ็ปๆฅ็ฅ่พๅฐ็ป่ง่ฎกๅซๅฅนๆ่งๆๆ น่ฎบ่ฟๅๆ" + "ๅ ไนๅบๅผบๆพๅณ่ฅฟ่ขซๅนฒๅๅฟ ๆๅ ๅๅไปปๅๆฎๅค้" + "ๅ็ป่ฒๅ ้จๅณไฟๆฒปๅ้ ็พ่ง็ญ้ขไธๆตทๅฃไธๅฏผๅจ" + "ๅๅฟไธ้ๅขไบๆต้ถๆฒนๆๆฏๆไบคๅ่ไป่ฎคๅ ญๅ ฑๆ" + "ๆถ่ฏๆนๆธ ๅทฑ็พๅ้่ฝฌๆดๅ้ฃๅๆ็ฝๆ้่ฑๅธฆๅฎ" + "ๅบ่บซ่ฝฆไพ็ๅกๅ ทไธๆฏ็ฎ่ณ่พพ่ตฐ็งฏ็คบ่ฎฎๅฃฐๆฅๆๅฎ" + "็ฑปๅ ซ็ฆปๅๅ็กฎๆ็งๅผ ไฟก้ฉฌ่่ฏ็ฑณๆด็ฉบๅ ๅตไป้" + "ๆธฉไผ ๅ่ฎธๆญฅ็พคๅนฟ็ณ่ฎฐ้ๆฎต็ ็ๆๆๅพๅซไธ็ฉถ่ง" + "่ถ็ป่ฃ ๅฝฑ็ฎไฝๆ้ณไผไนฆๅธๅคๅฎนๅฟ้กป้ ๅ้้ช่ฟ" + "ๆญๆทฑ้พ่ฟ็ฟๅๅจๅง็ด ๆๅคๅๅ้็ๅไน ๅ็บฆๆฏ" + "่ฌๅฒๆๅณไพฟๅขๅพ้ ธๅๅธๅ ไฝ้คๆถๆๅบ็งฐๅคชๅ็ฒพ" + "ๅผๅท็ๆ็ปดๅ้ๆ ๅๅญๅๆฏไบฒๅฟซๆๆฏ้ขๆฅๆฑๅ" + "็ผ็ๆๆ ผๅ ปๆ็ฝฎๆดพๅฑ็ๅงๅดไธ็ถ่ฒๅไบฌ่ฏ้ๅฑ" + "ๅๅ ็ซไฝ่ฐๆปกๅฟๅฑ็ งๅ็บข็ปๅผๅฌ่ฏฅ้ไปทไธฅ้พ้ฃ" //MimeTypeAudio output base64 mine-type. MimeTypeAudio = "audio/wav" //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 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 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 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
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 }
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, 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 Store ยถ
type Store interface { // Set sets the digits for the captcha id. Set(id string, value string) error // 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
Source Files ยถ
- 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_sync_map.go
- util.go
- util_audio.go