util.go 535 B

123456789101112131415161718192021222324252627282930
  1. package util
  2. import (
  3. "crypto/rand"
  4. "encoding/hex"
  5. "fmt"
  6. )
  7. func GenPSK(length int) ([]byte, error) {
  8. b := make([]byte, length)
  9. _, err := rand.Read(b)
  10. if err != nil {
  11. return nil, fmt.Errorf("random bytes generation failed: %w", err)
  12. }
  13. return b, nil
  14. }
  15. func GenPSKHex(length int) (string, error) {
  16. b, err := GenPSK(length)
  17. if err != nil {
  18. return "", fmt.Errorf("can't generate hex key: %w", err)
  19. }
  20. return hex.EncodeToString(b), nil
  21. }
  22. func PSKFromHex(input string) ([]byte, error) {
  23. return hex.DecodeString(input)
  24. }