2015-02-01 13:57:57 +00:00
|
|
|
package uid
|
2013-03-17 15:42:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
2013-10-11 11:57:07 +00:00
|
|
|
"encoding/hex"
|
2013-03-17 15:42:47 +00:00
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// uid returns a unique id. These ids consist of 128 bits from a
|
|
|
|
// cryptographically strong pseudo-random generator and are like uuids, but
|
|
|
|
// without the dashes and significant bits.
|
|
|
|
//
|
|
|
|
// See: http://en.wikipedia.org/wiki/UUID#Random_UUID_probability_of_duplicates
|
2015-02-01 13:57:57 +00:00
|
|
|
func Uid() string {
|
2013-03-17 15:42:47 +00:00
|
|
|
id := make([]byte, 16)
|
|
|
|
_, err := io.ReadFull(rand.Reader, id)
|
|
|
|
if err != nil {
|
2016-09-27 20:10:16 +00:00
|
|
|
// This is probably an appropriate way to handle errors from our source
|
2013-03-17 15:42:47 +00:00
|
|
|
// for random bits.
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-10-11 11:57:07 +00:00
|
|
|
return hex.EncodeToString(id)
|
2013-03-17 15:42:47 +00:00
|
|
|
}
|