aboutsummaryrefslogtreecommitdiff
path: root/util/rand.go
blob: 1e4ec954ff1148924b1d2909a03290ca36bd14a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package util

import (
	"crypto/rand"
	"math/big"
)

var (
	runes        = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
	runes_length = len(runes)
)

func NewRandID(n int) (string, error) {
	data := make([]rune, n)
	for i := range data {
		num, err := rand.Int(rand.Reader, big.NewInt(int64(runes_length)))
		if err != nil {
			return "", err
		}
		data[i] = runes[num.Int64()]
	}
	return string(data), nil
}

func NewSessionID() (string, error) {
	return NewRandID(24)
}

func NewCSRFToken() (string, error) {
	return NewRandID(24)
}