| 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. |
| 6 | // |
| 7 | //nolint:forbidigo |
| 8 | package main |
| 9 | |
| 10 | import ( |
| 11 | "flag" |
| 12 | "fmt" |
| 13 | "os" |
| 14 | |
| 15 | "code.forgejo.org/go-chi/captcha" |
| 16 | ) |
| 17 | |
| 18 | var ( |
| 19 | flagLen = flag.Int("len", captcha.DefaultLen, "length of captcha") |
| 20 | flagImgW = flag.Int("width", captcha.StdWidth, "image captcha width") |
| 21 | flagImgH = flag.Int("height", captcha.StdHeight, "image captcha height") |
| 22 | ) |
| 23 | |
| 24 | func usage() { |
| 25 | fmt.Fprintf(os.Stderr, "usage: capgen [flags] filename\n") |
| 26 | flag.PrintDefaults() |
| 27 | } |
| 28 | |
| 29 | func main() { |
| 30 | flag.Parse() |
| 31 | fname := flag.Arg(0) |
| 32 | if fname == "" { |
| 33 | usage() |
| 34 | os.Exit(1) |
| 35 | } |
| 36 | f, err := os.Create(fname) |
| 37 | if err != nil { |
| 38 | panic(err) |
| 39 | } |
| 40 | defer f.Close() |
| 41 | d := captcha.RandomDigits(*flagLen) |
| 42 | if _, err := captcha.NewImage("", d, *flagImgW, *flagImgH).WriteTo(f); err != nil { |
| 43 | panic(err) |
| 44 | } |
| 45 | for _, digit := range d { |
| 46 | fmt.Print(string(digit + 48)) |
| 47 | } |
| 48 | fmt.Println() |
| 49 | } |