TheAlgorithms/Go · error
len(key): %d must be as long as size squared: %d
Error message
len(key): %d must be as long as size squared: %d
What it means
Constructor validation error from NewPolybius: the provided key length does not equal size*size, meaning there are not enough key characters to fill every cell of the square Polybius grid the constructor builds.
Source
Thrown at cipher/polybius/polybius.go:46
}
key = strings.ToUpper(key)
if size > len(chars) {
return nil, fmt.Errorf("provided size %d is too small to use to slice string %q of len %d", size, chars, len(chars))
}
for _, r := range chars {
if (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') {
return nil, fmt.Errorf("provided string %q should only contain latin characters", chars)
}
}
chars = strings.ToUpper(chars)[:size]
for i, r := range chars {
if strings.ContainsRune(chars[i+1:], r) {
return nil, fmt.Errorf("%q contains same character %q", chars[i+1:], r)
}
}
if len(key) != size*size {
return nil, fmt.Errorf("len(key): %d must be as long as size squared: %d", len(key), size*size)
}
return &Polybius{size, chars, key}, nil
}
// Encrypt encrypts with polybius encryption
func (p *Polybius) Encrypt(text string) (string, error) {
encryptedText := ""
for _, char := range strings.ToUpper(text) {
encryptedChar, err := p.encipher(char)
if err != nil {
return "", fmt.Errorf("failed encipher: %w", err)
}
encryptedText += encryptedChar
}
return encryptedText, nil
}
// Decrypt decrypts with polybius encryptionView on GitHub (pinned to 5ba447ec5f)
Solutions
- Pad or trim the key to exactly size*size characters before construction
- Validate key length at the API boundary and surface the required length
- Generate a key of the correct length from a passphrase
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at cipher/polybius/polybius.go:46 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02).
Data as JSON: /api/errors/d8aab38c2a1968f6.
Report an issue: GitHub.