Initial commit

This commit is contained in:
Ivan 2025-06-11 00:24:24 +05:00
commit f2788efb82
Signed by: landlord
GPG key ID: 416A03BC4ADA4CD7
9 changed files with 954 additions and 0 deletions

26
token.go Normal file
View 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
}