31 Star 127 Fork 43

ShirDon-廖显东 / go验证码合集包

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
captcha.go 4.33 KB
一键复制 编辑 原始数据 按行查看 历史
shirdon 提交于 2019-07-07 16:16 . commit
//++++++++++++++++++++++++++++++++++++++++
//Fighting for great,share generate value!
//Build the best soft by golang,let's go!
//++++++++++++++++++++++++++++++++++++++++
//Author:ShirDon <http://www.shirdon.com>
//Email:hcbsts@163.com; 823923263@qq.com
//++++++++++++++++++++++++++++++++++++++++
package captchas_with_go
import (
"errors"
"image"
"strings"
"time"
)
//Captcha is the core captcha struct
type Captcha struct {
store StoreInterface
wordManager *WordManager
filterManager *ImageFilterManager
captchaConfig *CaptchaConfig
imageConfig *ImageConfig
filterConfig *FilterConfig
storeConfig *StoreConfig
}
//CreateCaptcha is a method to create new Captcha struct
func CreateCaptcha(wordManager *WordManager, captchaConfig *CaptchaConfig, imageConfig *ImageConfig, filterConfig *FilterConfig, storeConfig *StoreConfig) (*Captcha, error) {
var retErr error
captcha := new(Captcha)
store, err := createStore(storeConfig)
if nil == err {
captcha.store = store
} else {
retErr = err
}
if nil != wordManager {
captcha.wordManager = wordManager
} else {
retErr = errors.New("CreateCaptcha fail:invalid wordManager")
}
captcha.captchaConfig = captchaConfig
captcha.imageConfig = imageConfig
captcha.filterConfig = filterConfig
captcha.filterManager = CreateImageFilterManagerByConfig(filterConfig)
return captcha, retErr
}
//CreateCaptchaFromConfigFile is a method to create new Captcha struct
func CreateCaptchaFromConfigFile(configFile string) (*Captcha, error) {
var captcha *Captcha
var retErr error
err, wordDict, captchaConfig, imageConfig, filterConfig, storeConfig := loadConfigFromFile(configFile)
if nil == err {
wordmgr, err := CreateWordManagerFromDataFile(wordDict)
if nil == err {
captcha, retErr = CreateCaptcha(wordmgr, captchaConfig, imageConfig, filterConfig, storeConfig)
} else {
retErr = err
}
} else {
retErr = err
}
return captcha, retErr
}
//GetKey will generate a key with required length
func (captcha *Captcha) GetKey(length int) (string, error) {
var retErr error
var rst string
text, err := captcha.wordManager.Get(length)
if nil != err {
retErr = err
} else {
info := new(CaptchaInfo)
info.Text = text
info.CreateTime = time.Now()
info.ShownTimes = 0
rst = captcha.store.Add(info)
}
return rst, retErr
}
//Verify will verify the user's input and the server stored captcha text
func (captcha *Captcha) Verify(key, textToVerify string) (bool, string) {
info := captcha.store.Get(key)
if nil == info {
return false, "captcha info not found"
}
if info.CreateTime.Add(captcha.captchaConfig.LifeTime).Before(time.Now()) {
return false, "captcha expires"
}
verified := false
if captcha.captchaConfig.CaseSensitive {
verified = info.Text == textToVerify
} else {
verified = strings.ToLower(info.Text) == strings.ToLower(textToVerify)
}
if !verified {
return false, "captcha text not match"
}
captcha.store.Del(key)
return true, ""
}
//GetImage will generate the binary image data
func (captcha *Captcha) GetImage(key string) (image.Image, error) {
info := captcha.store.Get(key)
if nil == info {
return nil, errors.New("captcha info not found")
}
if info.CreateTime.Add(captcha.captchaConfig.LifeTime).Before(time.Now()) {
return nil, errors.New("captcha expires")
}
if captcha.captchaConfig.ChangeTextOnRefresh {
if info.ShownTimes > 0 {
text, err := captcha.wordManager.Get(len(info.Text))
if nil != err {
return nil, err
} else {
info.Text = text
}
}
info.ShownTimes++
captcha.store.Update(key, info)
}
cimg := captcha.genImage(info.Text)
return cimg, nil
}
func createStore(config *StoreConfig) (StoreInterface, error) {
var store StoreInterface
var err error
switch config.Engine {
case STORE_ENGINE_BUILDIN:
store = CreateCStore(config.LifeTime, config.GcProbability, config.GcDivisor)
break
case STORE_ENGINE_MEMCACHE:
store = CreateMCStore(config.LifeTime, config.Servers)
break
default:
creator, has := storeCreators[config.Engine]
if !has {
err = errors.New("Not supported engine:'" + config.Engine + "'")
break
}
store, err = creator(config)
}
return store, err
}
func (captcha *Captcha) genImage(text string) *CImage {
cimg := CreateCImage(captcha.imageConfig)
cimg.drawString(text)
for _, filter := range captcha.filterManager.GetFilters() {
filter.Proc(cimg)
}
return cimg
}
Go
1
https://gitee.com/shirdonl/captchas_with_go.git
git@gitee.com:shirdonl/captchas_with_go.git
shirdonl
captchas_with_go
go验证码合集包
master

搜索帮助