|
@@ -43,8 +43,6 @@ package captcha
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
"bytes"
|
|
"bytes"
|
|
|
- "crypto/rand"
|
|
|
|
|
- "github.com/dchest/uniuri"
|
|
|
|
|
"io"
|
|
"io"
|
|
|
"os"
|
|
"os"
|
|
|
)
|
|
)
|
|
@@ -57,13 +55,13 @@ const (
|
|
|
CollectNum = 100
|
|
CollectNum = 100
|
|
|
// Expiration time of captchas used by default store.
|
|
// Expiration time of captchas used by default store.
|
|
|
Expiration = 10 * 60 // 10 minutes
|
|
Expiration = 10 * 60 // 10 minutes
|
|
|
-
|
|
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
-var ErrNotFound = os.NewError("captcha with the given id not found")
|
|
|
|
|
-
|
|
|
|
|
-// globalStore is a shared storage for captchas, generated by New function.
|
|
|
|
|
-var globalStore = NewMemoryStore(CollectNum, Expiration)
|
|
|
|
|
|
|
+var (
|
|
|
|
|
+ ErrNotFound = os.NewError("captcha with the given id not found")
|
|
|
|
|
+ // globalStore is a shared storage for captchas, generated by New function.
|
|
|
|
|
+ globalStore = NewMemoryStore(CollectNum, Expiration)
|
|
|
|
|
+)
|
|
|
|
|
|
|
|
// SetCustomStore sets custom storage for captchas, replacing the default
|
|
// SetCustomStore sets custom storage for captchas, replacing the default
|
|
|
// memory store. This function must be called before generating any captchas.
|
|
// memory store. This function must be called before generating any captchas.
|
|
@@ -71,19 +69,6 @@ func SetCustomStore(s Store) {
|
|
|
globalStore = s
|
|
globalStore = s
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// RandomDigits returns a byte slice of the given length containing random
|
|
|
|
|
-// digits in range 0-9.
|
|
|
|
|
-func RandomDigits(length int) []byte {
|
|
|
|
|
- d := make([]byte, length)
|
|
|
|
|
- if _, err := io.ReadFull(rand.Reader, d); err != nil {
|
|
|
|
|
- panic("error reading random source: " + err.String())
|
|
|
|
|
- }
|
|
|
|
|
- for i := range d {
|
|
|
|
|
- d[i] %= 10
|
|
|
|
|
- }
|
|
|
|
|
- return d
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
// New creates a new captcha with the standard length, saves it in the internal
|
|
// New creates a new captcha with the standard length, saves it in the internal
|
|
|
// storage and returns its id.
|
|
// storage and returns its id.
|
|
|
func New() string {
|
|
func New() string {
|
|
@@ -93,7 +78,7 @@ func New() string {
|
|
|
// NewLen is just like New, but accepts length of a captcha solution as the
|
|
// NewLen is just like New, but accepts length of a captcha solution as the
|
|
|
// argument.
|
|
// argument.
|
|
|
func NewLen(length int) (id string) {
|
|
func NewLen(length int) (id string) {
|
|
|
- id = uniuri.New()
|
|
|
|
|
|
|
+ id = randomId()
|
|
|
globalStore.Set(id, RandomDigits(length))
|
|
globalStore.Set(id, RandomDigits(length))
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|