Debian Codegraph @

random.go

raw symbols SCIP
1 // Copyright 2011-2014 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 package captcha
6
7 import (
8 "crypto/hmac"
9 "crypto/rand"
10 "crypto/sha256"
11 "io"
12 )
13
14 // idLen is a length of captcha id string.
15 // (20 bytes of 62-letter alphabet give ~119 bits.)
16 const idLen = 20
17
18 // idChars are characters allowed in captcha id.
19 var idChars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
20
21 // rngKey is a secret key used to deterministically derive seeds for
22 // PRNGs used in image. Generated once during initialization.
23 var rngKey [32]byte
24
25 func init() {
26 if _, err := io.ReadFull(rand.Reader, rngKey[:]); err != nil {
27 panic("captcha: error reading random source: " + err.Error())
28 }
29 }
30
31 // Purposes for seed derivation. The goal is to make deterministic PRNG produce
32 // different outputs for images and audio by using different derived seeds.
33 const (
34 imageSeedPurpose = 0x01
35 audioSeedPurpose = 0x02
36 )
37
38 // deriveSeed returns a 16-byte PRNG seed from rngKey, purpose, id and digits.
39 // Same purpose, id and digits will result in the same derived seed for this
40 // instance of running application.
41 //
42 // out = HMAC(rngKey, purpose || id || 0x00 || digits)
43 func deriveSeed(purpose byte, id string, digits []byte) (out [32]byte) {
44 var buf [sha256.Size]byte
45
46 h := hmac.New(sha256.New, rngKey[:])
47 h.Write([]byte{purpose})
48 h.Write([]byte(id))
49 h.Write([]byte{0})
50 h.Write(digits)
51
52 sum := h.Sum(buf[:0])
53 copy(out[:], sum)
54 return out
55 }
56
57 // RandomDigits returns a byte slice of the given length containing
58 // pseudorandom numbers in range 0-9. The slice can be used as a captcha
59 // solution.
60 func RandomDigits(length int) []byte {
61 return randomBytesMod(length, 10)
62 }
63
64 // randomBytes returns a byte slice of the given length read from CSPRNG.
65 func randomBytes(length int) (b []byte) {
66 b = make([]byte, length)
67 if _, err := io.ReadFull(rand.Reader, b); err != nil {
68 panic("captcha: error reading random source: " + err.Error())
69 }
70 return b
71 }
72
73 // randomBytesMod returns a byte slice of the given length, where each byte is
74 // a random number modulo mod.
75 func randomBytesMod(length int, mod byte) (b []byte) {
76 if length == 0 {
77 return nil
78 }
79 if mod == 0 {
80 panic("captcha: bad mod argument for randomBytesMod")
81 }
82
83 maxrb := 255 - byte(256%int(mod))
84 b = make([]byte, length)
85 i := 0
86 for {
87 r := randomBytes(length + (length / 4))
88 for _, c := range r {
89 if c > maxrb {
90 // Skip this number to avoid modulo bias.
91 continue
92 }
93 b[i] = c % mod
94 i++
95 if i == length {
96 return b
97 }
98 }
99 }
100 }
101
102 // randomID returns a new random id string.
103 func randomID() string {
104 b := randomBytesMod(idLen, byte(len(idChars)))
105 for i, c := range b {
106 b[i] = idChars[c]
107 }
108 return string(b)
109 }