GO图形验证码

发布日期: 2022-05-10 11:50:52 作者: Stephen 评论: 0

base64captcha是基于Go的图形验证码插件,支持任意Unicode字符,并且可以轻松自定义以支持数学、中文韩文、日文、俄语、阿拉伯语等。

安装

go get -u github.com/mojocn/base64Captcha

使用

生成

// 设置自带的 store(可以配置成redis)
var store = base64Captcha.DefaultMemStore

type CaptchaResponse struct {
    Id     string
    Encode string
}

func Captcha() *CaptchaResponse {
    // 配置验证码的参数
    driverString := base64Captcha.DriverString{
        Height:          40,
        Width:           100,
        NoiseCount:      0,
        ShowLineOptions: 2 | 4,
        Length:          8,
        Source:          "1234567890qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM",
        BgColor:         &color.RGBA{R: 3, G: 102, B: 214, A: 125},
        Fonts:           []string{"wqy-microhei.ttc"},
    }
    // ConvertFonts 按名称加载字体
    driver := driverString.ConvertFonts()
    // 创建 Captcha
    captcha := base64Captcha.NewCaptcha(driver, store)
    // Generate 生成随机 id、base64 图像字符串
    id, b64s, err := captcha.Generate()
    if err != nil {
        return nil
    }
    return &CaptchaResponse{
        Id:     id,
        Encode: b64s,
    }
}

验证

func VerifyCaptcha(id string, VerifyValue string) bool {
    if store.Verify(id, VerifyValue, true) {
        //验证成功
        return true
    }
    return false
}

示例

{
    "Id": "yq43FE70VFZsaZxXzf9e",
    "Encode": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAoCAYAAAAIeF9DAAANP0lEQVR4nOybCXQUx5nHvz6m574kje5rhCRkjCwMwmBJtqzYwnaAdSQ7ITYOcUyMwdnNUzBs7HixHUIOWBzFdgI+8lg2SwKBXZblMNfKgtgIDBEgC10IdCMJzaXR3NNXXo/cw2guaaTxBfzfmzfdPVVffV2//qq/qpJw4uIH6+C2AlSfOX99qN/mdJ15daJ2yr67Oaid2l1rgtrAJ2r4VlE0QISCAGFA8AoAwjsUyVNws2gqMKYCwVchI+RWAhMMxETuO1oQfDXukMU5e7NCmQyIzwOCr5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