Created
May 27, 2019 06:17
-
-
Save jony4/de199cd03e999d2131b3a09f61d0a83b to your computer and use it in GitHub Desktop.
SUID is a simple golang library that generates concise, unambiguous, URL-safe UUIDs.You can use it as your Private Password Generator!
This file contains hidden or 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 suid | |
import ( | |
"fmt" | |
"math/big" | |
"github.com/google/uuid" | |
) | |
const ( | |
alphabet = "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" | |
) | |
var ( | |
zero = big.NewInt(0) | |
zeronew = big.NewInt(0) | |
alphalen = big.NewInt(int64(len(alphabet))) | |
) | |
// SUID is short uuid, which is human readable and based on google/uuid (UUID V4) package unique id. | |
func SUID() string { | |
uuid := uuid.New().String() | |
var uuidb = []byte(uuid) | |
nbyte := append([]byte{}, append(uuidb[0:8], append(uuidb[9:13], append(uuidb[14:18], append(uuidb[19:23], append(uuidb[24:28], uuidb[28:]...)...)...)...)...)...) | |
bigi, ok := zero.SetString(string(nbyte), 16) | |
if !ok { | |
panic(fmt.Errorf("couldn't zero.SetString")) | |
} | |
var output []byte | |
for bigi.Int64() != 0 { | |
bign, digit := zeronew.DivMod(bigi, alphalen, new(big.Int)) | |
output = append(output, alphabet[digit.Int64()]) | |
bigi = bign | |
} | |
return string(output) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment