Title: fix: actually reject KeywordShiftedAlphabet keywords with repeated characters by Aditya2375 · Pull Request #2037 · TheAlgorithms/JavaScript · GitHub
Open Graph Title: fix: actually reject KeywordShiftedAlphabet keywords with repeated characters by Aditya2375 · Pull Request #2037 · TheAlgorithms/JavaScript
X Title: fix: actually reject KeywordShiftedAlphabet keywords with repeated characters by Aditya2375 · Pull Request #2037 · TheAlgorithms/JavaScript
Description: know more Describe your change: Add an algorithm? Fix a bug or typo in an existing algorithm? Documentation change? Bug: checkKeywordValidity in Ciphers/KeywordShiftedAlphabet.js is a no-op: it calls return false inside a forEach callback, which only exits the callback, so the function always returns true. Keywords with repeated characters - which the file header explicitly forbids ("Keyword must be a word where each character can occur only once") - are accepted, and checkInputs never throws its 'Invalid keyword!' error. With a repeated-character keyword the translation table is no longer a bijection, so encryption silently loses information: encrypt('HELLO', 'ATTACK AT DAWN') // 'HSSHLG HS LHVK' (no error) decrypt('HELLO', 'HSSHLG HS LHVK') // 'ATTACK AT CAWN' - D became C There is also a case hole: 'Kk' passes the check, but the table is built from the lowercased keyword, where the two characters collapse to a duplicate. Fix: validate the lowercased keyword and use every (which can actually return early), so each character must be the first occurrence of itself: function checkKeywordValidity(keyword) { const chars = keyword.toLowerCase().split('') return chars.every((char, index) => chars.indexOf(char) === index) } Tests: two new tests in Ciphers/test/KeywordShiftedAlphabet.test.js: reject 'HELLO' on both encrypt and decrypt, and reject 'Kk'. All 4 tests in the file pass locally (vitest); the two new tests fail on the unpatched code. The two pre-existing round-trip tests are unaffected. Checklist: I have read CONTRIBUTING.md. This pull request is all my own work -- I have not plagiarized. I know that pull requests will not be merged if they fail the automated tests. This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms. All new JavaScript files are placed inside an existing directory. All filenames should use the UpperCamelCase (PascalCase) style. There should be no spaces in filenames. Example:UserProfile.js is allowed but userprofile.js,Userprofile.js,user-Profile.js,userProfile.js are not All new algorithms have a URL in their comments that points to Wikipedia or another similar explanation. If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.
Open Graph Description: know more Describe your change: Add an algorithm? Fix a bug or typo in an existing algorithm? Documentation change? Bug: checkKeywordValidity in Ciphers/KeywordShiftedAlphabet.js is a no-op: ...
X Description: know more Describe your change: Add an algorithm? Fix a bug or typo in an existing algorithm? Documentation change? Bug: checkKeywordValidity in Ciphers/KeywordShiftedAlphabet.js is a no-op: ...
Opengraph URL: https://github.com/TheAlgorithms/JavaScript/pull/2037
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:e1539162-9241-2fa6-293f-9159ad6824e7 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | A372:0DF6:1D14845:26934C1:6B068D76 |
| html-safe-nonce | fe4a4ccd09f69bc44a40d8998dbc0e71b1376ff004e67259b24c71d1c8b47da4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMzcyOjBERjY6MUQxNDg0NToyNjkzNEMxOjZCMDY4RDc2IiwidmlzaXRvcl9pZCI6IjY3Nzk1NzE4MTk0NDA5MzQyNjMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 8a8c054f327ceda33480b032cdd2dc623a1f1f381ec2f38008ae548d16562e1c |
| hovercard-subject-tag | pull_request:5472085908 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/TheAlgorithms/JavaScript/pull/2037/files |
| twitter:image | https://avatars.githubusercontent.com/u/213192267?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/213192267?s=400&v=4 |
| og:image:alt | know more Describe your change: Add an algorithm? Fix a bug or typo in an existing algorithm? Documentation change? Bug: checkKeywordValidity in Ciphers/KeywordShiftedAlphabet.js is a no-op: ... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4462ff7ebfd3d551f202659f2423dae617b6849d23b1df6465dc504ef497d14a |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/TheAlgorithms/JavaScript git https://github.com/TheAlgorithms/JavaScript.git |
| octolytics-dimension-user_id | 20487725 |
| octolytics-dimension-user_login | TheAlgorithms |
| octolytics-dimension-repository_id | 97086543 |
| octolytics-dimension-repository_nwo | TheAlgorithms/JavaScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 97086543 |
| octolytics-dimension-repository_network_root_nwo | TheAlgorithms/JavaScript |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 6d3909414814ff86ce7da4c51a0f4918a13fb41b |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | no-cache |
Viewport: width=device-width