go 驗證碼實現

依賴

"github.com/mojocn/base64Captcha"

 官方預覽

實現

import (
	cp "github.com/mojocn/base64Captcha"
	"image/color"
)

type captcha struct {
	c *cp.Captcha
}

//創建字符串驗證碼實例
func NewCaptcha(height, width, length int, source string) *captcha {
	driver := cp.NewDriverString(height, width, 0, cp.OptionShowHollowLine,
		length, source, &color.RGBA{0, 0, 0, 0}, []string{"Flim-Flam.ttf"})
	cape := cp.NewCaptcha(driver, cp.DefaultMemStore)
	return &captcha{cape}
}

//驗證是否有效
func (r *captcha) Verify(id, answer string) bool {
	get := cp.DefaultMemStore.Get(id, false)
	if get == "" {
		return false
	}
	return r.c.Verify(id, answer, true)
}

//生成base64
func (r *captcha) Generate() (id, b64s string, err error) {
	return r.c.Generate()
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章