mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 04:50:52 +08:00
22 lines
562 B
Go
22 lines
562 B
Go
package misc
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"fmt"
|
|
)
|
|
|
|
// GenerateRandomState generates a cryptographically secure random state parameter
|
|
// for OAuth2 flows to prevent CSRF attacks.
|
|
//
|
|
// Returns:
|
|
// - string: A hexadecimal encoded random state string
|
|
// - error: An error if the random generation fails, nil otherwise
|
|
func GenerateRandomState() (string, error) {
|
|
bytes := make([]byte, 16)
|
|
if _, err := rand.Read(bytes); err != nil {
|
|
return "", fmt.Errorf("failed to generate random bytes: %w", err)
|
|
}
|
|
return hex.EncodeToString(bytes), nil
|
|
}
|