captcha

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2025 License: MIT Imports: 9 Imported by: 0

README ΒΆ

go captcha

🧩 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

πŸ“¦ Project Structure

gocaptcha/
β”œβ”€β”€ captcha.go
β”œβ”€β”€ config.go
β”œβ”€β”€ store.go
β”œβ”€β”€ driver/
β”‚   β”œβ”€β”€ memorystore/
β”‚   └── redisstore/
β”œβ”€β”€ internal/
β”‚   β”œβ”€β”€ image/
β”‚   β”œβ”€β”€ random/
β”‚   └── assets/
β”‚       └── fonts/
β”‚           └── DejaVuSans-Bold.ttf
└── examples/

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

func New ΒΆ

func New(secret string, opts ...Option) *captcha

Defaut config

Width:     200
Height:    80
Length:    6
Expire:    60 //seconds
FontSize:  38
Store:     memorystore.New()

Use New(secret string, opts ...Option) with option to manual config

Types ΒΆ

type Config ΒΆ

type Config struct {
	SecretKey string
	Width     int
	Height    int
	Length    int
	Expire    int
	FontSize  float64
	Store     Store
}

type Option ΒΆ

type Option func(*Config)

func WithExpire ΒΆ

func WithExpire(seconds int) Option

Expire the captcha code.

Expire defines the CAPTCHA expiration time in seconds.

func WithFontSize ΒΆ

func WithFontSize(fontSize float64) Option

FontSize defines the font size used to render the CAPTCHA text in pixels.

func WithLength ΒΆ

func WithLength(n int) Option

Length defines the number of characters in the CAPTCHA code.

func WithSize ΒΆ

func WithSize(width, height int) Option

Width and Height define the image size of the generated CAPTCHA in pixels.

func WithStore ΒΆ

func WithStore(store Store) Option

Available: memorystore or redisstore.

To create a custom driver, implement the `Store` interface to define your own storage backend for CAPTCHA data.

type Store ΒΆ

type Store interface {
	Set(key string, value string, ttl time.Duration) error
	Get(key string) (string, error)
	Delete(key string) error
}

Store defines the interface for CAPTCHA data storage. To create a custom driver, implement this interface.

Directories ΒΆ

Path Synopsis
driver
examples
basic command
redis command
internal

Jump to

Keyboard shortcuts

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