Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
B

base64Captcha

> 编程语言
Open source

captcha of base64 image string

2.4K stars0 likes1 views
WebsiteGitHub

About

captcha of base64 image string

A flexible and various captcha package

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

1. Doc & Demo

  • English
  • 中文文档
  • Playground

2. Quick start

2.1 Use history version

Tag v1.2.2

go get github.com/mojocn/[email protected]

or edit your go.mod file to

github.com/mojocn/[email protected]

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

  • Build-in Memory Store
go
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
go
// 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.

…

2.3.4 ‍Generate Base64(image/audio) string

go
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

go
//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

…

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

  • dchest/captha
  • @slayercat
  • @amzyang
  • @Luckyboys
  • @hi-sb

5. Licence

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

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Goanti-spamarithmetic-captchaaudio-captchasbase64

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言