Ejemplo n.º 1
0
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)
}
Ejemplo n.º 2
0
func (this *Captcha) GetImage(width int, height int) ([]byte, error) {
	buffer := bytes.NewBuffer(nil)
	img := captcha.NewImage("", this.data, width, height)
	_, err := img.WriteTo(buffer)
	if err != nil {
		return nil, err
	}

	return buffer.Bytes(), nil
}
Ejemplo n.º 3
0
// Will serve image, when user does not click reload
// yet.
func (rcv *controller) createNewImage(image string) error {

	c := redis.Get()
	secret, err := goredis.Bytes(c.Do("GET", image))
	if err != nil {
		return err
	}
	png.Encode(rcv.response, captcha.NewImage(image, secret, captcha.StdWidth, captcha.StdHeight))
	return nil
}
Ejemplo n.º 4
0
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")
	}
}
Ejemplo n.º 5
0
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
}
Ejemplo n.º 6
0
// 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))
}