Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
61 lines (57 loc) · 1.59 KB

File metadata and controls

61 lines (57 loc) · 1.59 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Package randstring generates random strings.
//
// Example usage:
//
// s := randstring.NewLen(4) // s is now "apHC"
//
// A standard string created by NewLen consists of Latin upper and
// lowercase letters, and numbers (from the set of 62 allowed
// characters).
//
// Functions read from crypto/rand random source, and panic if they fail to
// read from it.
//
// This package is adapted (simplified) from Dmitry Chestnykh's uniuri
// package.
package randstring
import "crypto/rand"
// stdChars is a set of standard characters allowed in the string.
var stdChars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
// NewLen returns a new random string of the provided length,
// consisting of standard characters.
func NewLen(length int) string {
return NewLenChars(length, stdChars)
}
// NewLenChars returns a new random string of the provided length,
// consisting of the provided byte slice of allowed characters
// (maximum 256).
func NewLenChars(length int, chars []byte) string {
if length == 0 {
return ""
}
clen := len(chars)
if clen < 2 || clen > 256 {
panic("randstring: wrong charset length for NewLenChars")
}
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("randstring: error reading random bytes: " + err.Error())
}
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)
}
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.