all repos — captcha @ 68d217e79dfab952ad179041ac11e1efaf071d3a

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

Export RandomDigits. Remove NewRandom* functions.
Dmitry Chestnykh dmitry@codingrobots.com
Sun, 24 Apr 2011 13:30:31 +0200
commit

68d217e79dfab952ad179041ac11e1efaf071d3a

parent

c179a205da7d0e66a572f0798b189398bf492658

4 files changed, 8 insertions(+), 26 deletions(-)

jump to
M audio.goaudio.go

@@ -74,15 +74,6 @@ a.body.Write(endingBeepSound)

return a } -// NewRandomAudio generates a sequence of random digits with the given length, -// and returns a new audio captcha with these digits, and the sequence of -// digits itself. -func NewRandomAudio(length int) (a *Audio, digits []byte) { - digits = randomDigits(length) - a = NewAudio(digits) - return -} - // WriteTo writes captcha audio in WAVE format into the given io.Writer, and // returns the number of bytes written and an error if any. func (a *Audio) WriteTo(w io.Writer) (n int64, err os.Error) {
M captcha.gocaptcha.go

@@ -13,9 +13,9 @@ const StdLength = 6

var globalStore = newStore() -// randomDigits return a byte slice of the given length containing random +// RandomDigits returns a byte slice of the given length containing random // digits in range 0-9. -func randomDigits(length int) []byte { +func RandomDigits(length int) []byte { d := make([]byte, length) if _, err := io.ReadFull(rand.Reader, d); err != nil { panic(err)

@@ -30,7 +30,7 @@ // New creates a new captcha of the given length, saves it in the internal

// storage, and returns its id. func New(length int) (id string) { id = uniuri.New() - globalStore.saveCaptcha(id, randomDigits(length)) + globalStore.saveCaptcha(id, RandomDigits(length)) return }

@@ -45,7 +45,7 @@ old := globalStore.getDigits(id)

if old == nil { return false } - globalStore.saveCaptcha(id, randomDigits(len(old))) + globalStore.saveCaptcha(id, RandomDigits(len(old))) return true }
M cmd/main.gocmd/main.go

@@ -35,16 +35,16 @@ log.Fatalf("%s", err)

} defer f.Close() var w io.WriterTo - var ns []byte + d := captcha.RandomDigits(*flagLen) switch { case *flagAudio: - w, ns = captcha.NewRandomAudio(*flagLen) + w = captcha.NewAudio(d) case *flagImage: - w, ns = captcha.NewRandomImage(*flagLen, *flagImgW, *flagImgH) + w = captcha.NewImage(d, *flagImgW, *flagImgH) } _, err = w.WriteTo(f) if err != nil { log.Fatalf("%s", err) } - fmt.Println(ns) + fmt.Println(d) }
M image.goimage.go

@@ -59,15 +59,6 @@ img.strikeThrough()

return img } -// NewRandomImage generates a sequence of random digits with the given length, -// and returns a new captcha image of the given width and height with generated -// digits printed on it, and the sequence of digits itself. -func NewRandomImage(length, width, height int) (img *Image, digits []byte) { - digits = randomDigits(length) - img = NewImage(digits, width, height) - return -} - // WriteTo writes captcha image in PNG format into the given writer. // // Bug: while Image conforms to io.WriterTo interface, this function returns 0