Skip to content

Commit

Permalink
refactor(utils)
Browse files Browse the repository at this point in the history
  • Loading branch information
h2non committed Jun 5, 2016
1 parent b9f0f8a commit 5293daf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
14 changes: 7 additions & 7 deletions utils/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
// worldwide. Distributed without any warranty.
// http://creativecommons.org/publicdomain/zero/1.0/

// Package uniuri generates random strings good for use in URIs to identify
// Package id generates random strings good for use in URIs to identify
// unique objects.
//
// Example usage:
//
// s := uniuri.New() // s is now "apHCJBl7L1OmC57n"
// s := utils.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
Expand All @@ -26,14 +26,14 @@ package utils
import "crypto/rand"

const (
// StdLen is a standard length of uniuri string to achive ~95 bits of entropy.
// StdLen is a standard length of id string to achive ~95 bits of entropy.
StdLen = 16
// UUIDLen is a length of uniuri string to achive ~119 bits of entropy, closest
// UUIDLen is a length of id string to achive ~119 bits of entropy, closest
// to what can be losslessly converted to UUIDv4 (122 bits).
UUIDLen = 20
)

// StdChars is a set of standard characters allowed in uniuri string.
// StdChars is a set of standard characters allowed in id string.
var StdChars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")

// NewID returns a new random string of the standard length, consisting of
Expand All @@ -56,15 +56,15 @@ func NewLenChars(length int, chars []byte) string {
}
clen := len(chars)
if clen < 2 || clen > 256 {
panic("uniuri: wrong charset length for NewLenChars")
panic("id: 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("uniuri: error reading random bytes: " + err.Error())
panic("id: error reading random bytes: " + err.Error())
}
for _, rb := range r {
c := int(rb)
Expand Down
2 changes: 1 addition & 1 deletion utils/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestNewLenCharsMaxLength(t *testing.T) {
NewLenChars(32, chars)
}

func TestBias(t *testing.T) {
func testBias(t *testing.T) {
chars := []byte("abcdefghijklmnopqrstuvwxyz")
slen := 100000
s := NewLenChars(slen, chars)
Expand Down

0 comments on commit 5293daf

Please sign in to comment.