{"record":{"id":"d8aab38c2a1968f6","repo":"TheAlgorithms/Go","slug":"len-key-d-must-be-as-long-as-size-squared-d","errorCode":null,"errorMessage":"len(key): %d must be as long as size squared: %d","messagePattern":"len\\(key\\): (.+?) must be as long as size squared: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cipher/polybius/polybius.go","lineNumber":46,"sourceCode":"\t}\n\tkey = strings.ToUpper(key)\n\tif size > len(chars) {\n\t\treturn nil, fmt.Errorf(\"provided size %d is too small to use to slice string %q of len %d\", size, chars, len(chars))\n\t}\n\tfor _, r := range chars {\n\t\tif (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') {\n\t\t\treturn nil, fmt.Errorf(\"provided string %q should only contain latin characters\", chars)\n\t\t}\n\t}\n\tchars = strings.ToUpper(chars)[:size]\n\tfor i, r := range chars {\n\t\tif strings.ContainsRune(chars[i+1:], r) {\n\t\t\treturn nil, fmt.Errorf(\"%q contains same character %q\", chars[i+1:], r)\n\t\t}\n\t}\n\n\tif len(key) != size*size {\n\t\treturn nil, fmt.Errorf(\"len(key): %d must be as long as size squared: %d\", len(key), size*size)\n\t}\n\treturn &Polybius{size, chars, key}, nil\n}\n\n// Encrypt encrypts with polybius encryption\nfunc (p *Polybius) Encrypt(text string) (string, error) {\n\tencryptedText := \"\"\n\tfor _, char := range strings.ToUpper(text) {\n\t\tencryptedChar, err := p.encipher(char)\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"failed encipher: %w\", err)\n\t\t}\n\t\tencryptedText += encryptedChar\n\t}\n\treturn encryptedText, nil\n}\n\n// Decrypt decrypts with polybius encryption","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/cipher/polybius/polybius.go#L28-L64","documentation":"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.","triggerScenarios":"Thrown at cipher/polybius/polybius.go:46 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","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"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}