all repos — captcha @ d6bfb3fd16f213d34e00e2e546c922172873b076

Go package captcha implements generation and verification of image and audio CAPTCHAs.

capgen/main.go (view raw)

 1// capgen is an utility to test captcha generation.
 2package main
 3
 4import (
 5	"flag"
 6	"fmt"
 7	"github.com/dchest/captcha"
 8	"io"
 9	"log"
10	"os"
11)
12
13var (
14	flagImage = flag.Bool("i", true, "output image captcha")
15	flagAudio = flag.Bool("a", false, "output audio captcha")
16	flagLen   = flag.Int("len", captcha.DefaultLen, "length of captcha")
17	flagImgW  = flag.Int("width", captcha.StdWidth, "image captcha width")
18	flagImgH  = flag.Int("height", captcha.StdHeight, "image captcha height")
19)
20
21func usage() {
22	fmt.Fprintf(os.Stderr, "usage: captcha [flags] filename\n")
23	flag.PrintDefaults()
24}
25
26func main() {
27	flag.Parse()
28	fname := flag.Arg(0)
29	if fname == "" {
30		usage()
31		os.Exit(1)
32	}
33	f, err := os.Create(fname)
34	if err != nil {
35		log.Fatalf("%s", err)
36	}
37	defer f.Close()
38	var w io.WriterTo
39	d := captcha.RandomDigits(*flagLen)
40	switch {
41	case *flagAudio:
42		w = captcha.NewAudio(d)
43	case *flagImage:
44		w = captcha.NewImage(d, *flagImgW, *flagImgH)
45	}
46	_, err = w.WriteTo(f)
47	if err != nil {
48		log.Fatalf("%s", err)
49	}
50	fmt.Println(d)
51}