🧩 go captcha is a simple, lightweight library for generating and verifying image-based CAPTCHA codes written in Go.
- 🖼️ Generate CAPTCHA images with customizable size and font
- 🔐 Built-in encryption for secure token handling
- 🧠 Optional cache drivers: in-memory or Redis
- ⚙️ Flexible configuration using options pattern
- 💨 Zero external dependencies (except
freetypeandgo-redis/v9)
go get github.com/kal72/go-captcha@latestpackage main
import (
"fmt"
"github.com/kal72/go-captcha"
)
func main() {
cap := captcha.New("secret-key")
base64Image, text, token, err := cap.Generate()
if err != nil {
panic(err)
}
fmt.Println("text: " + text)
fmt.Println("token: " + token)
fmt.Println("image: " + base64Image)
// Verify
_, err = cap.Verify(text, token)
if err != nil {
panic("verify failed: " + err.Error())
} else {
fmt.Println("verify success ")
}
}import (
"github.com/kal72/go-captcha"
"github.com/kal72/go-captcha/driver/redisstore"
)
cap := captcha.New("secret-key",
captcha.WithStore(redisstore.New(redisstore.Config{
Addr: "localhost:6379",
Password: "pass",
DB: 0,
Prefix: "captcha",
})),
)Example output image generated:
| Option | Description |
|---|---|
WithLength(n int) |
Number of characters in the CAPTCHA code |
WithSize(width, height int) |
CAPTCHA image size in pixels |
WithFontSize(size int) |
Font size in pixels |
WithExpire(seconds int) |
Expiration time in seconds |
WithStore(store Store) |
Use custom store driver (memory, Redis, or your own) |
To create a custom driver, implement the Store interface:
type Store interface {
Set(key, value string, ttl time.Duration) error
Get(key string) (string, error)
Delete(key string) error
}Then register it via:
captcha.WithStore(myCustomDriver)See full runnable examples in the examples/ folder:
examples/
├── basic/
├── redis/
Run an example:
go run ./examples/basic/main.gogocaptcha/
├── captcha.go
├── config.go
├── store.go
├── driver/
│ ├── memorystore/
│ └── redisstore/
├── internal/
│ ├── image/
│ ├── random/
│ └── assets/
│ └── fonts/
│ └── DejaVuSans-Bold.ttf
└── examples/
