all repos — captcha @ b80c582b3330260a764c6fbd0f8d3e7f74164147

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

capgen/main.go (view raw)

 1// Copyright 2011 Dmitry Chestnykh. All rights reserved.
 2// Use of this source code is governed by a MIT-style
 3// license that can be found in the LICENSE file.
 4
 5// capgen is an utility to test captcha generation.
 6package main
 7
 8import (
 9	"flag"
10	"fmt"
11	"github.com/dchest/captcha"
12	"io"
13	"log"
14	"os"
15)
16
17var (
18	flagImage = flag.Bool("i", true, "output image captcha")
19	flagAudio = flag.Bool("a", false, "output audio captcha")
20	flagLang  = flag.String("lang", "en", "language for audio captcha")
21	flagLen   = flag.Int("len", captcha.DefaultLen, "length of captcha")
22	flagImgW  = flag.Int("width", captcha.StdWidth, "image captcha width")
23	flagImgH  = flag.Int("height", captcha.StdHeight, "image captcha height")
24)
25
26func usage() {
27	fmt.Fprintf(os.Stderr, "usage: captcha [flags] filename\n")
28	flag.PrintDefaults()
29}
30
31func main() {
32	flag.Parse()
33	fname := flag.Arg(0)
34	if fname == "" {
35		usage()
36		os.Exit(1)
37	}
38	f, err := os.Create(fname)
39	if err != nil {
40		log.Fatalf("%s", err)
41	}
42	defer f.Close()
43	var w io.WriterTo
44	d := captcha.RandomDigits(*flagLen)
45	switch {
46	case *flagAudio:
47		w = captcha.NewAudio(d, *flagLang)
48	case *flagImage:
49		w = captcha.NewImage(d, *flagImgW, *flagImgH)
50	}
51	_, err = w.WriteTo(f)
52	if err != nil {
53		log.Fatalf("%s", err)
54	}
55	fmt.Println(d)
56}