You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.3 KiB
50 lines
1.3 KiB
package generate
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/binary"
|
|
"encoding/hex"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// ID generates a 24 character hex string from 8 bytes of current time
|
|
// in ns and 4 bytes of crypto-random. In practical terms, that makes
|
|
// them orderable.
|
|
func ID() string {
|
|
bytes := make([]byte, 12)
|
|
binary.BigEndian.PutUint64(bytes, uint64(time.Now().UnixNano()))
|
|
rand.Read(bytes[8:])
|
|
|
|
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)
|
|
rand.Read(bytes)
|
|
|
|
return strings.ToLower(hex.EncodeToString(bytes))
|
|
}
|