caesar_code 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c07a4a5992a81276bc877035301819821ddb7036
4
+ data.tar.gz: cc44718b9caddea12c2bc53b64d26ba42454d8ce
5
+ SHA512:
6
+ metadata.gz: 307ff2f894ca876a75f3c7d0486b8ad191c2edf00ca4c50c6e58390c8d1f9dfd7341133c300e6d27b2ab019a3d8438e2979739b4c010c87fe903f7cfe966ab0a
7
+ data.tar.gz: 79c6a003538cfcdb8e571169137dee8aca0b63c196c2efde3a39c269137429b790e3e74dce6843cd33adefe14f482b483c268fd891e36e4aefb0c150bda9ab01
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in caesar.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 otamm
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,51 @@
1
+ # CaesarCode
2
+
3
+ A kinda simple gem that implements and breaks Caesar or Shift (basically Caesar cipher
4
+ with a number of shifts not equivalent to 3) ciphers. Breaking a cipher relies on user input to
5
+ confirm a message is readable and makes sense. The 15 most common words of the English language
6
+ are loaded by default and the user is prompted a message asking whether or not the cipher was
7
+ successfully broken based on this list of words; the list can be arbitrarily extended as an optional
8
+ second parameter to the #decipher method.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'caesar_code'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install caesar
25
+
26
+ ## Usage
27
+
28
+ Enciphering:
29
+ ```ruby
30
+ CaesarCode.encipher("abcd")
31
+ # => "DEFG"
32
+ # By default, the encryption shifts each letter 3 times.
33
+ CaesarCode.encipher("IBM",25)
34
+ # => "HAL"
35
+ # The encryption can also be made with an arbitrary number of shifts.
36
+ ```
37
+
38
+ Deciphering:
39
+ ```ruby
40
+ CaesarCode.decipher("cf")
41
+ # Will prompt the user asking if the message "BE" makes any sense (as "BE" is one of the most common words in English)
42
+ CaesarCode.decipher("ifmmp IBM", "HAL")
43
+ # Will prompt the user on the message "HELLO HAL", as "HAL" was added to the list of words to search for in the text.
44
+ ```
45
+ ## Contributing
46
+
47
+ 1. Fork it ( https://github.com/[my-github-username]/caesar/fork )
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create a new Pull Request
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ Dir.glob('spec/tasks/**/*.rake').each(&method(:import))
4
+ #run RSpec with 'bundle exec rake spec'
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'caesar_code/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "caesar_code"
8
+ spec.version = CaesarCode::VERSION
9
+ spec.authors = ["Otávio Monteagudo"]
10
+ spec.email = ["oivatom@gmail.com"]
11
+ spec.summary = %q{Implementation of encipher and decipher functions of the Caesar/Shift cipher.}
12
+ spec.description = %q{Easily implement a function to encipher or decipher a given message encrypted with either Caesar or Shift cipher. Deciphering relies on user input and a list of common words.}
13
+ spec.homepage = "https://github.com/otamm/caesar-code"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,105 @@
1
+ require "caesar_code/version"
2
+
3
+ module CaesarCode
4
+
5
+ @@alpha_nums = {
6
+ "A"=>1,
7
+ "B"=>2,
8
+ "C"=>3,
9
+ "D"=>4,
10
+ "E"=>5,
11
+ "F"=>6,
12
+ "G"=>7,
13
+ "H"=>8,
14
+ "I"=>9,
15
+ "J"=>10,
16
+ "K"=>11,
17
+ "L"=>12,
18
+ "M"=>13,
19
+ "N"=>14,
20
+ "O"=>15,
21
+ "P"=>16,
22
+ "Q"=>17,
23
+ "R"=>18,
24
+ "S"=>19,
25
+ "T"=>20,
26
+ "U"=>21,
27
+ "V"=>22,
28
+ "W"=>23,
29
+ "X"=>24,
30
+ "Y"=>25,
31
+ "Z"=>0
32
+ }
33
+ # 'Z => 0' because of the "modulo 26" operation.
34
+
35
+ def self.encipher(message,key=3) #officially, Caesar cipher shifts 3 positions. the n position shift cipher is the "shift cipher".
36
+
37
+ if key.class == String
38
+ key = @@alpha_nums[key]
39
+ elsif key.class != Fixnum
40
+ raise "Key argument must be either a string or integer."
41
+ end
42
+
43
+ cipher = []
44
+ message = message.upcase.split("")
45
+ message.each do |char|
46
+ if @@alpha_nums[char] == nil
47
+ cipher.push(char)
48
+ else
49
+ cipher.push(@@alpha_nums.find { |k,val| @@alpha_nums[k] == ((@@alpha_nums[char] + key) % 26) }[0])
50
+ end
51
+ end
52
+ return cipher.join("")
53
+ end
54
+
55
+ def self.decipher(ciphered,words_to_search_for=false)
56
+ most_common_english_words = ["THE","BE","TO","OF","AND","A","IN","THAT","HAVE","I","IT","FOR","WITH","AS","IS"]
57
+
58
+ #checks for optional argument and adds it to words of interest.
59
+ if words_to_search_for
60
+ if words_to_search_for.class == Array
61
+ words_to_search_for.each { |word| word = word.upcase }
62
+ most_common_english_words = words_to_search_for + most_common_english_words
63
+ elsif words_to_search_for.class == String
64
+ words_to_search_for = words_to_search_for.upcase.split(/\s/)
65
+ most_common_english_words = words_to_search_for + most_common_english_words
66
+ end
67
+ end
68
+
69
+ # variables that need to be declared outside the function's main loop.
70
+ ciphered = ciphered.upcase
71
+ coded_words_chars = ciphered.split("")
72
+ all_messages = []
73
+ alphaposition = 0
74
+ #main loop; iterates over alphabet and shifts each character of the input cipher.
75
+ #(line 47 to 56) Asks user to confirm a possible match. Outputs all shifts if no confirmation is received.
76
+ while alphaposition <= 26
77
+ message = []
78
+ alphaposition += 1
79
+ coded_words_chars.each do |char|
80
+ if @@alpha_nums[char] == nil
81
+ message.push(char)
82
+ else
83
+ message.push(@@alpha_nums.find { |key,val| @@alpha_nums[key] == (@@alpha_nums[char] + alphaposition) % 26 }[0])
84
+ end
85
+ end
86
+ message = message.join("").split(/[^A-Z0-9]/) # the RegEx splits 'message' by each char that fits 'not an uppercase letter neither a digit'
87
+ most_common_english_words.each do |word|
88
+ if message.include?(word)
89
+ puts message.join(" ")
90
+ puts "Does this make any sense? (y/n)"
91
+ answer = gets.chomp
92
+ if answer == 'y' || answer == 'Y'
93
+ puts "Encrypted with #{ alphaposition } shift(s)"
94
+ return message.join(" ")
95
+ end
96
+ end
97
+ end
98
+ all_messages.push(message.join(" "))
99
+ end
100
+
101
+ #output in case of failure of match recognition by the user.
102
+ puts all_messages
103
+ end
104
+
105
+ end
@@ -0,0 +1,10 @@
1
+ module CaesarCode
2
+ VERSION = "0.0.1"
3
+ end
4
+
5
+ # Follow semver.org 's guidelines:
6
+
7
+ # MAJOR version when you make incompatible API changes,
8
+ # MINOR version when you add functionality in a backwards-compatible manner, and
9
+ # PATCH version when you make backwards-compatible bug fixes.
10
+ # Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe CaesarCode do
4
+
5
+ describe '#encipher' do
6
+
7
+ it 'enciphers with 3 shits by default' do
8
+ output = CaesarCode.encipher("IBM")
9
+ expect(output).to eq "LEP"
10
+ end
11
+
12
+ it 'enciphers with an arbitrary number of shifts' do
13
+ output = CaesarCode.encipher("IBM",25)
14
+ expect(output).to eq "HAL"
15
+ end
16
+
17
+ end
18
+
19
+ #describe '#decipher with positives' do
20
+ #before do
21
+ # $positive = StringIO.new("y\n")
22
+ # $negative = StringIO.new("n\n")
23
+ #end
24
+
25
+ #after do
26
+ # $positive = STDIN
27
+ # $negative = STDIN
28
+ #end
29
+
30
+ #it 'asks user to confirm a deciphered message' do
31
+ # positive = StringIO.new("y\n")
32
+ # allow(Caesar.decipher("cf")).to receive(:answer) { positive }
33
+ # expect(deciphered).to eq "BE"
34
+ #end
35
+ #end
36
+
37
+ end
@@ -0,0 +1,2 @@
1
+ require 'caesar_code'
2
+ #require 'stringio'
@@ -0,0 +1,3 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: caesar_code
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Otávio Monteagudo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Easily implement a function to encipher or decipher a given message encrypted
56
+ with either Caesar or Shift cipher. Deciphering relies on user input and a list
57
+ of common words.
58
+ email:
59
+ - oivatom@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - caesar_code.gemspec
70
+ - lib/caesar_code.rb
71
+ - lib/caesar_code/version.rb
72
+ - spec/caesar_code_spec.rb
73
+ - spec/spec_helper.rb
74
+ - spec/tasks/rspec.rake
75
+ homepage: https://github.com/otamm/caesar-code
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.2.2
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Implementation of encipher and decipher functions of the Caesar/Shift cipher.
99
+ test_files:
100
+ - spec/caesar_code_spec.rb
101
+ - spec/spec_helper.rb
102
+ - spec/tasks/rspec.rake