rand.go 402 Bytes
Newer Older
Simao Gomes Viana's avatar
Simao Gomes Viana committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
package injection

import (
	"crypto/rand"
	"encoding/base32"
)


func GenerateRandomBytes(len int) ([]byte, error) {
	b := make([]byte, len)
	_, err := rand.Read(b)
	if err != nil {
		return nil, err
	}

	return b, nil
}

func GenerateRandomStringURLSafe(len int) (string, error) {
	b, err := GenerateRandomBytes(len)
	return base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(b), err
}