diff --git a/generate/id.go b/generate/id.go index e31403f..e4351e2 100644 --- a/generate/id.go +++ b/generate/id.go @@ -19,6 +19,28 @@ func ID() string { 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 func SessionID() string { bytes := make([]byte, 24)