https://medium.com/@kpbird/golang-generate-fixed-size-random-string-dd6dbd5e63c0
(n int)
| 68 | |
| 69 | // https://medium.com/@kpbird/golang-generate-fixed-size-random-string-dd6dbd5e63c0 |
| 70 | func RandomString(n int) string { |
| 71 | const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 72 | |
| 73 | output := make([]byte, n) |
| 74 | // We will take n bytes, one byte for each character of output. |
| 75 | randomness := make([]byte, n) |
| 76 | // read all random |
| 77 | _, err := rand.Read(randomness) |
| 78 | if err != nil { |
| 79 | panic(err) |
| 80 | } |
| 81 | l := len(letterBytes) |
| 82 | // fill output |
| 83 | for pos := range output { |
| 84 | // get random item |
| 85 | random := uint8(randomness[pos]) |
| 86 | // random % 64 |
| 87 | randomPos := random % uint8(l) |
| 88 | // put into output |
| 89 | output[pos] = letterBytes[randomPos] |
| 90 | } |
| 91 | return string(output) |
| 92 | } |
| 93 | |
| 94 | func BuildParamsHeader(params map[string]interface{}) (string) { |
| 95 | header := "CYPHER " |