OLD | NEW |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Unix cryptographically secure pseudorandom number | 5 // Unix cryptographically secure pseudorandom number |
6 // generator. | 6 // generator. |
7 | 7 |
8 package rand | 8 package rand |
9 | 9 |
10 import ( | 10 import ( |
(...skipping 14 matching lines...) Expand all Loading... |
25 type devReader struct { | 25 type devReader struct { |
26 name string | 26 name string |
27 f io.Reader | 27 f io.Reader |
28 mu sync.Mutex | 28 mu sync.Mutex |
29 } | 29 } |
30 | 30 |
31 func (r *devReader) Read(b []byte) (n int, err os.Error) { | 31 func (r *devReader) Read(b []byte) (n int, err os.Error) { |
32 r.mu.Lock() | 32 r.mu.Lock() |
33 defer r.mu.Unlock() | 33 defer r.mu.Unlock() |
34 if r.f == nil { | 34 if r.f == nil { |
35 » » f, err := os.Open(r.name, os.O_RDONLY, 0) | 35 » » f, err := os.Open(r.name) |
36 if f == nil { | 36 if f == nil { |
37 return 0, err | 37 return 0, err |
38 } | 38 } |
39 r.f = bufio.NewReader(f) | 39 r.f = bufio.NewReader(f) |
40 } | 40 } |
41 return r.f.Read(b) | 41 return r.f.Read(b) |
42 } | 42 } |
43 | 43 |
44 // Alternate pseudo-random implementation for use on | 44 // Alternate pseudo-random implementation for use on |
45 // systems without a reliable /dev/urandom. So far we | 45 // systems without a reliable /dev/urandom. So far we |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 r.seed[i] = r.time[i] ^ r.dst[i] | 116 r.seed[i] = r.time[i] ^ r.dst[i] |
117 } | 117 } |
118 r.cipher.Encrypt(r.seed[0:], r.seed[0:]) | 118 r.cipher.Encrypt(r.seed[0:], r.seed[0:]) |
119 | 119 |
120 m := copy(b, r.dst[0:]) | 120 m := copy(b, r.dst[0:]) |
121 b = b[m:] | 121 b = b[m:] |
122 } | 122 } |
123 | 123 |
124 return n, nil | 124 return n, nil |
125 } | 125 } |
OLD | NEW |