2014-11-06 23:13:45 +01:00
|
|
|
// Written in 2011-2014 by Dmitry Chestnykh
|
|
|
|
//
|
|
|
|
// The author(s) have dedicated all copyright and related and
|
|
|
|
// neighboring rights to this software to the public domain
|
|
|
|
// worldwide. Distributed without any warranty.
|
|
|
|
// http://creativecommons.org/publicdomain/zero/1.0/
|
|
|
|
|
2011-04-05 17:57:25 +02:00
|
|
|
// Package uniuri generates random strings good for use in URIs to identify
|
|
|
|
// unique objects.
|
|
|
|
//
|
|
|
|
// Example usage:
|
|
|
|
//
|
|
|
|
// s := uniuri.New() // s is now "apHCJBl7L1OmC57n"
|
|
|
|
//
|
|
|
|
// A standard string created by New() is 16 bytes in length and consists of
|
|
|
|
// Latin upper and lowercase letters, and numbers (from the set of 62 allowed
|
|
|
|
// characters), which means that it has ~95 bits of entropy. To get more
|
|
|
|
// entropy, you can use NewLen(UUIDLen), which returns 20-byte string, giving
|
|
|
|
// ~119 bits of entropy, or any other desired length.
|
|
|
|
//
|
|
|
|
// Functions read from crypto/rand random source, and panic if they fail to
|
|
|
|
// read from it.
|
|
|
|
package uniuri
|
|
|
|
|
2012-01-20 15:24:58 +01:00
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"io"
|
|
|
|
)
|
2011-04-05 17:57:25 +02:00
|
|
|
|
|
|
|
const (
|
|
|
|
// Standard length of uniuri string to achive ~95 bits of entropy.
|
|
|
|
StdLen = 16
|
|
|
|
// Length of uniurl string to achive ~119 bits of entropy, closest
|
|
|
|
// to what can be losslessly converted to UUIDv4 (122 bits).
|
|
|
|
UUIDLen = 20
|
|
|
|
)
|
|
|
|
|
|
|
|
// Standard characters allowed in uniuri string.
|
2011-04-05 21:48:14 +02:00
|
|
|
var StdChars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
|
2011-04-05 17:57:25 +02:00
|
|
|
|
2011-04-05 19:55:23 +02:00
|
|
|
// New returns a new random string of the standard length, consisting of
|
|
|
|
// standard characters.
|
2011-04-05 17:57:25 +02:00
|
|
|
func New() string {
|
|
|
|
return NewLenChars(StdLen, StdChars)
|
|
|
|
}
|
|
|
|
|
2011-04-05 19:55:23 +02:00
|
|
|
// NewLen returns a new random string of the provided length, consisting of
|
|
|
|
// standard characters.
|
2011-04-05 17:57:25 +02:00
|
|
|
func NewLen(length int) string {
|
|
|
|
return NewLenChars(length, StdChars)
|
|
|
|
}
|
|
|
|
|
2011-04-05 19:55:23 +02:00
|
|
|
// NewLenChars returns a new random string of the provided length, consisting
|
|
|
|
// of the provided byte slice of allowed characters (maximum 256).
|
2011-04-05 17:57:25 +02:00
|
|
|
func NewLenChars(length int, chars []byte) string {
|
|
|
|
b := make([]byte, length)
|
2011-06-06 14:34:01 +02:00
|
|
|
r := make([]byte, length+(length/4)) // storage for random bytes.
|
2014-08-09 17:03:38 +02:00
|
|
|
clen := len(chars)
|
|
|
|
if clen > 256 {
|
|
|
|
panic("uniuri: maximum length of charset for NewLenChars is 256")
|
|
|
|
}
|
2014-08-09 17:13:26 +02:00
|
|
|
maxrb := 256 - (256 % clen)
|
2011-06-06 14:34:01 +02:00
|
|
|
i := 0
|
|
|
|
for {
|
|
|
|
if _, err := io.ReadFull(rand.Reader, r); err != nil {
|
2012-01-16 14:29:26 +01:00
|
|
|
panic("error reading from random source: " + err.Error())
|
2011-06-06 14:34:01 +02:00
|
|
|
}
|
2014-08-09 17:05:33 +02:00
|
|
|
for _, rb := range r {
|
|
|
|
c := int(rb)
|
2014-08-09 15:08:44 +02:00
|
|
|
if c > maxrb {
|
2011-06-06 14:34:01 +02:00
|
|
|
// Skip this number to avoid modulo bias.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
b[i] = chars[c%clen]
|
|
|
|
i++
|
|
|
|
if i == length {
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
}
|
2011-04-05 17:57:25 +02:00
|
|
|
}
|
2013-12-12 18:01:04 +01:00
|
|
|
panic("unreachable")
|
2011-04-05 17:57:25 +02:00
|
|
|
}
|