comparison common/random.go @ 408:ac23905e64b1

Improve WFS proxy a lot. It now generates signed re-writings.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 15 Aug 2018 15:55:41 +0200
parents misc/random.go@33b59c848771
children
comparison
equal deleted inserted replaced
407:cffd144c99ea 408:ac23905e64b1
1 package common
2
3 import (
4 "bytes"
5 "crypto/rand"
6 "io"
7 "log"
8 "math/big"
9 )
10
11 func GenerateRandomKey(length int) []byte {
12 k := make([]byte, length)
13 if _, err := io.ReadFull(rand.Reader, k); err != nil {
14 return nil
15 }
16 return k
17 }
18
19 func RandomString(n int) string {
20
21 const (
22 special = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
23 alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
24 "abcdefghijklmnopqrstuvwxyz" +
25 "0123456789" +
26 special
27 )
28
29 max := big.NewInt(int64(len(alphabet)))
30 out := make([]byte, n)
31
32 for i := 0; i < 1000; i++ {
33 for i := range out {
34 v, err := rand.Int(rand.Reader, max)
35 if err != nil {
36 log.Panicf("error: %v\n", err)
37 }
38 out[i] = alphabet[v.Int64()]
39 }
40 // Ensure at least one special char.
41 if bytes.IndexAny(out, special) >= 0 {
42 return string(out)
43 }
44 }
45 log.Println("warn: Your random generator may be broken.")
46 out[0] = special[0]
47 return string(out)
48 }