base64Captcha

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 22, 2023 License: Apache-2.0 Imports: 24 Imported by: 0

README ยถ

A flexible and various captcha package

Test Go Report Card GoDoc Build Status codecov stability-stable Foundation

Base64captcha supports any unicode character and can easily be customized to support Math Chinese Korean Japanese Russian Arabic etc.

1. ๐Ÿ“–๐Ÿ“–๐Ÿ“– Doc & Demo

2. ๐Ÿš€๐Ÿš€๐Ÿš€ Quick start

2.1 ๐ŸŽฌ๐ŸŽฌ๐ŸŽฌ Use history version

Tag v1.2.2

go get github.com/mojocn/base64Captcha@v1.2.2

or edit your go.mod file to

github.com/mojocn/base64Captcha@v1.2.2

2.2 ๐Ÿ“ฅ๐Ÿ“ฅ๐Ÿ“ฅ Download package
go get -u github.com/mojocn/base64Captcha

For Gopher from mainland China without VPN go get golang.org/x/image failure solution:

  • go version > 1.11
  • set env GOPROXY=https://goproxy.io
2.3 ๐Ÿ‚๐Ÿ‚๐Ÿ‚ How to code with base64Captcha
2.3.1 ๐Ÿ‡๐Ÿ‡๐Ÿ‡ Implement Store interface or use build-in memory 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
}

2.3.2 ๐Ÿ„๐Ÿ„๐Ÿ„ Implement Driver interface or use one of build-in drivers

There are some build-in drivers:

  1. Build-in Driver Digit
  2. Build-in Driver String
  3. Build-in Driver Math
  4. Build-in Driver Chinese
// Driver captcha interface for captcha engine to to write staff
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)
}
2.3.3 ๐Ÿšด๐Ÿšด๐Ÿšด โ€Core code captcha.go

captcha.go is the entry of base64Captcha which is quite simple.

package base64Captcha

import (
	"math/rand"
	"time"
)

func init() {
	//init rand seed
	rand.Seed(time.Now().UnixNano())
}

// Captcha captcha basic information.
type Captcha struct {
	Driver Driver
	Store  Store
}

//NewCaptcha creates a captcha instance from driver and store
func NewCaptcha(driver Driver, store Store) *Captcha {
	return &Captcha{Driver: driver, Store: store}
}

//Generate generates a random id, base64 image string or an error if any
func (c *Captcha) Generate() (id, b64s string, err error) {
	id,content, answer := c.Driver.GenerateIdQuestionAnswer()
	item, err := c.Driver.DrawCaptcha(content)
	if err != nil {
		return "", "", err
	}
	c.Store.Set(id, answer)
	b64s = item.EncodeB64string()
	return
}

//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.
func (c *Captcha) Verify(id, answer string, clear bool) (match bool) {
	match = c.Store.Get(id, clear) == answer
	return
}

2.3.4 ๐Ÿšต๐Ÿšต๐Ÿšต โ€Generate Base64(image/audio) string
func (c *Captcha) Generate() (id, b64s string, err error) {
	id,content, answer := c.Driver.GenerateIdQuestionAnswer()
	item, err := c.Driver.DrawCaptcha(content)
	if err != nil {
		return "", "", err
	}
	c.Store.Set(id, answer)
	b64s = item.EncodeB64string()
	return
}
2.3.5 ๐Ÿคธ๐Ÿคธ๐Ÿคธ Verify Answer
//if you has multiple captcha instances which shares a same store. You may want to use `store.Verify` method instead.
//Verify by given id key and remove the captcha value in store, return boolean value.
func (c *Captcha) Verify(id, answer string, clear bool) (match bool) {
	match = c.Store.Get(id, clear) == answer
	return
}
2.3.6 ๐Ÿƒ๐Ÿƒ๐Ÿƒ โ€Full Example
// example of HTTP server that uses the captcha package.
package main

import (
	"encoding/json"
	"fmt"
	"github.com/mojocn/base64Captcha"
	"log"
	"net/http"
)

//configJsonBody json request body.
type configJsonBody struct {
	Id            string
	CaptchaType   string
	VerifyValue   string
	DriverAudio   *base64Captcha.DriverAudio
	DriverString  *base64Captcha.DriverString
	DriverChinese *base64Captcha.DriverChinese
	DriverMath    *base64Captcha.DriverMath
	DriverDigit   *base64Captcha.DriverDigit
}

var store = base64Captcha.DefaultMemStore

// base64Captcha create http handler
func generateCaptchaHandler(w http.ResponseWriter, r *http.Request) {
	//parse request parameters
	decoder := json.NewDecoder(r.Body)
	var param configJsonBody
	err := decoder.Decode(&param)
	if err != nil {
		log.Println(err)
	}
	defer r.Body.Close()
	var driver base64Captcha.Driver

	//create base64 encoding captcha
	switch param.CaptchaType {
	case "audio":
		driver = param.DriverAudio
	case "string":
		driver = param.DriverString.ConvertFonts()
	case "math":
		driver = param.DriverMath.ConvertFonts()
	case "chinese":
		driver = param.DriverChinese.ConvertFonts()
	default:
		driver = param.DriverDigit
	}
	c := base64Captcha.NewCaptcha(driver, store)
	id, b64s, err := c.Generate()
	body := map[string]interface{}{"code": 1, "data": b64s, "captchaId": id, "msg": "success"}
	if err != nil {
		body = map[string]interface{}{"code": 0, "msg": err.Error()}
	}
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	json.NewEncoder(w).Encode(body)
}

