Home / Function/ unsafeRandString() — fiber Function Reference

unsafeRandString() — fiber Function Reference

Architecture documentation for the unsafeRandString() function in hooks.go from the fiber codebase.

Entity Profile

Dependency Diagram

graph TD
  3ab0b2e2_48f6_4b95_606e_02e49d3669ca["unsafeRandString()"]
  14cfc1be_1c8f_085d_1a4e_f0de9527aaba["hooks.go"]
  3ab0b2e2_48f6_4b95_606e_02e49d3669ca -->|defined in| 14cfc1be_1c8f_085d_1a4e_f0de9527aaba
  7f2499fb_dfce_29cc_8d1f_64b8c4c1e120["parserRequestHeader()"]
  7f2499fb_dfce_29cc_8d1f_64b8c4c1e120 -->|calls| 3ab0b2e2_48f6_4b95_606e_02e49d3669ca
  style 3ab0b2e2_48f6_4b95_606e_02e49d3669ca fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

client/hooks.go lines 41–68

func unsafeRandString(n int) (string, error) {
	inputLength := byte(len(letterBytes))

	// Compute the largest multiple of inputLength ≤ 256 to avoid modulo bias.
	// Any byte ≥ max will be rejected and re‑read.
	maxLength := byte(256 - (256 % int(inputLength)))

	out := make([]byte, n)
	buf := make([]byte, n)

	// Read n raw bytes in one shot
	if _, err := rand.Read(buf); err != nil {
		return "", fmt.Errorf("rand.Read failed: %w", err)
	}

	for i, b := range buf {
		// Reject values ≥ maxLength
		for b >= maxLength {
			if _, err := rand.Read(buf[i : i+1]); err != nil {
				return "", fmt.Errorf("rand.Read failed: %w", err)
			}
			b = buf[i]
		}
		out[i] = letterBytes[b%inputLength]
	}

	return utils.UnsafeString(out), nil
}

Domain

Subdomains

Defined In

Frequently Asked Questions

What does unsafeRandString() do?
unsafeRandString() is a function in the fiber codebase, defined in client/hooks.go.
Where is unsafeRandString() defined?
unsafeRandString() is defined in client/hooks.go at line 41.
What calls unsafeRandString()?
unsafeRandString() is called by 1 function(s): parserRequestHeader.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free