на оценку 5

This commit is contained in:
2026-11-10 16:21:46 +03:00
parent 276dbdad8a
commit 7110afc598
170 changed files with 21149 additions and 30 deletions
+32
View File
@@ -0,0 +1,32 @@
package security
import (
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"fmt"
)
const sessionTokenBytes = 32
func GenerateSessionToken() (string, []byte, error) {
randomBytes := make([]byte, sessionTokenBytes)
if _, err := rand.Read(randomBytes); err != nil {
return "", nil, fmt.Errorf("generate session token: %w", err)
}
token := base64.RawURLEncoding.EncodeToString(randomBytes)
tokenHash := HashSessionToken(token)
return token, tokenHash, nil
}
func HashSessionToken(token string) []byte {
digest := sha256.Sum256([]byte(token))
hash := make([]byte, len(digest))
copy(hash, digest[:])
return hash
}