all repos — captcha @ 5dd14861570e98336fbbade6f45b85645e8aca28

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

cmd/main.go (view raw)

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