{"record":{"id":"d5d037ab5d962816","repo":"TheAlgorithms/Go","slug":"missing-key","errorCode":null,"errorMessage":"missing Key","messagePattern":"missing Key","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cipher/transposition/transposition.go","lineNumber":20,"sourceCode":"// description: Transposition cipher\n// details:\n// Implementation \"Transposition cipher\" is a method of encryption by which the positions held by units of plaintext (which are commonly characters or groups of characters) are shifted according to a regular system, so that the ciphertext constitutes a permutation of the plaintext [Transposition cipher](https://en.wikipedia.org/wiki/Transposition_cipher)\n// time complexity: O(n)\n// space complexity: O(n)\n// author(s) [red_byte](https://github.com/i-redbyte)\n// see transposition_test.go\n\npackage transposition\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\t\"sort\"\n\t\"strings\"\n)\n\nvar ErrNoTextToEncrypt = errors.New(\"no text to encrypt\")\nvar ErrKeyMissing = errors.New(\"missing Key\")\n\nconst placeholder = ' '\n\nfunc getKey(keyWord string) []int {\n\tkeyWord = strings.ToLower(keyWord)\n\tword := []rune(keyWord)\n\tvar sortedWord = make([]rune, len(word))\n\tcopy(sortedWord, word)\n\tsort.Slice(sortedWord, func(i, j int) bool { return sortedWord[i] < sortedWord[j] })\n\tusedLettersMap := make(map[rune]int)\n\twordLength := len(word)\n\tresultKey := make([]int, wordLength)\n\tfor i := 0; i < wordLength; i++ {\n\t\tchar := word[i]\n\t\tnumberOfUsage := usedLettersMap[char]\n\t\tresultKey[i] = getIndex(sortedWord, char) + numberOfUsage + 1 //+1 -so that indexing does not start at 0\n\t\tnumberOfUsage++\n\t\tusedLettersMap[char] = numberOfUsage","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/cipher/transposition/transposition.go#L2-L38","documentation":"ErrKeyMissing is returned by the transposition cipher when the key length is zero or negative, meaning no usable key was supplied. The key length drives the column permutation, so an empty key makes encryption/decryption impossible. It is returned before any text processing happens.","triggerScenarios":"Encrypt(key, text) or Decrypt(key, text) with an empty string key (keyLength == 0); Decrypt also returns it when the derived numeric key slice is empty.","commonSituations":"Config value for the cipher key left empty (missing env var, blank YAML field), or a key that is only whitespace producing an empty key word.","solutions":["Validate the key is a non-empty, non-whitespace string before calling Encrypt/Decrypt.","Load the key from config/env with an explicit empty check at startup.","Compare with errors.Is(err, transposition.ErrKeyMissing) and fail fast with a 'key required' configuration error."],"exampleFix":"// before\nresult, err := transposition.Encrypt(os.Getenv(\"CIPHER_KEY\"), text) // empty env var -> ErrKeyMissing\n// after\nkey := os.Getenv(\"CIPHER_KEY\")\nif len(strings.TrimSpace(key)) == 0 { return nil, errors.New(\"CIPHER_KEY must be set\") }\nresult, err := transposition.Encrypt(key, text)","handlingStrategy":"validation","validationCode":"func hasKey(key string) bool {\n    return len(strings.TrimSpace(key)) > 0\n}","typeGuard":null,"tryCatchPattern":"out, err := transposition.Encrypt(key, text)\nif errors.Is(err, transposition.ErrKeyMissing) {\n    return fmt.Errorf(\"transposition key missing: %w\", err)\n}","preventionTips":["Check the key at config load time and fail fast if empty.","Never pass raw env vars without an empty check.","Use keys of several characters, not single-char words."],"tags":["transposition","cipher","go","missing-key"],"backgroundTag":"missing-encryption-key","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}