all repos — captcha @ 7633411f435abe1ce534409eaf47a96f230908fb

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	flagLen   = flag.Int("len", captcha.DefaultLen, "length of captcha")
21	flagImgW  = flag.Int("width", captcha.StdWidth, "image captcha width")
22	flagImgH  = flag.Int("height", captcha.StdHeight, "image captcha height")
23)
24
25func usage() {
26	fmt.Fprintf(os.Stderr, "usage: captcha [flags] filename\n")
27	flag.PrintDefaults()
28}
29
30func main() {
31	flag.Parse()
32	fname := flag.Arg(0)
33	if fname == "" {
34		usage()
35		os.Exit(1)
36	}
37	f, err := os.Create(fname)
38	if err != nil {
39		log.Fatalf("%s", err)
40	}
41	defer f.Close()
42	var w io.WriterTo
43	d := captcha.RandomDigits(*flagLen)
44	switch {
45	case *flagAudio:
46		w = captcha.NewAudio(d)
47	case *flagImage:
48		w = captcha.NewImage(d, *flagImgW, *flagImgH)
49	}
50	_, err = w.WriteTo(f)
51	if err != nil {
52		log.Fatalf("%s", err)
53	}
54	fmt.Println(d)
55}