go-twitch/auth/state.go

25 lines
472 B
Go
Raw Permalink Normal View History

2024-03-04 18:14:38 -05:00
package auth
import (
"crypto/rand"
"math/big"
mrand "math/rand"
)
const (
stateLength = 32
stateChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"
)
func GenerateState() string {
b := make([]byte, stateLength)
for i := range b {
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(stateChars))))
if err != nil {
num = big.NewInt(mrand.Int63n(int64(len(stateChars))))
}
b[i] = stateChars[num.Int64()]
}
return string(b)
}