Skip to content

go captcha is a simple, lightweight library for generating and validating image-based CAPTCHA codes.

License

Notifications You must be signed in to change notification settings

kal72/go-captcha

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go captcha

gocaptcha logo

Go Reference Go Report Card License: MIT GitHub release (latest by date)

🧩 go captcha is a simple, lightweight library for generating and verifying image-based CAPTCHA codes written in Go.


✨ Features

  • 🖼️ 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 freetype and go-redis/v9)

🚀 Installation

go get github.com/kal72/go-captcha@latest

🧱 Usage Example

Basic Example

package 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 ")
    }
}

Using Redis Driver

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 CAPTCHA Image

Example output image generated:

CAPTCHA Example


⚙️ Configuration Options

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)

🧩 Custom Driver

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)

🖋️ Example Directory

See full runnable examples in the examples/ folder:

examples/
 ├── basic/
 ├── redis/

Run an example:

go run ./examples/basic/main.go

📦 Project Structure

gocaptcha/
├── captcha.go
├── config.go
├── store.go
├── driver/
│   ├── memorystore/
│   └── redisstore/
├── internal/
│   ├── image/
│   ├── random/
│   └── assets/
│       └── fonts/
│           └── DejaVuSans-Bold.ttf
└── examples/

About

go captcha is a simple, lightweight library for generating and validating image-based CAPTCHA codes.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages