padding.go 729 B

123456789101112131415161718192021222324
  1. package utils
  2. import (
  3. "math/rand/v2"
  4. )
  5. var (
  6. // 8 ÷ (397/62)
  7. h2packCorrectionFactor = 1.2493702770780857
  8. base62TotalCharsNum = 62
  9. base62Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  10. )
  11. // H2Base62Pad generates a base62 padding string for HTTP/2 header
  12. // The total len will be slightly longer than the input to match the length after h2(h3 also) header huffman encoding
  13. func H2Base62Pad[T int32 | int64 | int](expectedLen T) string {
  14. actualLenFloat := float64(expectedLen) * h2packCorrectionFactor
  15. actualLen := int(actualLenFloat)
  16. result := make([]byte, actualLen)
  17. for i := range actualLen {
  18. result[i] = base62Chars[rand.N(base62TotalCharsNum)]
  19. }
  20. return string(result)
  21. }