| 1 | // Written in 2011-2014 by Dmitry Chestnykh |
| 2 | // |
| 3 | // The author(s) have dedicated all copyright and related and |
| 4 | // neighboring rights to this software to the public domain |
| 5 | // worldwide. Distributed without any warranty. |
| 6 | // http://creativecommons.org/publicdomain/zero/1.0/ |
| 7 | |
| 8 | // Package uniuri generates random strings good for use in URIs to identify |
| 9 | // unique objects. |
| 10 | // |
| 11 | // Example usage: |
| 12 | // |
| 13 | // s := uniuri.New() // s is now "apHCJBl7L1OmC57n" |
| 14 | // |
| 15 | // A standard string created by New() is 16 bytes in length and consists of |
| 16 | // Latin upper and lowercase letters, and numbers (from the set of 62 allowed |
| 17 | // characters), which means that it has ~95 bits of entropy. To get more |
| 18 | // entropy, you can use NewLen(UUIDLen), which returns 20-byte string, giving |
| 19 | // ~119 bits of entropy, or any other desired length. |
| 20 | // |
| 21 | // Functions read from crypto/rand random source, and panic if they fail to |
| 22 | // read from it. |
| 23 | package uniuri |
| 24 | |
| 25 | import ( |
| 26 | "crypto/rand" |
| 27 | "math" |
| 28 | ) |
| 29 | |
| 30 | const ( |
| 31 | // StdLen is a standard length of uniuri string to achive ~95 bits of entropy. |
| 32 | StdLen = 16 |
| 33 | // UUIDLen is a length of uniuri string to achive ~119 bits of entropy, closest |
| 34 | // to what can be losslessly converted to UUIDv4 (122 bits). |
| 35 | UUIDLen = 20 |
| 36 | ) |
| 37 | |
| 38 | // StdChars is a set of standard characters allowed in uniuri string. |
| 39 | var StdChars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") |
| 40 | |
| 41 | // New returns a new random string of the standard length, consisting of |
| 42 | // standard characters. |
| 43 | func New() string { |
| 44 | return NewLenChars(StdLen, StdChars) |
| 45 | } |
| 46 | |
| 47 | // NewLen returns a new random string of the provided length, consisting of |
| 48 | // standard characters. |
| 49 | func NewLen(length int) string { |
| 50 | return NewLenChars(length, StdChars) |
| 51 | } |
| 52 | |
| 53 | // maxBufLen is the maximum length of a temporary buffer for random bytes. |
| 54 | const maxBufLen = 2048 |
| 55 | |
| 56 | // minRegenBufLen is the minimum length of temporary buffer for random bytes |
| 57 | // to fill after the first rand.Read request didn't produce the full result. |
| 58 | // If the initial buffer is smaller, this value is ignored. |
| 59 | // Rationale: for performance, assume it's pointless to request fewer bytes from rand.Read. |
| 60 | const minRegenBufLen = 16 |
| 61 | |
| 62 | // estimatedBufLen returns the estimated number of random bytes to request |
| 63 | // given that byte values greater than maxByte will be rejected. |
| 64 | func estimatedBufLen(need, maxByte int) int { |
| 65 | return int(math.Ceil(float64(need) * (255 / float64(maxByte)))) |
| 66 | } |
| 67 | |
| 68 | // NewLenCharsBytes returns a new random byte slice of the provided length, consisting |
| 69 | // of the provided byte slice of allowed characters (maximum 256). |
| 70 | func NewLenCharsBytes(length int, chars []byte) []byte { |
| 71 | if length == 0 { |
| 72 | return nil |
| 73 | } |
| 74 | clen := len(chars) |
| 75 | if clen < 2 || clen > 256 { |
| 76 | panic("uniuri: wrong charset length for NewLenChars") |
| 77 | } |
| 78 | maxrb := 255 - (256 % clen) |
| 79 | buflen := estimatedBufLen(length, maxrb) |
| 80 | if buflen < length { |
| 81 | buflen = length |
| 82 | } |
| 83 | if buflen > maxBufLen { |
| 84 | buflen = maxBufLen |
| 85 | } |
| 86 | buf := make([]byte, buflen) // storage for random bytes |
| 87 | out := make([]byte, length) // storage for result |
| 88 | i := 0 |
| 89 | for { |
| 90 | if _, err := rand.Read(buf[:buflen]); err != nil { |
| 91 | panic("uniuri: error reading random bytes: " + err.Error()) |
| 92 | } |
| 93 | for _, rb := range buf[:buflen] { |
| 94 | c := int(rb) |
| 95 | if c > maxrb { |
| 96 | // Skip this number to avoid modulo bias. |
| 97 | continue |
| 98 | } |
| 99 | out[i] = chars[c%clen] |
| 100 | i++ |
| 101 | if i == length { |
| 102 | return out |
| 103 | } |
| 104 | } |
| 105 | // Adjust new requested length, but no smaller than minRegenBufLen. |
| 106 | buflen = estimatedBufLen(length-i, maxrb) |
| 107 | if buflen < minRegenBufLen && minRegenBufLen < cap(buf) { |
| 108 | buflen = minRegenBufLen |
| 109 | } |
| 110 | if buflen > maxBufLen { |
| 111 | buflen = maxBufLen |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // NewLenChars returns a new random string of the provided length, consisting |
| 117 | // of the provided byte slice of allowed characters (maximum 256). |
| 118 | func NewLenChars(length int, chars []byte) string { |
| 119 | return string(NewLenCharsBytes(length, chars)) |
| 120 | } |