func main() { flag.Parse() fname := flag.Arg(0) if fname == "" { usage() os.Exit(1) } f, err := os.Create(fname) if err != nil { log.Fatalf("%s", err) } defer f.Close() var w io.WriterTo d := captcha.RandomDigits(*flagLen) switch { case *flagAudio: w = captcha.NewAudio(d, *flagLang) case *flagImage: w = captcha.NewImage(d, *flagImgW, *flagImgH) } _, err = w.WriteTo(f) if err != nil { log.Fatalf("%s", err) } fmt.Println(d) }
func NewCaptcha(db *periwinkle.Tx) *Captcha { o := Captcha{ ID: captcha.New(), Value: string(captcha.RandomDigits(defaultLen)), } if err := db.Create(&o).Error; err != nil { dbError(err) } return &o }
// Generate png filename and encode it. The encoded code will // will save on html and will by post request decoded, that // use to identify, if the user have enter the right captcha code. func Create() (string, string) { image := uniuri.NewLen(25) secret := captcha.RandomDigits(7) c := redis.Get() if _, err := c.Do("SET", image, secret); err != nil { panic(err.Error()) } if _, err := c.Do("EXPIRE", image, expired); err != nil { panic(err.Error()) } return image, base64.StdEncoding.EncodeToString([]byte(image)) }
func GetImageCaptcha(c *gin.Context) { session := sessions.Default(c) captchaId := captcha.New() bytes := captcha.RandomDigits(4) captcheString := util.GetCaptchaString(bytes) session.Set(constant.LOGIN_CAPTCHA, captcheString) Logger.Debug("new captcha:%v", captcheString) session.Save() image := captcha.NewImage(captchaId, bytes, captcha.StdWidth/2, captcha.StdHeight/2) _, err := image.WriteTo(c.Writer) if err != nil { Logger.Error("error occured when writing captcha image to response") } }
func gen(fname string) (Id string) { f, err := os.Create(fname) if err != nil { log.Fatalf("%s", err) } defer f.Close() var w io.WriterTo d := captcha.RandomDigits(6) // six digits is enough w = captcha.NewImage(d, captcha.StdWidth, captcha.StdHeight) _, err = w.WriteTo(f) if err != nil { log.Fatalf("%s", err) } log.Println("DEBUG Captcha: Created new image ", d) str := trans(d) return str }
// Request new image, if the previous captcha is difficult to recognize. func (rcv *controller) changeImage(image string) { c := redis.Get() // Configure out, if the image still available. _, err := goredis.Bytes(c.Do("GET", image)) if err != nil { http.NotFound(rcv.response, rcv.request) return } secret := captcha.RandomDigits(7) if _, err := c.Do("SET", image, secret); err != nil { panic(err.Error()) } if _, err := c.Do("EXPIRE", image, expired); err != nil { panic(err.Error()) } png.Encode(rcv.response, captcha.NewImage(image, secret, captcha.StdWidth, captcha.StdHeight)) }