Created
August 21, 2021 20:47
-
-
Save countingtoten/dee015bee6a39780f6de92deec6b2725 to your computer and use it in GitHub Desktop.
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 rand | |
import ( | |
"crypto/rand" | |
"encoding/base64" | |
) | |
const ( | |
byteLength = 6 | |
) | |
// NewURLSafeToken generate and return a token that can be used in a url | |
func NewURLSafeToken() (string, error) { | |
buf := make([]byte, byteLength) | |
_, err := rand.Read(buf) | |
if err != nil { | |
return "", err | |
} | |
length := base64.URLEncoding.EncodedLen(byteLength) | |
enc := make([]byte, length) | |
base64.URLEncoding.Encode(enc, buf) | |
return string(enc), nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment