Golang 中如何优雅的生成验证码 | 格塔里

格塔里

一个不负责任的教程博客

0%

Golang 中如何优雅的生成验证码

install

1
go get github.com/mojocn/base64Captcha@v1.2.2

demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main

import (
// "os"

"github.com/mojocn/base64Captcha"
)

func main() {
var configC = base64Captcha.ConfigCharacter{
Height: 88,
Width: 240,
// const CaptchaModeNumber:数字,CaptchaModeAlphabet:字母,CaptchaModeArithmetic:算术,CaptchaModeNumberAlphabet:数字字母混合.
Mode: base64Captcha.CaptchaModeArithmetic,
ComplexOfNoiseText: base64Captcha.CaptchaComplexMedium,
ComplexOfNoiseDot: base64Captcha.CaptchaComplexMedium,
IsUseSimpleFont: true,
IsShowHollowLine: true,
IsShowNoiseDot: true,
IsShowNoiseText: false,
IsShowSlimeLine: true,
IsShowSineLine: true,
CaptchaLen: 6,
}
// GenerateCaptcha 第一个参数为空字符串, 包会自动在服务器一个随机种子给你产生随机uiid.
idKeyC, capC := base64Captcha.GenerateCaptcha("", configC)
// 以base64编码保存为文件
base64Captcha.CaptchaWriteToFile(capC, "./", idKeyC, "png")
// 获取正确答案
verifyValue := capC.(*base64Captcha.CaptchaImageChar).VerifyValue
// 展示正确答案
// println(verifyValue)
// 删除内存键防止内存泄漏
base64Captcha.VerifyCaptchaAndIsClear(idKeyC, verifyValue, true)
// os.Remove("./" + idKeyC + "png")
}

download demo

1
2
3
curl -o base64Captcha.go https://gtary.com/script/base64Captcha.go
go get github.com/mojocn/base64Captcha@v1.2.2
go run base64Captcha.go