Initial commit
This commit is contained in:
commit
f2788efb82
9 changed files with 954 additions and 0 deletions
26
token.go
Normal file
26
token.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
package csrf
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
)
|
||||
|
||||
const (
|
||||
alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
)
|
||||
|
||||
// Generate a new, cryptographically-secure, URL-safe token.
|
||||
func generateCSRFToken(tokenLength uint) (string, error) {
|
||||
arr := make([]byte, tokenLength)
|
||||
alphabetLength := byte(len(alphabet))
|
||||
|
||||
_, err := rand.Read(arr)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
for i := range arr {
|
||||
arr[i] = alphabet[arr[i]%alphabetLength]
|
||||
}
|
||||
|
||||
return string(arr), nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue