driver

package
v1.3.9 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2025 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MimeTypeImage output base64 mine-type.
	MimeTypeImage = "image/png"

	// MimeTypeAudio output base64 mine-type.
	MimeTypeAudio = "audio/wav"

	// OptionShowHollowLine shows hollow line
	OptionShowHollowLine = 2
	// OptionShowSlimeLine shows slime line
	OptionShowSlimeLine = 4
	// OptionShowSineLine shows sine line
	OptionShowSineLine = 8
)
View Source
const (
	StringLength = 20
	TxtNumbers   = "012346789"
	TxtAlphabet  = "ABCDEFGHJKMNOQRSTUVXYZabcdefghjkmnoqrstuvxyz"
)

Variables

View Source
var DefaultDriverAudio = &DriverAudio{
	Length:   6,
	Language: "en",
}

DefaultDriverAudio is a default audio driver

View Source
var DefaultDriverChinese = &DriverChinese{
	Width:           100,
	Height:          32,
	Length:          6,
	ShowLineOptions: 0,
	NoiseCount:      0,
	Source:          "县果栋容他锹射纳堤洲冶架缓飞善挑捏绒既寨剧缝辆语愉谱鸟详坛饶碰扛笔试晶巴呀塘有谣辜确丝活将宪染淋范殖",
	Fonts:           []string{"wqy-microhei.ttc"},
}

DefaultDriverChinese is a default chinese driver

View Source
var DefaultDriverDigit = &DriverDigit{
	Width:           100,
	Height:          32,
	Length:          6,
	ShowLineOptions: 0,
	NoiseCount:      0,
}

DefaultDriverDigit is a default digit driver

View Source
var DefaultDriverLetter = &DriverLetter{
	Width:           100,
	Height:          32,
	Length:          6,
	ShowLineOptions: 1,
	NoiseCount:      0,
	Source:          TxtAlphabet,
}

DefaultDriverLetter is a default letter driver

View Source
var DefaultDriverMath = &DriverMath{
	Width:           100,
	Height:          32,
	ShowLineOptions: 0,
	NoiseCount:      0,
}

DefaultDriverMath is a default math driver

View Source
var DefaultDriverString = &DriverString{
	Width:           100,
	Height:          32,
	Length:          6,
	ShowLineOptions: 0,
	NoiseCount:      0,
	Source:          TxtNumbers + TxtAlphabet,
}

DefaultDriverString is a default string driver

Functions

func RandomBytes

func RandomBytes(length int) (b []byte)

RandomBytes returns a byte slice of the given length read from CSPRNG.

func RandomColor

func RandomColor() color.RGBA

RandomColor returns a random color.RGBA.

func RandomDigits

func RandomDigits(length int) []byte

RandomDigits returns a byte slice of the given length, where each byte is a

func RandomInt

func RandomInt(n int) int

RandomInt returns a random integer in range [0, n)]

func RandomRange

func RandomRange[T int | float64](from, to T) T

RandomRange returns a random number in range [from, to).]

func RandomString

func RandomString() string

RandomString returns a random string of given size.

func RandomText

func RandomText(size int, sourceChars string) string

RandomText returns a random string of given size.

Types

type Driver

type Driver interface {
	// DrawCaptcha draws binary item
	DrawCaptcha(content string) (item Item, err error)
	// GenerateCaptcha creates rand id, content and answer
	GenerateCaptcha() (id, q, a string)
}

Driver captcha interface for captcha engine 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(d DriverAudio) *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) GenerateCaptcha

func (d *DriverAudio) GenerateCaptcha() (id, q, a string)

GenerateCaptcha creates id,captcha content and answer

type DriverChinese

