Update documentation.
Dmitry Chestnykh dmitry@codingrobots.com
Thu, 21 Apr 2011 18:05:53 +0200
2 files changed,
19 insertions(+),
10 deletions(-)
M
captcha.go
→
captcha.go
@@ -13,8 +13,12 @@ "sync"
) const ( + // Expiration time for captchas Expiration = 2 * 60 // 2 minutes - CollectNum = 100 // number of items that triggers collection + // The number of captchas created that triggers garbage collection + CollectNum = 100 + // The number of numbers to use in captcha + NumCount = 6 ) // expValue stores timestamp and id of captchas. It is used in a list inside@@ -48,7 +52,7 @@ rand.Seed(time.Seconds())
} func randomNumbers() []byte { - n := make([]byte, 6) + n := make([]byte, NumCount) if _, err := io.ReadFull(crand.Reader, n); err != nil { panic(err) }@@ -58,7 +62,8 @@ }
return n } -// New creates a new captcha, saves it in internal storage, and returns its id. +// New creates a new captcha, saves it in the internal storage, and returns its +// id. func New() string { ns := randomNumbers() id := uniuri.New()@@ -67,7 +72,7 @@ defer store.mu.Unlock()
store.ids[id] = ns store.exp.PushBack(expValue{time.Seconds(), id}) store.colNum++ - if store.colNum > collectNum { + if store.colNum > CollectNum { Collect() store.colNum = 0 }@@ -86,8 +91,11 @@ }
return NewImage(ns, width, height).PNGEncode(w) } -// Verify returns true if the given numbers are the numbers that were used in -// the given captcha id. +// Verify returns true if the given numbers are the numbers that were used to +// create the given captcha id. +// +// The function deletes the captcha with the given id from the internal +// storage, so that the same captcha can't be used anymore. func Verify(id string, numbers []byte) bool { store.mu.Lock() defer store.mu.Unlock()@@ -99,7 +107,7 @@ store.ids[id] = nil, false
return bytes.Equal(numbers, realns) } -// Collect garbage-collects expired and used captchas from the internal +// Collect deletes expired and used captchas from the internal // storage. It is called automatically by New function every CollectNum // generated captchas, but still exported to enable freeing memory manually if // needed.@@ -112,7 +120,7 @@ ev, ok := e.Value.(expValue)
if !ok { return } - if ev.timestamp+expiration < now { + if ev.timestamp+Expiration < now { store.ids[ev.id] = nil, false store.exp.Remove(e) } else {
M
image.go
→
image.go
@@ -9,10 +9,11 @@ "rand"
) const ( - maxSkew = 2 // Standard width and height for captcha image StdWidth = 300 StdHeight = 80 + + maxSkew = 2 ) type CaptchaImage struct {@@ -24,7 +25,7 @@ dotSize int
} // NewImage returns a new captcha image of the given width and height with the -// given slice of numbers, where each number is 0-9. +// given slice of numbers, where each number must be in range 0-9. func NewImage(numbers []byte, width, height int) *CaptchaImage { img := new(CaptchaImage) img.NRGBA = image.NewNRGBA(width, height)