Golang库-base64图片验证码库base64Captcha
原创...
摘要
Base64captcha是一个golang base64图片验证码生成库。Base64captcha支持任何 unicode 字符,并可轻松定制为支持数学、中文、韩文、日文、俄文、阿拉伯文等。
Golang库-base64图片验证码库base64Captcha
介绍
web开发中,我们经常用到验证码。Base64captcha是一个golang base64图片验证码生成库,很方便我们直接使用,提高开发效率。
地址: https://github.com/mojocn/base64Captcha
使用
环境 go1.22.0(go>1.11)
安装
go get -u github.com/mojocn/base64Captcha
例子
package main
import (
"fmt"
"image/color"
"time"
"github.com/mojocn/base64Captcha"
)
// mathConfig 生成图形化算术验证码配置
func mathConfig() *base64Captcha.DriverMath {
mathType := &base64Captcha.DriverMath{
// 设置图片的宽度、长度等属性
Height: 50,
Width: 100,
NoiseCount: 0,
ShowLineOptions: base64Captcha.OptionShowHollowLine,
BgColor: &color.RGBA{
R: 40,
G: 30,
B: 89,
A: 29,
},
Fonts: nil,
}
return mathType
}
// digitConfig 生成图形化数字验证码配置
func digitConfig() *base64Captcha.DriverDigit {
digitType := &base64Captcha.DriverDigit{
Height: 50,
Width: 100,
Length: 5,
MaxSkew: 0.45,
DotCount: 80,
}
return digitType
}
// stringConfig 生成图形化字符串验证码配置
func stringConfig() *base64Captcha.DriverString {
stringType := &base64Captcha.DriverString{
Height: 50,
Width: 100,
NoiseCount: 0,
ShowLineOptions: base64Captcha.OptionShowHollowLine | base64Captcha.OptionShowSlimeLine,
Length: 5,
Source: "123456789qwertyuiopasdfghjklzxcvb",
BgColor: &color.RGBA{
R: 40,
G: 30,
B: 89,
A: 29,
},
Fonts: nil,
}
return stringType
}
// chineseConfig 生成图形化汉字验证码配置
func chineseConfig() *base64Captcha.DriverChinese {
chineseType := &base64Captcha.DriverChinese{
Height: 50,
Width: 100,
NoiseCount: 0,
ShowLineOptions: base64Captcha.OptionShowSlimeLine,
Length: 2,
Source: "设想,你在,处理,消费者,的音,频输,出音,频可,能无,论什,么都,没有,任何,输出,或者,它可,能是,单声道,立体声,或是,环绕立,体声的,不想要,的值",
BgColor: &color.RGBA{
R: 40,
G: 30,
B: 89,
A: 29,
},
Fonts: nil,
}
return chineseType
}
// autoConfig 生成图形化数字音频验证码配置
func autoConfig() *base64Captcha.DriverAudio {
chineseType := &base64Captcha.DriverAudio{
Length: 4,
Language: "zh",
}
return chineseType
}
// 自定义验证存储对象
// 使用DefaultMemStore 创建的对象,存储的验证码为 10240 个,过期时间为 10分钟
// var result = base64Captcha.DefaultMemStore
// 设置存储的验证码为 20240个,过期时间为 3分钟
var result = base64Captcha.NewMemoryStore(20240, 3*time.Minute)
// 生成验证码
func GenCaptcha(captcha_type string) (string, string, string, error) {
var driver base64Captcha.Driver
switch captcha_type {
case "audio":
driver = autoConfig()
case "string":
driver = stringConfig()
case "math":
driver = mathConfig()
case "chinese":
driver = chineseConfig()
case "digit":
driver = digitConfig()
}
if driver == nil {
panic("Please set Captcha Driver")
}
// 创建验证码并传入创建的类型的配置,以及存储的对象
c := base64Captcha.NewCaptcha(driver, result)
id, b64s, ans, err := c.Generate()
return id, b64s, ans, err
}
// 校验验证码
func VerifyCaptcha(id, ans string) bool {
return result.Verify(id, ans, true)
}
// 获取验证码答案
func GetCaptchaAnswer(codeId string) string {
return result.Get(codeId, false)
}
func main() {
id, b64s, ans, err := GenCaptcha("math")
if err != nil {
panic(err)
}
fmt.Printf("生成的验证码id: %v\n", id)
fmt.Printf("生成的码证码图片: %v\n", b64s)
fmt.Printf("生成的验证码答案: %v\n", ans)
fmt.Printf("获取验证码答案: %v\n", GetCaptchaAnswer(id))
fmt.Printf("校验验证码: %v\n", VerifyCaptcha(id, ans))
}
运行测试 生成的base图片可在浏览器打开
# go run main.go
生成的验证码id: NVkvoOc6caAXxUz8tVb1
生成的码证码图片: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAyCAYAAACqNX6+AAAJ+0lEQVR4nOyaeXhU1d3Hz3Lvnbmz3MlCSMRIQAxbXoiAD4GAyPLqgwjKYnFBcXsUW23VqnUptoIWH0uKR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ZKf/y298L88dCjf7bOEB/TG5cM6SVjSB8jY0gfI2NIHyNjSB8jY0gfI2NIHyNjSB/jPwEAAP//ocQIpws2PPIAAAAASUVORK5CYII=
生成的验证码答案: 45
获取验证码答案: 45
校验验证码: true
更多文章请关注我的公众号,自动推送更新