Skip to content

fix: actually reject KeywordShiftedAlphabet keywords with repeated characters - #2037

Open
Aditya2375 wants to merge 2 commits into
TheAlgorithms:masterfrom
Aditya2375:fix-keyword-shifted-alphabet-validation
Open

Aditya2375 wants to merge 2 commits into
TheAlgorithms:masterfrom
Aditya2375:fix-keyword-shifted-alphabet-validation

Conversation

@Aditya2375

Copy link
Copy Markdown

Open in Gitpod 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}.

…aracters

checkKeywordValidity returned false inside a forEach callback, which only exits the callback, so it always returned true and repeated-character keywords were accepted. With such a keyword the translation table is not a bijection and encryption loses information: encrypt('HELLO', 'ATTACK AT DAWN') gave 'HSSHLG HS LHVK', which decrypted to 'ATTACK AT CAWN'. Validate the lowercased keyword with every() so each character must be the first occurrence of itself; this also closes the 'Kk' case hole.
Regression tests for the validation fix: 'HELLO' (repeated L) must throw on encrypt and decrypt, and 'Kk' must throw because the translation table is built from the lowercased keyword.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.93%. Comparing base (5c39e87) to head (8d2cffc).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2037      +/-   ##
==========================================
+ Coverage   85.91%   85.93%   +0.01%     
==========================================
  Files         379      379              
  Lines       19778    19777       -1     
  Branches     3016     3015       -1     
==========================================
+ Hits        16993    16996       +3     
+ Misses       2785     2781       -4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants