rand.go 338 B

123456789101112131415161718192021
  1. package util
  2. import (
  3. "crypto/rand"
  4. "encoding/base64"
  5. )
  6. var enc = base64.URLEncoding
  7. func NewRandID(n int) (string, error) {
  8. data := make([]byte, enc.DecodedLen(n))
  9. _, err := rand.Read(data)
  10. if err != nil {
  11. return "", err
  12. }
  13. return enc.EncodeToString(data), nil
  14. }
  15. func NewCSRFToken() (string, error) {
  16. return NewRandID(24)
  17. }