Skip to content

Instantly share code, notes, and snippets.

@countingtoten
Created August 21, 2021 20:47
Show Gist options
  • Save countingtoten/dee015bee6a39780f6de92deec6b2725 to your computer and use it in GitHub Desktop.
Save countingtoten/dee015bee6a39780f6de92deec6b2725 to your computer and use it in GitHub Desktop.
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