all repos — captcha @ d7cb604d49c3777d159741e1df389c6c540310f8

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

random.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
 5package captcha
 6
 7import (
 8	crand "crypto/rand"
 9	"io"
10	"rand"
11	"time"
12)
13
14// idLen is a length of captcha id string.
15const idLen = 20
16
17// idChars are characters allowed in captcha id.
18var idChars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
19
20func init() {
21	rand.Seed(time.Nanoseconds())
22}
23
24// RandomDigits returns a byte slice of the given length containing
25// pseudorandom numbers in range 0-9. The slice can be used as a captcha
26// solution.
27func RandomDigits(length int) (b []byte) {
28	b = randomBytes(length)
29	for i := range b {
30		b[i] %= 10
31	}
32	return
33}
34
35// randomBytes returns a byte slice of the given length read from CSPRNG.
36func randomBytes(length int) (b []byte) {
37	b = make([]byte, length)
38	if _, err := io.ReadFull(crand.Reader, b); err != nil {
39		panic("captcha: error reading random source: " + err.String())
40	}
41	return
42}
43
44// randomId returns a new random id string.
45func randomId() string {
46	b := randomBytes(idLen)
47	alen := byte(len(idChars))
48	for i, c := range b {
49		b[i] = idChars[c%alen]
50	}
51	return string(b)
52}
53
54// rnd returns a non-crypto pseudorandom int in range [from, to].
55func rnd(from, to int) int {
56	return rand.Intn(to+1-from) + from
57}
58
59// rndf returns a non-crypto pseudorandom float64 in range [from, to].
60func rndf(from, to float64) float64 {
61	return (to-from)*rand.Float64() + from
62}