-
-
Notifications
You must be signed in to change notification settings - Fork 150
Sync simple cipher #1025
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Sync simple cipher #1025
Changes from all commits
9734db8
d233261
fa8a1a1
cf210cf
fdadef0
4507f0f
a898747
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -84,6 +84,7 @@ scrabble-score | |
| secret-handshake | ||
| series | ||
| sieve | ||
| simple-cipher | ||
| space-age | ||
| spiral-matrix | ||
| square-root | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # This is an auto-generated file. | ||
| # | ||
| # Regenerating this file via `configlet sync` will: | ||
| # - Recreate every `description` key/value pair | ||
| # - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
| # - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
| # - Preserve any other key/value pair | ||
| # | ||
| # As user-added comments (using the # character) will be removed when this file | ||
| # is regenerated, comments can be added via a `comment` key. | ||
|
|
||
| [b8bdfbe1-bea3-41bb-a999-b41403f2b15d] | ||
| description = "Random key cipher -> Can encode" | ||
|
|
||
| [3dff7f36-75db-46b4-ab70-644b3f38b81c] | ||
| description = "Random key cipher -> Can decode" | ||
|
|
||
| [8143c684-6df6-46ba-bd1f-dea8fcb5d265] | ||
| description = "Random key cipher -> Is reversible. I.e., if you apply decode in a encoded result, you must see the same plaintext encode parameter as a result of the decode method" | ||
|
|
||
| [defc0050-e87d-4840-85e4-51a1ab9dd6aa] | ||
| description = "Random key cipher -> Key is made only of lowercase letters" | ||
|
|
||
| [565e5158-5b3b-41dd-b99d-33b9f413c39f] | ||
| description = "Substitution cipher -> Can encode" | ||
|
|
||
| [d44e4f6a-b8af-4e90-9d08-fd407e31e67b] | ||
| description = "Substitution cipher -> Can decode" | ||
|
|
||
| [70a16473-7339-43df-902d-93408c69e9d1] | ||
| description = "Substitution cipher -> Is reversible. I.e., if you apply decode in a encoded result, you must see the same plaintext encode parameter as a result of the decode method" | ||
|
|
||
| [69a1458b-92a6-433a-a02d-7beac3ea91f9] | ||
| description = "Substitution cipher -> Can double shift encode" | ||
|
|
||
| [21d207c1-98de-40aa-994f-86197ae230fb] | ||
| description = "Substitution cipher -> Can wrap on encode" | ||
|
|
||
| [a3d7a4d7-24a9-4de6-bdc4-a6614ced0cb3] | ||
| description = "Substitution cipher -> Can wrap on decode" | ||
|
|
||
| [e31c9b8c-8eb6-45c9-a4b5-8344a36b9641] | ||
| description = "Substitution cipher -> Can encode messages longer than the key" | ||
|
|
||
| [93cfaae0-17da-4627-9a04-d6d1e1be52e3] | ||
| description = "Substitution cipher -> Can decode messages longer than the key" |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,29 +1,8 @@ | ||||||||||||||||||||||
| <?php | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /* | ||||||||||||||||||||||
| * By adding type hints and enabling strict type checking, code can become | ||||||||||||||||||||||
| * easier to read, self-documenting and reduce the number of potential bugs. | ||||||||||||||||||||||
| * By default, type declarations are non-strict, which means they will attempt | ||||||||||||||||||||||
| * to change the original type to match the type specified by the | ||||||||||||||||||||||
| * type-declaration. | ||||||||||||||||||||||
| * | ||||||||||||||||||||||
| * In other words, if you pass a string to a function requiring a float, | ||||||||||||||||||||||
| * it will attempt to convert the string value to a float. | ||||||||||||||||||||||
| * | ||||||||||||||||||||||
| * To enable strict mode, a single declare directive must be placed at the top | ||||||||||||||||||||||
| * of the file. | ||||||||||||||||||||||
| * This means that the strictness of typing is configured on a per-file basis. | ||||||||||||||||||||||
| * This directive not only affects the type declarations of parameters, but also | ||||||||||||||||||||||
| * a function's return type. | ||||||||||||||||||||||
| * | ||||||||||||||||||||||
| * For more info review the Concept on strict type checking in the PHP track | ||||||||||||||||||||||
| * <link>. | ||||||||||||||||||||||
| * | ||||||||||||||||||||||
| * To disable strict typing, comment out the directive below. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| declare(strict_types=1); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| use PHPUnit\Framework\Attributes\TestDox; | ||||||||||||||||||||||
| use PHPUnit\Framework\TestCase; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| class SimpleCipherTest extends TestCase | ||||||||||||||||||||||
|
|
@@ -33,62 +12,57 @@ public static function setUpBeforeClass(): void | |||||||||||||||||||||
| require_once 'SimpleCipher.php'; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function testRandomCipherKeyIsLetters(): void | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| $cipher = new SimpleCipher(); | ||||||||||||||||||||||
| $this->assertMatchesRegularExpression('/\A[a-z]+\z/', $cipher->key); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Here we take advantage of the fact that plaintext of "aaa..." doesn't | ||||||||||||||||||||||
| * output the key. This is a critical problem with shift ciphers, some | ||||||||||||||||||||||
| * characters will always output the key verbatim. | ||||||||||||||||||||||
| * | ||||||||||||||||||||||
| * Uuid: b8bdfbe1-bea3-41bb-a999-b41403f2b15d. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| #[TestDox('Random key cipher -> Can encode')] | ||||||||||||||||||||||
| public function testRandomKeyCipherEncode(): void | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please adjust the method names to match the TestDox sentence.
Suggested change
|
||||||||||||||||||||||
| { | ||||||||||||||||||||||
| $cipher = new SimpleCipher(); | ||||||||||||||||||||||
| $plaintext = 'aaaaaaaaaa'; | ||||||||||||||||||||||
| $this->assertEquals(substr($cipher->key, 0, 10), $cipher->encode($plaintext)); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Uuid: 3dff7f36-75db-46b4-ab70-644b3f38b81c. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
|
Comment on lines
+30
to
+32
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
| #[TestDox('Random key cipher -> Can decode')] | ||||||||||||||||||||||
| public function testRandomKeyCipherDecode(): void | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| $cipher = new SimpleCipher(); | ||||||||||||||||||||||
| $plaintext = 'aaaaaaaaaa'; | ||||||||||||||||||||||
| $this->assertEquals($plaintext, $cipher->decode(substr($cipher->key, 0, 10))); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Uuid: 8143c684-6df6-46ba-bd1f-dea8fcb5d265. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| #[TestDox('Random key cipher -> Is reversible. I.e., if you apply decode in a encoded result, you must see the same plaintext encode parameter as a result of the decode method')] | ||||||||||||||||||||||
| public function testRandomKeyCipherReversible(): void | ||||||||||||||||||||||
|
Comment on lines
+41
to
45
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a real special case. The example should not be in the TestDox.
Suggested change
|
||||||||||||||||||||||
| { | ||||||||||||||||||||||
| $cipher = new SimpleCipher(); | ||||||||||||||||||||||
| $plaintext = 'abcdefghij'; | ||||||||||||||||||||||
| $this->assertEquals($plaintext, $cipher->decode($cipher->encode($plaintext))); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function testCipherWithCapsKey(): void | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| $this->expectException(InvalidArgumentException::class); | ||||||||||||||||||||||
| $cipher = new SimpleCipher('ABCDEF'); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function testCipherWithNumericKey(): void | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| $this->expectException(InvalidArgumentException::class); | ||||||||||||||||||||||
| $cipher = new SimpleCipher('12345'); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function testCipherWithEmptyKey(): void | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| $this->expectException(InvalidArgumentException::class); | ||||||||||||||||||||||
| $cipher = new SimpleCipher(''); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function testCipherKeyIsAsSubmitted(): void | ||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Uuid: defc0050-e87d-4840-85e4-51a1ab9dd6aa. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
|
Comment on lines
+52
to
+54
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please go through all DocBlocks and fix the indentation. 4 spaces on the comment starting line, then 5 spaces until the comment ends. That's the rule. And no dot after the UUID.
Suggested change
|
||||||||||||||||||||||
| #[TestDox('Random key cipher -> Key is made only of lowercase letters')] | ||||||||||||||||||||||
| public function testRandomCipherKeyIsLetters(): void | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| $cipher = new SimpleCipher('abcdefghij'); | ||||||||||||||||||||||
| $this->assertEquals($cipher->key, 'abcdefghij'); | ||||||||||||||||||||||
| $cipher = new SimpleCipher(); | ||||||||||||||||||||||
| $this->assertMatchesRegularExpression('/\A[a-z]+\z/', $cipher->key); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Uuid: 565e5158-5b3b-41dd-b99d-33b9f413c39f. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| #[TestDox('Substitution cipher -> Can encode')] | ||||||||||||||||||||||
| public function testCipherEncode(): void | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| $cipher = new SimpleCipher('abcdefghij'); | ||||||||||||||||||||||
|
|
@@ -97,6 +71,10 @@ public function testCipherEncode(): void | |||||||||||||||||||||
| $this->assertEquals($ciphertext, $cipher->encode($plaintext)); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Uuid: d44e4f6a-b8af-4e90-9d08-fd407e31e67b. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| #[TestDox('Substitution cipher -> Can decode')] | ||||||||||||||||||||||
| public function testCipherDecode(): void | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| $cipher = new SimpleCipher('abcdefghij'); | ||||||||||||||||||||||
|
|
@@ -105,13 +83,21 @@ public function testCipherDecode(): void | |||||||||||||||||||||
| $this->assertEquals($plaintext, $cipher->decode($ciphertext)); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Uuid: 70a16473-7339-43df-902d-93408c69e9d1. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| #[TestDox('Substitution cipher -> Is reversible. I.e., if you apply decode in a encoded result, you must see the same plaintext encode parameter as a result of the decode method')] | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, it's better to skip the example here:
Suggested change
|
||||||||||||||||||||||
| public function testCipherReversible(): void | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| $cipher = new SimpleCipher('abcdefghij'); | ||||||||||||||||||||||
| $plaintext = 'abcdefghij'; | ||||||||||||||||||||||
| $this->assertEquals($plaintext, $cipher->decode($cipher->encode($plaintext))); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Uuid: 69a1458b-92a6-433a-a02d-7beac3ea91f9. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| #[TestDox('Substitution cipher -> Can double shift encode')] | ||||||||||||||||||||||
| public function testDoubleShiftEncode(): void | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| $cipher = new SimpleCipher('iamapandabear'); | ||||||||||||||||||||||
|
|
@@ -120,6 +106,10 @@ public function testDoubleShiftEncode(): void | |||||||||||||||||||||
| $this->assertEquals($ciphertext, $cipher->encode($plaintext)); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Uuid: 21d207c1-98de-40aa-994f-86197ae230fb. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| #[TestDox('Substitution cipher -> Can wrap on encode')] | ||||||||||||||||||||||
| public function testCipherEncodeWrap(): void | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| $cipher = new SimpleCipher('abcdefghij'); | ||||||||||||||||||||||
|
|
@@ -128,26 +118,39 @@ public function testCipherEncodeWrap(): void | |||||||||||||||||||||
| $this->assertEquals($ciphertext, $cipher->encode($plaintext)); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function testShiftCipherEncode(): void | ||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Uuid: a3d7a4d7-24a9-4de6-bdc4-a6614ced0cb3. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| #[TestDox('Substitution cipher -> Can wrap on decode')] | ||||||||||||||||||||||
| public function testCipherDecodeWrap(): void | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| $cipher = new SimpleCipher('dddddddddd'); | ||||||||||||||||||||||
| $plaintext = 'aaaaaaaaaa'; | ||||||||||||||||||||||
| $ciphertext = 'dddddddddd'; | ||||||||||||||||||||||
| $this->assertEquals($ciphertext, $cipher->encode($plaintext)); | ||||||||||||||||||||||
| $cipher = new SimpleCipher('abcdefghij'); | ||||||||||||||||||||||
| $plaintext = 'zzzzzzzzzz'; | ||||||||||||||||||||||
| $ciphertext = 'zabcdefghi'; | ||||||||||||||||||||||
| $this->assertEquals($plaintext, $cipher->decode($ciphertext)); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function testShiftCipherDecode(): void | ||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Uuid: e31c9b8c-8eb6-45c9-a4b5-8344a36b9641. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| #[TestDox('Substitution cipher -> Can encode messages longer than the key')] | ||||||||||||||||||||||
| public function testCanEncodeMessageLongerThanKey(): void | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| $cipher = new SimpleCipher('dddddddddd'); | ||||||||||||||||||||||
| $plaintext = 'aaaaaaaaaa'; | ||||||||||||||||||||||
| $ciphertext = 'dddddddddd'; | ||||||||||||||||||||||
| $this->assertEquals($plaintext, $cipher->decode($ciphertext)); | ||||||||||||||||||||||
| $cipher = new SimpleCipher('abc'); | ||||||||||||||||||||||
| $cipherText = 'iboaqcnecbfcr'; | ||||||||||||||||||||||
| $plainText = 'iamapandabear'; | ||||||||||||||||||||||
| $this->assertEquals($cipherText, $cipher->encode($plainText)); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public function testShiftCipherReversible(): void | ||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Uuid: 93cfaae0-17da-4627-9a04-d6d1e1be52e3. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| #[TestDox('Substitution cipher -> Can decode messages longer than the key')] | ||||||||||||||||||||||
| public function testCanDecodeMessageLongerThanKey(): void | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| $cipher = new SimpleCipher('dddddddddd'); | ||||||||||||||||||||||
| $plaintext = 'abcdefghij'; | ||||||||||||||||||||||
| $this->assertEquals($plaintext, $cipher->decode($cipher->encode($plaintext))); | ||||||||||||||||||||||
| $cipher = new SimpleCipher('abc'); | ||||||||||||||||||||||
| $cipherText = 'iboaqcnecbfcr'; | ||||||||||||||||||||||
| $plainText = 'iamapandabear'; | ||||||||||||||||||||||
| $this->assertEquals($plainText, $cipher->decode($cipherText)); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not add a dot at the end of the UUIDs: