|
@ -19,6 +19,28 @@ func ID() string { |
|
|
return strings.ToLower(hex.EncodeToString(bytes)) |
|
|
return strings.ToLower(hex.EncodeToString(bytes)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// FriendlyID generates a human-friendly ID that has a higher uniqueness,
|
|
|
|
|
|
// and a limited alphabet designed for human readability and manual copying.
|
|
|
|
|
|
//
|
|
|
|
|
|
// There are 7.98e+22 IDs in a 16-length ID, which means that if there are
|
|
|
|
|
|
// 10,000 items, you're as likely to guess one as winning the grand prize
|
|
|
|
|
|
// of the biggest lotteries around the world billions of times in a row.
|
|
|
|
|
|
func FriendlyID(length int) string { |
|
|
|
|
|
const alphabet = "0123456789abcdfghjkmpqrtvxy" |
|
|
|
|
|
const alphabetLength = byte(len(alphabet)) |
|
|
|
|
|
|
|
|
|
|
|
// Generate random bytes
|
|
|
|
|
|
results := make([]byte, length) |
|
|
|
|
|
rand.Read(results) |
|
|
|
|
|
|
|
|
|
|
|
// Truncate it within the alphabet
|
|
|
|
|
|
for i := 0; i < 16; i++ { |
|
|
|
|
|
results[i] = alphabet[results[i]%alphabetLength] |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return string(results) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// SessionID generates a 48 character hex string with crypto/rand
|
|
|
// SessionID generates a 48 character hex string with crypto/rand
|
|
|
func SessionID() string { |
|
|
func SessionID() string { |
|
|
bytes := make([]byte, 24) |
|
|
bytes := make([]byte, 24) |
|
|