csrf/rand.go

29 lines
596 B
Go
Raw Permalink Normal View History

2025-01-12 11:40:15 +00:00
package csrf
import (
"crypto/rand"
"encoding/base64"
"fmt"
"math"
)
func urlRandomBase64String(l int) string {
buff := make([]byte, int(math.Ceil(float64(l)/float64(1.33333333333))))
rand.Read(buff)
str := base64.RawURLEncoding.EncodeToString(buff)
return str[:l] // strip 1 extra character we get from odd length results
}
func randChars(len int) []byte {
val := make([]byte, len)
if _, err := rand.Read(val); err != nil {
fmt.Println(err)
}
return val
}
func SessionRandB64(len int) string {
val := randChars(len)
res := base64.StdEncoding.EncodeToString(val)
return res
}