For a RESTful backend API, I want to generate unique url tokens to be used to authenticate users.
The unique data provided at registration to generate tokens are email addresses. But after generating tokens and sending that to the users, I don't need to decrypt received tokens to get email or other information. So the encryption can be one-way.
Initially I used bcrypt to do so:
func GenerateToken(email string) string {
hash, err := bcrypt.GenerateFromPassword([]byte(email), bcrypt.DefaultCost)
if err != nil {
log.Fatal(err)
}
fmt.Println("Hash to store:", string(hash))
return string(hash)
}
But since the tokens come as a url parameter (like /api/path/to/{token}
) I can not use bcrypt because it generates tokens containing /
like this:
"$2a$10$NebCQ8BD7xOa82nkzRGA9OEh./zhBOPcuV98vpOKBKK6ZTFuHtqlK"
which will break the routing.
So I'm wondering what is the best way to generate some unique 16-32 character alphanumeric tokens based on emails in Golang?
As it was already mentioned you are doing it wrong and this is super insecure.
func GenerateSecureToken(length int) string {
b := make([]byte, length)
if _, err := rand.Read(b); err != nil {
return ""
}
return hex.EncodeToString(b)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With