base64Captcha: github.com/mojocn/base64Captcha Index | Files | Directories

package base64Captcha

import "github.com/mojocn/base64Captcha"

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

Package Files

bindata.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 interface_driver.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

Constants

const (
    TxtNumbers = "012346789"

    TxtAlphabet = "ABCDEFGHJKMNOQRSTUVXYZabcdefghjkmnoqrstuvxyz"

    TxtSimpleCharaters = "13467ertyiadfhjkxcvbnERTYADFGHJKXCVBN"

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

    MimeTypeAudio = "audio/wav"

    MimeTypeImage = "image/png"

    Emoji = "" /* 270 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

func Asset Uses

func Asset(name string) ([]byte, error)

Asset loads and returns the asset for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetDigest Uses

func AssetDigest(name string) ([sha256.Size]byte, error)

AssetDigest returns the digest of the file with the given name. It returns an error if the asset could not be found or the digest could not be loaded.

func AssetDir Uses

func AssetDir(name string) ([]string, error)

AssetDir returns the file names below a certain directory embedded in the file by go-bindata. For example if you run go-bindata on data/... and data contains the following hierarchy:

data/
  foo.txt
  img/
    a.png
    b.png

then AssetDir("data") would return []string{"foo.txt", "img"}, AssetDir("data/img") would return []string{"a.png", "b.png"}, AssetDir("foo.txt") and AssetDir("notexist") would return an error, and AssetDir("") will return []string{"data"}.

func AssetInfo Uses

func AssetInfo(name string) (os.FileInfo, error)

AssetInfo loads and returns the asset info for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetNames Uses

func AssetNames() []string

AssetNames returns the names of the assets.

func AssetString Uses

func AssetString(name string) (string, error)

AssetString returns the asset contents as a string (instead of a []byte).

func Digests Uses

func Digests() (map[string][sha256.Size]byte, error)

Digests returns a map of all known files and their checksums.

func MustAsset Uses

func MustAsset(name string) []byte

MustAsset is like Asset but panics when Asset would return an error. It simplifies safe initialization of global variables.

func MustAssetString Uses

func MustAssetString(name string) string

MustAssetString is like AssetString but panics when Asset would return an error. It simplifies safe initialization of global variables.

func RandColor Uses

func RandColor() color.RGBA

RandColor get random color. 生成随机颜色.

func RandDeepColor Uses

func RandDeepColor() color.RGBA

RandDeepColor get random deep color. 随机生成深色系.

func RandLightColor Uses

func RandLightColor() color.RGBA

RandLightColor get random ligth color. 随机生成浅色.

func RandText Uses

func RandText(size int, sourceChars string) string

RandText creates random text of given size.

func RandomId Uses

func RandomId() string

RandomId returns a new random id key string.

func RestoreAsset Uses

func RestoreAsset(dir, name string) error

RestoreAsset restores an asset under the given directory.

func RestoreAssets Uses

func RestoreAssets(dir, name string) error

RestoreAssets restores an asset under the given directory recursively.

type Captcha Uses

type Captcha struct {
    Driver Driver
    Store  Store
}

Captcha captcha basic information.

func NewCaptcha Uses

func NewCaptcha(driver Driver, store Store) *Captcha

NewCaptcha creates a captcha instance from driver and store

func (*Captcha) Generate Uses

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

Generate generates a random id, base64 image string or an error if any

func (*Captcha) Verify Uses

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

Verify by a given id key and remove the captcha value in store, return boolean value. if you has multiple captcha instances which share a same store. You may want to call `store.Verify` method instead.

type Driver Uses

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 Uses

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 Uses

func NewDriverAudio(length int, language string) *DriverAudio

NewDriverAudio creates a driver of audio

func (*DriverAudio) DrawCaptcha Uses

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

DrawCaptcha creates audio captcha item

func (*DriverAudio) GenerateIdQuestionAnswer Uses

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

GenerateIdQuestionAnswer creates id,captcha content and answer

type DriverChinese Uses

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 Uses

func NewDriverChinese(height int, width int, noiseCount int, showLineOptions int, length int, source string, bgColor *color.RGBA, fonts []string) *DriverChinese

NewDriverChinese creates a driver of Chinese characters

func (*DriverChinese) ConvertFonts Uses

func (d *DriverChinese) ConvertFonts() *DriverChinese

ConvertFonts loads fonts by names

func (*DriverChinese) DrawCaptcha Uses

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

DrawCaptcha generates captcha item(image)

func (*DriverChinese) GenerateIdQuestionAnswer Uses

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

GenerateIdQuestionAnswer generates captcha content and its answer

type DriverDigit Uses

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 Uses

func NewDriverDigit(height int, width int, length int, maxSkew float64, dotCount int) *DriverDigit

NewDriverDigit creates a driver of digit

func (*DriverDigit) DrawCaptcha Uses

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

DrawCaptcha creates digit captcha item

func (*DriverDigit) GenerateIdQuestionAnswer Uses

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

GenerateIdQuestionAnswer creates captcha content and answer

type DriverLanguage Uses

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
}

DriverLanguage generates language unicode by lanuage

func NewDriverLanguage Uses

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

NewDriverLanguage creates a driver

func (*DriverLanguage) DrawCaptcha Uses

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

DrawCaptcha creates item

func (*DriverLanguage) GenerateIdQuestionAnswer Uses

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

GenerateIdQuestionAnswer creates content and answer

type DriverMath Uses

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 Uses

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 Uses

func (d *DriverMath) ConvertFonts() *DriverMath

ConvertFonts loads fonts from names

func (*DriverMath) DrawCaptcha Uses

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

DrawCaptcha creates math captcha item

func (*DriverMath) GenerateIdQuestionAnswer Uses

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

GenerateIdQuestionAnswer creates id,captcha content and answer

type DriverString Uses

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 Uses

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 Uses

func (d *DriverString) ConvertFonts() *DriverString

ConvertFonts loads fonts by names

func (*DriverString) DrawCaptcha Uses

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

DrawCaptcha draws captcha item

func (*DriverString) GenerateIdQuestionAnswer Uses

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

GenerateIdQuestionAnswer creates id,content and answer

type Item Uses

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 Uses

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

ItemAudio captcha-audio-engine return type.

func (*ItemAudio) EncodeB64string Uses

func (a *ItemAudio) EncodeB64string() string

EncodeB64string encodes a sound to base64 string

func (*ItemAudio) WriteTo Uses

func (a *ItemAudio) WriteTo(w io.Writer) (n int64, err error)

WriteTo writes captcha audio in WAVE format into the given io.Writer, and returns the number of bytes written and an error if any.

type ItemChar Uses

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

ItemChar captcha item of unicode characters

func NewItemChar Uses

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

NewItemChar creates a captcha item of characters

func (*ItemChar) BinaryEncoding Uses

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

BinaryEncoding encodes an image to PNG and returns a byte slice.

func (*ItemChar) EncodeB64string Uses

func (item *ItemChar) EncodeB64string() string

EncodeB64string encodes an image to base64 string

func (*ItemChar) WriteTo Uses

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

WriteTo writes captcha character in png format into the given io.Writer, and returns the number of bytes written and an error if any.

type ItemDigit Uses

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

ItemDigit digits captcha Struct

func NewItemDigit Uses

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

NewItemDigit create a instance of item-digit

func (*ItemDigit) EncodeB64string Uses

func (m *ItemDigit) EncodeB64string() string

EncodeB64string encodes an image to base64 string

func (*ItemDigit) EncodeBinary Uses

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

EncodeBinary encodes an image to PNG and returns a byte slice.

func (*ItemDigit) WriteTo Uses

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

WriteTo writes captcha character in png format into the given io.Writer, and returns the number of bytes written and an error if any.

type Store Uses

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 Uses

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

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 Uses

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

StoreSyncMap use sync.Map as store

func NewStoreSyncMap Uses

func NewStoreSyncMap(liveTime time.Duration) *StoreSyncMap

NewStoreSyncMap new a instance

func (StoreSyncMap) Get Uses

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

Set a string value

func (StoreSyncMap) Set Uses

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

Get get a string value

func (StoreSyncMap) Verify Uses

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

Verify check a string value

Directories

PathSynopsis
_examplesexample of HTTP server that uses the captcha package.

Package base64Captcha imports 26 packages (graph) and is imported by 20 packages. Updated 2020-09-11. Refresh now. Tools for package owners.