// base64Captcha verify http handler
func captchaVerifyHandle(w http.ResponseWriter, r *http.Request) {

	//parse request json body
	decoder := json.NewDecoder(r.Body)
	var param configJsonBody
	err := decoder.Decode(&param)
	if err != nil {
		log.Println(err)
	}
	defer r.Body.Close()
	//verify the captcha
	body := map[string]interface{}{"code": 0, "msg": "failed"}
	if store.Verify(param.Id, param.VerifyValue, true) {
		body = map[string]interface{}{"code": 1, "msg": "ok"}
	}

	//set json response
	w.Header().Set("Content-Type", "application/json; charset=utf-8")

	json.NewEncoder(w).Encode(body)
}

//start a net/http server
func main() {
	//serve Vuejs+ElementUI+Axios Web Application
	http.Handle("/", http.FileServer(http.Dir("./static")))

	//api for create captcha
	http.HandleFunc("/api/getCaptcha", generateCaptchaHandler)

	//api for verify captcha
	http.HandleFunc("/api/verifyCaptcha", captchaVerifyHandle)

	fmt.Println("Server is at :8777")
	if err := http.ListenAndServe(":8777", nil); err != nil {
		log.Fatal(err)
	}
}
2.3.7 Example Use Etcd as store

captcha with etcd database as store

3. ๐ŸŽจ๐ŸŽจ๐ŸŽจ Customization

You can customize your captcha display image by implementing interface driver and interface item.

There are some example for your reference.

  1. DriverMath
  2. DriverChinese
  3. ItemChar

You can even design the captcha struct to whatever you prefer.

4. ๐Ÿ’–๐Ÿ’–๐Ÿ’– Thanks

5. ๐Ÿญ๐Ÿญ๐Ÿญ Licence

base64Captcha source code is licensed under the Apache Licence, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0.html).

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 ยถ

View Source
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 */
)
View Source
const (
	//OptionShowHollowLine shows hollow line
	OptionShowHollowLine = 2
	//OptionShowSlimeLine shows slime line
	OptionShowSlimeLine = 4
	//OptionShowSineLine shows sine line
	OptionShowSineLine = 8
)

Variables ยถ

View Source
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)
)
View Source
var DefaultDriverAudio = NewDriverAudio(6, "en")

DefaultDriverAudio is a default audio driver

View Source
var DefaultDriverDigit = NewDriverDigit(80, 240, 5, 0.7, 80)

DefaultDriverDigit is a default driver of digit

View Source
var DefaultEmbeddedFonts = NewEmbeddedFontsStorage(defaultEmbeddedFontsFS)

Functions ยถ

func RandColor ยถ

func RandColor() color.RGBA

RandColor get random color. ็”Ÿๆˆ้šๆœบ้ขœ่‰ฒ.

func RandDeepColor ยถ

func RandDeepColor() color.RGBA

RandDeepColor get random deep color. ้šๆœบ็”Ÿๆˆๆทฑ่‰ฒ็ณป.

func RandLightColor ยถ

func RandLightColor() color.RGBA

RandLightColor get random ligth color. ้šๆœบ็”Ÿๆˆๆต…่‰ฒ.

func RandText ยถ

func RandText(size int, sourceChars string) string

RandText creates random text of given size.

func RandomId ยถ

func RandomId() string

RandomId returns a new random id key string.

Types ยถ

type Captcha ยถ

type Captcha struct {
	Driver Driver
	Store  Store
}

Captcha captcha basic information.

func NewCaptcha ยถ

func NewCaptcha(driver Driver, store Store) *Captcha

NewCaptcha creates a captcha instance from driver and store

func (*Captcha) Generate ยถ

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 ยถ

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 ยถ

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 ยถ

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

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 ยถ

func (a *ItemAudio) EncodeB64string() string

EncodeB64string encodes a sound to base64 string

func (*ItemAudio) WriteTo ยถ

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 ยถ

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

ItemChar captcha item of unicode characters

func NewItemChar ยถ

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

NewItemChar creates a captcha item of characters

func (*ItemChar) BinaryEncoding ยถ

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

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

func (*ItemChar) EncodeB64string ยถ

func (item *ItemChar) EncodeB64string() string

EncodeB64string encodes an image to base64 string

func (*ItemChar) WriteTo ยถ

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 ยถ

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

ItemDigit digits captcha Struct

func NewItemDigit ยถ

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

NewItemDigit create a instance of item-digit

func (*ItemDigit) EncodeB64string ยถ

func (m *ItemDigit) EncodeB64string() string

EncodeB64string encodes an image to base64 string

func (*ItemDigit) EncodeBinary ยถ

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

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

func (*ItemDigit) WriteTo ยถ

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 ยถ

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 ยถ

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 ยถ

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

func (StoreSyncMap) Verify ยถ

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

Verify check a string value

Directories ยถ

Path Synopsis
example of HTTP server that uses the captcha package.
example of HTTP server that uses the captcha package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL