Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Simple Cipher Exercism Ruby Challenge - Nov 2019

Avatar for su su
November 19, 2019

Simple Cipher Exercism Ruby Challenge - Nov 2019

The talk focused on Exercism.io challenge, "Implement a simple shift cipher like Caesar and a more secure substitution cipher." This 20 mins presentation was given at Ruby on Rails Oceania Sydney meet up on Thursday 19 November 2019.

https://exercism.io/tracks/ruby/exercises/simple-cipher

Su explored the following items from from a student's perspective:
- Simple ciper Caesar concepts
- Review ruby code
- Share 4 key lessons learnt by doing this coding challenge.

Avatar for su

su

November 19, 2019
Tweet

Other Decks in Programming

Transcript

  1. Aug 2019 Sep 2019 Oct 2019 Nov 2019 “Hello World”

    Self – Study Ruby Su’s Dev Career
  2. Outline • Cryptography • Caeser Cipher • Substitution Cipher •

    Caeser Challenge • Step – 1 • Step – 2 • Step – 3 • Thought Process • Code • Lessons Learned 3
  3. Cryptography Confidential important message! I will encrypt it! Thanks. I

    decrypt and can follow the instructions. Send via third party Julius Caesar Generals 4
  4. Simple Cipher Algorithm Defend the east wall of the castle

    - Julius Caesar efgfoe uif fbtu xbmm pg uif dbtumf What the heck does this mean?? Decryption is easy. -1 letter. Yes, we will defend the east wall! 5
  5. Substitution Cipher Algorithm I am a panda bear. iawvawarwtqwamkwbx I

    am a panda bear. 7 Alice Bob PlainText Cipher A W B M D Q E K M V N T P R R B Empty space A Full Sop X
  6. Substitution Cipher – Step 2 • Amend the code to

    allow us to specify a key and use that for the shift distance 9
  7. Ramdomness – Step 3.1 10 Hey, you. KEEP out. NO.

    I come from a source of randomness and have a key that contains only lowercase letters. I am VALID.
  8. Ramdomness – Step 3.2 12 Oh Oh, I am in

    trouble! I cannot submit the key because I forget! What shall I do. SMILE What is your key?
  9. Ramdomness – Step 3.2 • If someone doesn't submit a

    key at all, generate a truly random key of at least 100 characters in length. 13
  10. Ramdomness – Step 3.3 • If the key submitted is

    not composed only of lowercase letters, your solution should handle the error in a language-appropriate way. 14
  11. Thought Process 15 • How to convert letters to number?

    • s • Ruby Methods, Classes Constant Validate Encode Decode
  12. DEMO – Initialize Method 17 def initialize(key = nil) @key

    = key || 100.times.map { ALPHABET.sample }.join fail ArgumentError, 'invalid chars in key' unless valid?(@key) end
  13. DEMO – Initialize Method 18 • .sample method – to

    randomize characters. • .map method – to transform the random characters • .time method – to iterate through 100 times • .join method – to join the 100element array into a string and assign it to @key
  14. DEMO – Encode Method 20 def encode(text) a = 'a'.ord

    text.chars.zip(@key.chars).map do |char, key| ALPHABET[(char.ord - a + key.ord -a) % ALPHABET.length] end.join end
  15. DEMO – Encode Method 21 • .chars method - text

    string is separated into each individual character with .chars method. .chars more efficient than .split since it parses the underlying bytes to return the string's characters • .time method – to iterate through 100 times • .join method – to join the 100element array into a string and assign it to @key • .zip method – to create a new array and pair the original plain text characters and encryption characters • .ord method - convert a character to its ASCII value. a = 'a'.ord . • a = 97 • (Character position, not Unicode value. Therefore substract a.) • .map method - This method is called with a block and returns encrpyted text. • ALPHABET.Length => to ensure valid index of ALPHABET • .e.g ALPHABET[3]
  16. DEMO 24 class Cipher ALPHABET = [*'a'..'z'] attr_reader :key def

    initialize(key = nil) @key = key || 100.times.map { ALPHABET.sample }.join fail ArgumentError, 'invalid chars in key' unless valid?(@key) end def encode(text) a = 'a'.ord text.chars.zip(@key.chars).map do |char, key| ALPHABET[(char.ord - a + key.ord - a) % ALPHABET.length] end.join end def decode(code) code.chars.zip(@key.chars).map do |char, key| ALPHABET[char.ord - key.ord] end.join end private def valid? (key) !key.empty? && key !~ /[^a-z]/ end end
  17. Key Lessons Learnt 26 2 • You never know what

    other drivers and pedestrians will do!! • Expect incorrect input and handle it correctly • Think of both usual execution flow and unexpected flows Defensive Programming principle terminology
  18. Ruby 29 • Enumerable Modules o .zip – Glues together

    two enumerable objects o .map – Transforms every element of the enumerable object & returns the new version as an array o .join – Convert each element of they array to a string • Array Methods o .times o .sample – choose random element or n random elements from the array o .map – create a new array containing the values returned by the bock o A = [“a”, “b”, “c”, “d”] • String Methods o .char = str.each_char.to_a o e.g. meetup = “rorosyd”.chars => [“r”, “o”, “r”, “o”, “s”, “y”, “d”] • Ruby Access Control –Private Method • Tests 4
  19. 30

  20. References • How to write your own Caesar Cipher Encoder

    by Jusus Castello https://www.rubyguides.com/2015/03/caesar-cipher-in-ruby/ • Ruby Documentation, https://ruby-doc.org/ • Stack Overflow • The Well-Grounded Rubyist, Third Edition, https://www.manning.com/books/the-well-grounded-rubyist-third- edition • Youtube 31