Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@
"prerequisites": [],
"difficulty": 1
},
{
"slug": "space-age",
"name": "Space Age",
"uuid": "031f2fe0-e8b1-48c2-8ce3-f9ab8b76423c",
"practices": [],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "two-fer",
"name": "Two Fer",
Expand Down Expand Up @@ -311,12 +319,12 @@
"difficulty": 2
},
{
"slug": "space-age",
"name": "Space Age",
"uuid": "031f2fe0-e8b1-48c2-8ce3-f9ab8b76423c",
"slug": "simple-cipher",
"name": "Simple Cipher",
"uuid": "d8ab5e48-f9b3-4016-a576-c14f361d2db1",
"practices": [],
"prerequisites": [],
"difficulty": 1
"difficulty": 2
},
{
"slug": "triangle",
Expand Down
40 changes: 40 additions & 0 deletions exercises/practice/simple-cipher/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Instructions

Create an implementation of the [Vigenère cipher][wiki].
The Vigenère cipher is a simple substitution cipher.

## Cipher terminology

A cipher is an algorithm used to encrypt, or encode, a string.
The unencrypted string is called the _plaintext_ and the encrypted string is called the _ciphertext_.
Converting plaintext to ciphertext is called _encoding_ while the reverse is called _decoding_.

In a _substitution cipher_, each plaintext letter is replaced with a ciphertext letter which is computed with the help of a _key_.
(Note, it is possible for replacement letter to be the same as the original letter.)

## Encoding details

In this cipher, the key is a series of lowercase letters, such as `"abcd"`.
Each letter of the plaintext is _shifted_ or _rotated_ by a distance based on a corresponding letter in the key.
An `"a"` in the key means a shift of 0 (that is, no shift).
A `"b"` in the key means a shift of 1.
A `"c"` in the key means a shift of 2, and so on.

The first letter of the plaintext uses the first letter of the key, the second letter of the plaintext uses the second letter of the key and so on.
If you run out of letters in the key before you run out of letters in the plaintext, start over from the start of the key again.

If the key only contains one letter, such as `"dddddd"`, then all letters of the plaintext are shifted by the same amount (three in this example), which would make this the same as a rotational cipher or shift cipher (sometimes called a Caesar cipher).
For example, the plaintext `"iamapandabear"` would become `"ldpdsdqgdehdu"`.

If the key only contains the letter `"a"` (one or more times), the shift distance is zero and the ciphertext is the same as the plaintext.

Usually the key is more complicated than that, though!
If the key is `"abcd"` then letters of the plaintext would be shifted by a distance of 0, 1, 2, and 3.
If the plaintext is `"hello"`, we need 5 shifts so the key would wrap around, giving shift distances of 0, 1, 2, 3, and 0.
Applying those shifts to the letters of `"hello"` we get `"hfnoo"`.

## Random keys

If no key is provided, generate a key which consists of at least 100 random lowercase letters from the Latin alphabet.

[wiki]: https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher
19 changes: 19 additions & 0 deletions exercises/practice/simple-cipher/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"rmonnet"
],
"files": {
"solution": [
"simple_cipher.odin"
],
"test": [
"simple_cipher_test.odin"
],
"example": [
".meta/example.odin"
]
},
"blurb": "Implement the Vigenère cipher, a simple substitution cipher.",
"source": "Substitution Cipher at Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Substitution_cipher"
}
40 changes: 40 additions & 0 deletions exercises/practice/simple-cipher/.meta/example.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package simple_cipher

import "core:math/rand"

decode :: proc(ciphertext, key: string) -> string {

plaintext := make([]byte, len(ciphertext))
for i := 0; i < len(ciphertext); i += 1 {
shift := key[i % len(key)] - 'a'
plaintext[i] = ciphertext[i] - shift
// If we go before 'a', start again at 'z'.
if plaintext[i] < 'a' {
plaintext[i] += 26
}
}
return string(plaintext)
}

encode :: proc(plaintext, key: string) -> string {

ciphertext := make([]byte, len(plaintext))
for i := 0; i < len(plaintext); i += 1 {
shift := key[i % len(key)] - 'a'
ciphertext[i] = plaintext[i] + shift
// If we go past 'z', start again at 'a'.
if ciphertext[i] > 'z' {
ciphertext[i] -= 26
}
}
return string(ciphertext)
}

key :: proc() -> string {

key_bytes := make([]byte, 100)
for i := 0; i < len(key_bytes); i += 1 {
key_bytes[i] = u8(rand.int_max(26)) + 'a'
}
return string(key_bytes)
}
46 changes: 46 additions & 0 deletions exercises/practice/simple-cipher/.meta/tests.toml
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"
18 changes: 18 additions & 0 deletions exercises/practice/simple-cipher/simple_cipher.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package simple_cipher

import "core:math/rand"

decode :: proc(ciphertext, key: string) -> string {
// Implement the procedure.
return ""
}

encode :: proc(plaintext, key: string) -> string {
// Implement the procedure.
return ""
}

