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 ΒΆ
const ( //TxtNumbers chacters for numbers. TxtNumbers = "012346789" //TxtAlphabet characters for alphabet. TxtAlphabet = "ABCDEFGHJKMNOQRSTUVXYZabcdefghjkmnoqrstuvxyz" //MimeTypeImage output base64 mine-type. MimeTypeImage = "image/png" )
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 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 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