type DriverChinese struct {
	// Width Captcha png width in pixel.
	Width int

	// Height png height in pixel.
	Height int

	// Length random string length.
	Length int

	// NoiseCount text noise count.
	NoiseCount int

	// ShowLineOptions := OptionShowHollowLine | OptionShowSlimeLine | OptionShowSineLine .
	ShowLineOptions 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 config for chinese driver.

func NewDriverChinese

func NewDriverChinese(d DriverChinese) *DriverChinese

NewDriverChinese creates a driver of Chinese characters

func (*DriverChinese) DrawCaptcha

func (d *DriverChinese) DrawCaptcha(content string) (item Item, err error)

DrawCaptcha draws captcha item for chinese driver.

func (*DriverChinese) GenerateCaptcha

func (d *DriverChinese) GenerateCaptcha() (id, content, answer string)

GenerateCaptcha generates captcha id, content and answer for chinese driver.

type DriverDigit

type DriverDigit struct {
	// Width Captcha png width in pixel.
	Width int

	// Height png height in pixel.
	Height int

	// Length random string length.
	Length int

	// NoiseCount text noise count.
	NoiseCount int

	// ShowLineOptions := OptionShowHollowLine | OptionShowSlimeLine | OptionShowSineLine .
	ShowLineOptions int

	// Source is a Unicode which is the rand string from.
	Source string

	// BgColor captcha image background color (optional)
	BgColor *color.RGBA
}

DriverDigit config for digit driver.

func NewDriverDigit

func NewDriverDigit(d DriverDigit) *DriverDigit

NewDriverDigit creates a digit driver.

func (*DriverDigit) DrawCaptcha

func (d *DriverDigit) DrawCaptcha(content string) (item Item, err error)

DrawCaptcha draws captcha item for digit driver.

func (*DriverDigit) GenerateCaptcha

func (d *DriverDigit) GenerateCaptcha() (id, content, answer string)

GenerateCaptcha generates id, content and answer for digit driver.

type DriverLetter added in v1.3.9

type DriverLetter struct {
	// Width Captcha png width in pixel.
	Width int

	// Height png height in pixel.
	Height int

	// Length random string length.
	Length int

	// NoiseCount text noise count.
	NoiseCount int

	// ShowLineOptions := OptionShowHollowLine | OptionShowSlimeLine | OptionShowSineLine .
	ShowLineOptions int

	// Source is a Unicode which is the rand string from.
	Source string

	// BgColor captcha image background color (optional)
	BgColor *color.RGBA
}

DriverLetter config for letter driver.

func NewDriverLetter added in v1.3.9

func NewDriverLetter(d DriverLetter) *DriverLetter

NewDriverLetter creates a letter driver.

func (*DriverLetter) DrawCaptcha added in v1.3.9

func (d *DriverLetter) DrawCaptcha(content string) (item Item, err error)

DrawCaptcha draws captcha item for letter driver

func (*DriverLetter) GenerateCaptcha added in v1.3.9

func (d *DriverLetter) GenerateCaptcha() (id, content, answer string)

GenerateCaptcha generates id, content and answer for letter driver.

type DriverMath

type DriverMath struct {
	// Width Captcha png width in pixel.
	Width int

	// Height png height in pixel.
	Height 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 config for math driver.

func NewDriverMath

func NewDriverMath(d DriverMath) *DriverMath

NewDriverMath creates a math driver.

func (*DriverMath) DrawCaptcha

func (d *DriverMath) DrawCaptcha(question string) (item Item, err error)

DrawCaptcha draws captcha item for math driver.

func (*DriverMath) GenerateCaptcha

func (d *DriverMath) GenerateCaptcha() (id, question, answer string)

GenerateCaptcha generates id, content and answer for math captcha.

type DriverString

type DriverString struct {
	// Width Captcha png width in pixel.
	Width int
	// Height png height in pixel.
	Height int

	// Length random string length.
	Length int

	// NoiseCount text noise count.
	NoiseCount int

	// ShowLineOptions := OptionShowHollowLine | OptionShowSlimeLine | OptionShowSineLine .
	ShowLineOptions 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
}

DriverString config for string driver.

func NewDriverString

func NewDriverString(d DriverString) *DriverString

NewDriverString creates a string driver.

func (*DriverString) DrawCaptcha

func (d *DriverString) DrawCaptcha(content string) (item Item, err error)

DrawCaptcha draws captcha item for string driver.

func (*DriverString) GenerateCaptcha

func (d *DriverString) GenerateCaptcha() (id, content, answer string)

GenerateCaptcha generates id,content and answer for string driver.

type Fonts added in v1.3.9

type Fonts struct {
	Name []string
	Data []*truetype.Font
}

type Item

type Item interface {
	// Writer writes to a writer
	Writer(w io.Writer) (n int64, err error)
	// Encoder encodes as base64 string
	Encoder() string
}

Item is captcha item interface

type ItemAudio

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

ItemAudio captcha-audio-engine return type.

func (*ItemAudio) Encoder

func (a *ItemAudio) Encoder() string

Encoder encodes a sound to base64 string

func (*ItemAudio) Writer

func (a *ItemAudio) Writer(w io.Writer) (n int64, err error)

Writer 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) Encoder

func (item *ItemChar) Encoder() string

Encoder encodes an image to base64 string

func (*ItemChar) Writer

func (item *ItemChar) Writer(w io.Writer) (int64, error)

Writer writes captcha character in png format into the given io.Writer, and returns the number of bytes written and an error if any.

Jump to

Keyboard shortcuts

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