key :: proc() -> string {
// Implement the procedure.
return ""
}
190 changes: 190 additions & 0 deletions exercises/practice/simple-cipher/simple_cipher_test.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package simple_cipher

import "core:strings"
import "core:testing"

@(test)
/// description = Random key cipher -> Can encode
test_random_key_cipher___can_encode :: proc(t: ^testing.T) {

plaintext := "aaaaaaaaaa"
random_key := key()
result := encode(plaintext, random_key)
expected, _ := strings.substring(random_key, 0, len(plaintext))
defer {
delete(random_key)
delete(result)
}

testing.expectf(
t,
len(random_key) >= 100,
"key() returns key of less than 100 characters (%d)",
len(random_key),
)
testing.expect_value(t, result, expected)
}

@(test)
/// description = Random key cipher -> Can decode
test_random_key_cipher___can_decode :: proc(t: ^testing.T) {

random_key := key()
expected := "aaaaaaaaaa"
ciphertext, _ := strings.substring(random_key, 0, len(expected))
result := decode(ciphertext, random_key)
defer {
delete(random_key)
delete(result)
}

testing.expectf(
t,
len(random_key) >= 100,
"key() returns key of less than 100 characters (%d)",
len(random_key),
)
testing.expect_value(t, result, expected)
}

@(test)
/// 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
test_random_key_cipher___is_reversible_ie_if_you_apply_decode_in_a_encoded_result_you_must_see_the_same_plaintext_encode_parameter_as_a_result_of_the_decode_method :: proc(
t: ^testing.T,
) {

random_key := key()
plaintext := "abcdefghij"
ciphertext := encode(plaintext, random_key)
result := decode(ciphertext, random_key)
defer {
delete(random_key)
delete(ciphertext)
delete(result)
}

testing.expect_value(t, result, plaintext)
}

@(test)
/// description = Random key cipher -> Key is made only of lowercase letters
test_random_key_cipher___key_is_made_only_of_lowercase_letters :: proc(t: ^testing.T) {

random_key := key()
expected := strings.to_lower(random_key)
defer {
delete(random_key)
delete(expected)
}

testing.expect_value(t, random_key, expected)
}

@(test)
/// description = Substitution cipher -> Can encode
test_substitution_cipher___can_encode :: proc(t: ^testing.T) {

key := "abcdefghij"
plaintext := "aaaaaaaaaa"
result := encode(plaintext, key)
expected := "abcdefghij"
defer delete(result)

testing.expect_value(t, result, expected)
}

@(test)
/// description = Substitution cipher -> Can decode
test_substitution_cipher___can_decode :: proc(t: ^testing.T) {

key := "abcdefghij"
ciphertext := "abcdefghij"
result := decode(ciphertext, key)
expected := "aaaaaaaaaa"
defer delete(result)

testing.expect_value(t, result, expected)
}

@(test)
/// 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
test_substitution_cipher___is_reversible_ie_if_you_apply_decode_in_a_encoded_result_you_must_see_the_same_plaintext_encode_parameter_as_a_result_of_the_decode_method :: proc(
t: ^testing.T,
) {

key := "abcdefghij"
plaintext := "abcdefghij"
ciphertext := encode(plaintext, key)
result := decode(ciphertext, key)
defer {
delete(ciphertext)
delete(result)
}

testing.expect_value(t, result, plaintext)
}

@(test)
/// description = Substitution cipher -> Can double shift encode
test_substitution_cipher___can_double_shift_encode :: proc(t: ^testing.T) {

key := "iamapandabear"
plaintext := "iamapandabear"
result := encode(plaintext, key)
expected := "qayaeaagaciai"
defer delete(result)

testing.expect_value(t, result, expected)
}

@(test)
/// description = Substitution cipher -> Can wrap on encode
test_substitution_cipher___can_wrap_on_encode :: proc(t: ^testing.T) {

key := "abcdefghij"
plaintext := "zzzzzzzzzz"
result := encode(plaintext, key)
expected := "zabcdefghi"
defer delete(result)

testing.expect_value(t, result, expected)
}

@(test)
/// description = Substitution cipher -> Can wrap on decode
test_substitution_cipher___can_wrap_on_decode :: proc(t: ^testing.T) {

key := "abcdefghij"
ciphertext := "zabcdefghi"
result := decode(ciphertext, key)
expected := "zzzzzzzzzz"
defer delete(result)

testing.expect_value(t, result, expected)
}

@(test)
/// description = Substitution cipher -> Can encode messages longer than the key
test_substitution_cipher___can_encode_messages_longer_than_the_key :: proc(t: ^testing.T) {

key := "abc"
plaintext := "iamapandabear"
result := encode(plaintext, key)
expected := "iboaqcnecbfcr"
defer delete(result)

testing.expect_value(t, result, expected)
}

@(test)
/// description = Substitution cipher -> Can decode messages longer than the key
test_substitution_cipher___can_decode_messages_longer_than_the_key :: proc(t: ^testing.T) {

key := "abc"
ciphertext := "iboaqcnecbfcr"
result := decode(ciphertext, key)
expected := "iamapandabear"
defer delete(result)

testing.expect_value(t, result, expected)
}