Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Koderank/uuid/uuid.go
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
41 lines (33 sloc)
965 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package uuid | |
import ( | |
"crypto/rand" | |
"fmt" | |
"math/big" | |
) | |
const base62alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
// Creates a hexadecimal UUID. | |
func NewUuid() string { | |
b := make([]byte, 16) | |
rand.Read(b) | |
return fmt.Sprintf("%x", b) | |
} | |
// Creates a base-62 UUID. | |
func NewUuid62() string { | |
return ConvertUp(NewUuid(), base62alphabet) | |
} | |
// ConvertUp converts a hexadecimal UUID string to a base alphabet greater than | |
// 16. It is used here to compress a 32 character UUID down to 23 URL friendly | |
// characters. | |
func ConvertUp(oldNumber string, baseAlphabet string) string { | |
n := big.NewInt(0) | |
n.SetString(oldNumber, 16) | |
base := big.NewInt(int64(len(baseAlphabet))) | |
newNumber := make([]byte, 23) //converted size of max base-62 uuid | |
i := len(newNumber) | |
for n.Int64() != 0 { | |
i-- | |
_, r := n.DivMod(n, base, big.NewInt(0)) | |
newNumber[i] = baseAlphabet[r.Int64()] | |
} | |
return string(newNumber[i:]) | |
} |