代码拉取完成,页面将自动刷新
package z1captcha
import (
"fmt"
"image/color"
"strings"
"time"
"unicode/utf8"
"gitee.com/z1gotool/z1crypto"
"gitee.com/z1gotool/z1err"
"github.com/mojocn/base64Captcha"
)
type CaptchaOpt struct {
Length int // 验证码长度
Timeout int64 // 验证码超时时间
Source string // 验证码中可能出现的字符
Password string // 验证码密码
TimeoutPeriodSteps int64 // 验证码超时时间周期分步
TimeoutPeriodPreStep int64 // 验证码超时时间周期前面第几部
// Height png height in pixel.
Height int
// Width Captcha png width in pixel.
Width int
//NoiseCount text noise count.
NoiseCount int
//ShowLineOptions := OptionShowHollowLine | OptionShowSlimeLine | OptionShowSineLine .
ShowLineOptions int
//BgColor captcha image background color (optional)
BgColor *color.RGBA
}
func Generate(opt ...CaptchaOpt) (text, b64s string) {
optTmp := GetCaptchaOpt(opt...)
text = GenerateText(optTmp)
if `DrawCaptcha` != `` {
driver := base64Captcha.NewDriverString(
optTmp.Height,
optTmp.Width,
optTmp.NoiseCount,
optTmp.ShowLineOptions,
optTmp.Length,
// `0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`,
optTmp.Source,
// &color.RGBA{R: 255, G: 255, B: 255, A: 255},
optTmp.BgColor,
[]string{},
)
item, err := driver.ConvertFonts().DrawCaptcha(text)
z1err.Check(err)
b64s = item.EncodeB64string()
// log.Println(`--------b64s-------`, b64s)
}
return
}
func GenerateText(opt ...CaptchaOpt) (ret string) {
optTmp := GetCaptchaOpt(opt...)
length := optTmp.Length
timeout := optTmp.Timeout / optTmp.TimeoutPeriodSteps
if timeout == 0 {
timeout = 1
}
password := optTmp.Password
source := optTmp.Source
timeoutPeriodPreStep := optTmp.TimeoutPeriodPreStep
// source := `1a张三李四王五田七`
// length := int64(4)
// timeout := int64(3)
// password := `myzero1`
// source := `0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`
// // source := `1a张三李四王五田七`
expirationPeriod := time.Now().Unix() / timeout
expirationPeriod = expirationPeriod - timeoutPeriodPreStep
sourceLenth := utf8.RuneCountInString(source)
chars := strings.Split(source, "")
md51 := z1crypto.MD5Encode(fmt.Sprintf(`%v%v`, expirationPeriod, password))
md52 := z1crypto.MD5Encode(fmt.Sprintf(`%v%v1`, expirationPeriod, password))
baseStr := md51 + md52
captchaChart := ``
for i := 0; i < length; i++ {
start := 4 * i
end := 4 * (i + 1)
tmp := baseStr[start:end]
charCount := 0
for i := 0; i < len(tmp); i++ {
charCount = charCount + int(tmp[i])
}
index := charCount % sourceLenth
chart := chars[index]
captchaChart = captchaChart + chart
}
// log.Println(`-----expirationPeriod----------`, expirationPeriod)
// log.Println(`-----captchaChart----1------`, captchaChart, md51)
ret = captchaChart
return
}
func Verify(captcha string, opt ...CaptchaOpt) (ret bool) {
optTmp := GetCaptchaOpt(opt...)
for i := int64(0); i < optTmp.TimeoutPeriodSteps; i++ {
optTmp.TimeoutPeriodPreStep = i
if captcha == GenerateText(optTmp) {
ret = true
return
}
}
return
}
func GetCaptchaOpt(opt ...CaptchaOpt) (optRet CaptchaOpt) {
optTmp := CaptchaOpt{
Length: 6,
Timeout: 60,
Source: `0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`,
// Source: `1a张三李四王五田七`,
Password: `567myzero1@#$`,
TimeoutPeriodSteps: 5,
Height: 60,
Width: 180,
NoiseCount: 0,
ShowLineOptions: 14,
BgColor: &color.RGBA{R: 255, G: 255, B: 255, A: 255},
}
if len(opt) > 0 {
if opt[0].Length == 0 {
opt[0].Length = optTmp.Length
if opt[0].Length > 8 {
opt[0].Length = 8
}
}
if opt[0].Timeout == 0 {
opt[0].Timeout = optTmp.Timeout
}
if opt[0].TimeoutPeriodSteps == 0 {
opt[0].TimeoutPeriodSteps = optTmp.TimeoutPeriodSteps
}
if opt[0].Source == `` {
opt[0].Source = optTmp.Source
}
if opt[0].Password == `` {
opt[0].Password = optTmp.Password
}
if opt[0].Height == 0 {
opt[0].Height = optTmp.Height
}
if opt[0].Width == 0 {
opt[0].Width = optTmp.Width
}
if opt[0].NoiseCount == 0 {
opt[0].NoiseCount = optTmp.NoiseCount
}
if opt[0].ShowLineOptions == 0 {
opt[0].ShowLineOptions = optTmp.ShowLineOptions
}
if opt[0].BgColor == nil {
opt[0].BgColor = optTmp.BgColor
}
optTmp = opt[0]
}
optRet = optTmp
return
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。