cryptogram 0.1.1 → 0.1.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 52be6b3851ed66b26a6d4d8310bd726f1ff9d31b
4
- data.tar.gz: c799b07969056cf91bf201080c50412a503d53d3
3
+ metadata.gz: bb05a3f7c332d9aaf05ccbfb7a5d88df477dfbeb
4
+ data.tar.gz: d921172d42eb5ded2869c04ec605fa60e0205cb3
5
5
  SHA512:
6
- metadata.gz: '08bac23d613b18d5962dc7deccdc45be18b3455300bf86a592a8e03bee34cd20fb5db4d058844b6a9004b907f3a15811bf8834fb7333ba59794f11dad0d9a629'
7
- data.tar.gz: 6dca54e86cb47a2995f549c543d6c59029fd945a69e7231e651017e9d66eca37d56052ca136bd89f4d26507fe803a77c6b779c03a178b8f386780b94c542f666
6
+ metadata.gz: c7dcec80d8c8a2c56d4e86e454cdfb38a7a37d6be2dc26260cdce08ec43bb4e834c18d5cff55d776aa74364e69195d0e6176ba6f15c64008831f50a370af6e3f
7
+ data.tar.gz: 69e791543035b8b511676dd1bd5ab4494dbc633dcaf606dc874ea939e1f29801b9424800e0828870059586c3a55eb535f8a545c400113d1fb81760d786fb66d9
data/README.md CHANGED
@@ -53,6 +53,12 @@ encryptor.decrypt('Sdfn pb era zlwk ilyh grchq oltxru mxjv')
53
53
  # => "Pack my box with five dozen liquor jugs"
54
54
  ```
55
55
 
56
+ You can also use built-in alphabet presets (`lib/cryptogram/presets/alphabets.rb`):
57
+
58
+ ```ruby
59
+ encryptor = Cryptogram::Ciphers::Caesar.new(alphabet: :english, shift: shift)
60
+ ```
61
+
56
62
  ## Development
57
63
 
58
64
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -63,6 +69,12 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
63
69
 
64
70
  Bug reports and pull requests are welcome on GitHub at https://github.com/mkalmykov/cryptogram.
65
71
 
72
+ To contribute:
73
+
74
+ 1. Fork it.
75
+ 2. `git checkout -b feature/my_awesome_feature develop`
76
+ 3. Create a pull request into develop.
77
+
66
78
  ## License
67
79
 
68
80
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,34 @@
1
+ require 'cryptogram/presets/alphabets'
2
+
3
+ module Cryptogram
4
+ module Ciphers
5
+ class BaseCipher
6
+ #
7
+ # @param [Array, Symbol] alphabet Array of chars or presetted alphabet name
8
+ def initialize(alphabet:)
9
+ @alphabet = initialize_alphabet(alphabet)
10
+ end
11
+
12
+ def encrypt
13
+ raise NotImplementedError
14
+ end
15
+
16
+ def decrypt
17
+ raise NotImplementedError
18
+ end
19
+
20
+ private
21
+
22
+ def initialize_alphabet(alphabet)
23
+ case alphabet
24
+ when Array
25
+ alphabet
26
+ when Symbol
27
+ Cryptogram::Presets::Alphabets.fetch(alphabet)
28
+ else
29
+ raise ArgumentError, "Unsupported alphabet class: #{alphabet.class}"
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,24 +1,28 @@
1
+ require 'cryptogram/ciphers/base_cipher'
1
2
  require 'cryptogram/mapper'
2
3
 
3
4
  module Cryptogram
4
5
  module Ciphers
5
- class Caesar
6
+ class Caesar < BaseCipher
7
+ #
8
+ # @param [Array, Symbol] alphabet Array of chars or presetted alphabet name
9
+ # @param [Integer] shift Encryption shift
6
10
  def initialize(alphabet:, shift:)
7
- @mapper = ::Cryptogram::Mapper.new(alphabet: alphabet, shift: shift)
11
+ super(alphabet: alphabet)
12
+
13
+ @mapper = ::Cryptogram::Mapper.new(alphabet: @alphabet, shift: shift)
8
14
  end
9
15
 
10
16
  def encrypt(string)
11
- process(string) { |char| mapper.map_by_key(char) }
17
+ process(string) { |char| @mapper.map_to_shifted(char) }
12
18
  end
13
19
 
14
20
  def decrypt(string)
15
- process(string) { |char| mapper.map_by_value(char) }
21
+ process(string) { |char| @mapper.map_to_initial(char) }
16
22
  end
17
23
 
18
24
  private
19
25
 
20
- attr_reader :alphabet, :shift, :mapper
21
-
22
26
  def process(string, &block)
23
27
  raise ArgumentError unless block_given?
24
28
 
@@ -3,26 +3,24 @@ module Cryptogram
3
3
  def initialize(alphabet:, shift:)
4
4
  @shift = shift
5
5
 
6
- @shifted_mapper = build_mapper(alphabet)
7
- @shifted_swapcased_mapper = build_mapper(alphabet.join.swapcase.chars)
6
+ @mapper = build_mapper(alphabet)
7
+ @swapcased_mapper = build_mapper(alphabet.join.swapcase.chars)
8
8
  end
9
9
 
10
- def map_by_key(char)
11
- shifted_mapper.dig(char) || shifted_swapcased_mapper.dig(char) || char
10
+ def map_to_shifted(char)
11
+ @mapper.dig(char) || @swapcased_mapper.dig(char) || char
12
12
  end
13
13
 
14
- def map_by_value(char)
15
- shifted_mapper.key(char) || shifted_swapcased_mapper.key(char) || char
14
+ def map_to_initial(char)
15
+ @mapper.key(char) || @swapcased_mapper.key(char) || char
16
16
  end
17
17
 
18
18
  private
19
19
 
20
20
  def build_mapper(alphabet)
21
- shifted_alphabet = alphabet.rotate(shift)
21
+ shifted_alphabet = alphabet.rotate(@shift)
22
22
 
23
23
  Hash[alphabet.zip(shifted_alphabet)]
24
24
  end
25
-
26
- attr_reader :shift, :shifted_mapper, :shifted_swapcased_mapper
27
25
  end
28
26
  end
@@ -0,0 +1,18 @@
1
+ module Cryptogram
2
+ module Presets
3
+ module Alphabets
4
+ ALPHABETS = {
5
+ english: Array('a'..'z'),
6
+ russian: Array('а'..'я')
7
+ }.freeze
8
+
9
+ class << self
10
+ def fetch(alphabet_name)
11
+ ALPHABETS.fetch(alphabet_name) do
12
+ raise ArgumentError, "Unrecognized alphabet preset: #{alphabet_name}"
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,3 +1,3 @@
1
1
  module Cryptogram
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.1.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cryptogram
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maksim Kalmykov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-11-29 00:00:00.000000000 Z
11
+ date: 2018-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -85,8 +85,10 @@ files:
85
85
  - bin/setup
86
86
  - cryptogram.gemspec
87
87
  - lib/cryptogram.rb
88
+ - lib/cryptogram/ciphers/base_cipher.rb
88
89
  - lib/cryptogram/ciphers/caesar.rb
89
90
  - lib/cryptogram/mapper.rb
91
+ - lib/cryptogram/presets/alphabets.rb
90
92
  - lib/cryptogram/version.rb
91
93
  homepage: https://github.com/mkalmykov/cryptogram
92
94
  licenses: