Mercurial > gemma
annotate pkg/common/random.go @ 4855:01b593ea2311
Added missing doc strings for the STR tree in the mesh package.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 19 Nov 2019 17:48:02 +0100 |
parents | 8c5df0f3562e |
children | 5f47eeea988d |
rev | line source |
---|---|
1017
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
2 // without warranty, see README.md and license for details. |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
3 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
6 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
7 // Copyright (C) 2018 by via donau |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
9 // Software engineering by Intevation GmbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
10 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
11 // Author(s): |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
13 |
408
ac23905e64b1
Improve WFS proxy a lot. It now generates signed re-writings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
339
diff
changeset
|
14 package common |
339
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
15 |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
16 import ( |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
17 "bytes" |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
18 "crypto/rand" |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
19 "io" |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
20 "log" |
3650
01ce3ba9b0d0
Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3646
diff
changeset
|
21 "math" |
339
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
22 "math/big" |
3646
810b28f59b8b
Generate random points for second mesh.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1345
diff
changeset
|
23 mrand "math/rand" |
810b28f59b8b
Generate random points for second mesh.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1345
diff
changeset
|
24 "time" |
339
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
25 ) |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
26 |
1345
6a021108410b
Finished the doc strings for the common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1326
diff
changeset
|
27 // GenerateRandomKey generates a cryptographically secure random key |
6a021108410b
Finished the doc strings for the common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1326
diff
changeset
|
28 // of a given length. |
339
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
29 func GenerateRandomKey(length int) []byte { |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
30 k := make([]byte, length) |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
31 if _, err := io.ReadFull(rand.Reader, k); err != nil { |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
32 return nil |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
33 } |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
34 return k |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
35 } |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
36 |
1345
6a021108410b
Finished the doc strings for the common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1326
diff
changeset
|
37 // RandomString generates a cryptographically secure password |
6a021108410b
Finished the doc strings for the common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1326
diff
changeset
|
38 // of a given length which consists of alpha numeric characters |
6a021108410b
Finished the doc strings for the common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1326
diff
changeset
|
39 // and at least one 'special' one. |
339
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
40 func RandomString(n int) string { |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
41 |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
42 const ( |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
43 special = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
44 alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
45 "abcdefghijklmnopqrstuvwxyz" + |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
46 "0123456789" + |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
47 special |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
48 ) |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
49 |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
50 max := big.NewInt(int64(len(alphabet))) |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
51 out := make([]byte, n) |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
52 |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
53 for i := 0; i < 1000; i++ { |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
54 for i := range out { |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
55 v, err := rand.Int(rand.Reader, max) |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
56 if err != nil { |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
57 log.Panicf("error: %v\n", err) |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
58 } |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
59 out[i] = alphabet[v.Int64()] |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
60 } |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
61 // Ensure at least one special char. |
1326
515815565bd0
Simplified code a bit.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
62 if bytes.ContainsAny(out, special) { |
339
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
63 return string(out) |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
64 } |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
65 } |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
66 log.Println("warn: Your random generator may be broken.") |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
67 out[0] = special[0] |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
68 return string(out) |
33b59c848771
Factored out some miscellaneous code into own package.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
69 } |
3646
810b28f59b8b
Generate random points for second mesh.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1345
diff
changeset
|
70 |
4162
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3650
diff
changeset
|
71 // Random returns a function which generates pseudo random |
8c5df0f3562e
Made 'golint' and 'staticcheck' happy with common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3650
diff
changeset
|
72 // values in the range between low and high. |
3646
810b28f59b8b
Generate random points for second mesh.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1345
diff
changeset
|
73 func Random(low, high float64) func() float64 { |
810b28f59b8b
Generate random points for second mesh.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1345
diff
changeset
|
74 if low > high { |
810b28f59b8b
Generate random points for second mesh.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1345
diff
changeset
|
75 low, high = high, low |
810b28f59b8b
Generate random points for second mesh.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1345
diff
changeset
|
76 } |
3650
01ce3ba9b0d0
Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3646
diff
changeset
|
77 |
01ce3ba9b0d0
Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3646
diff
changeset
|
78 var seed int64 |
01ce3ba9b0d0
Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3646
diff
changeset
|
79 if seedInt, err := rand.Int(rand.Reader, big.NewInt(math.MaxInt64)); err != nil { |
01ce3ba9b0d0
Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3646
diff
changeset
|
80 log.Printf("warn: Generating good random seed failed: %v\n", err) |
01ce3ba9b0d0
Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3646
diff
changeset
|
81 seed = time.Now().Unix() |
01ce3ba9b0d0
Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3646
diff
changeset
|
82 } else { |
01ce3ba9b0d0
Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3646
diff
changeset
|
83 seed = seedInt.Int64() |
01ce3ba9b0d0
Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3646
diff
changeset
|
84 } |
01ce3ba9b0d0
Fixed generating of random points.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3646
diff
changeset
|
85 rnd := mrand.New(mrand.NewSource(seed)) |
3646
810b28f59b8b
Generate random points for second mesh.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1345
diff
changeset
|
86 m := high - low |
810b28f59b8b
Generate random points for second mesh.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1345
diff
changeset
|
87 return func() float64 { return rnd.Float64()*m + low } |
810b28f59b8b
Generate random points for second mesh.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1345
diff
changeset
|
88 } |