2019-02-10 19:00:16 +00:00
|
|
|
// Copyright 2019 Drone IO, Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
package rand
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
)
|
|
|
|
|
|
|
|
var chars = []byte("abcdefghijklmnopqrstuvwxyz0123456789")
|
|
|
|
|
|
|
|
// random string length
|
|
|
|
const length = 32
|
|
|
|
|
|
|
|
// String returns a string value.
|
|
|
|
func String() string {
|
|
|
|
clen := len(chars)
|
|
|
|
maxrb := 255 - (256 % clen)
|
|
|
|
b := make([]byte, length)
|
|
|
|
r := make([]byte, length+(length/4)) // storage for random bytes.
|
|
|
|
i := 0
|
|
|
|
for {
|
|
|
|
if _, err := rand.Read(r); err != nil {
|
|
|
|
panic("rand: error reading random bytes")
|
|
|
|
}
|
|
|
|
for _, rb := range r {
|
|
|
|
c := int(rb)
|
|
|
|
if c > maxrb {
|
|
|
|
// Skip this number to avoid modulo bias.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
b[i] = chars[c%clen]
|
|
|
|
i++
|
|
|
|
if i == length {
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|