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 +4 -4
- data/README.md +12 -0
- data/lib/cryptogram/ciphers/base_cipher.rb +34 -0
- data/lib/cryptogram/ciphers/caesar.rb +10 -6
- data/lib/cryptogram/mapper.rb +7 -9
- data/lib/cryptogram/presets/alphabets.rb +18 -0
- data/lib/cryptogram/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bb05a3f7c332d9aaf05ccbfb7a5d88df477dfbeb
|
4
|
+
data.tar.gz: d921172d42eb5ded2869c04ec605fa60e0205cb3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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.
|
17
|
+
process(string) { |char| @mapper.map_to_shifted(char) }
|
12
18
|
end
|
13
19
|
|
14
20
|
def decrypt(string)
|
15
|
-
process(string) { |char| mapper.
|
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
|
|
data/lib/cryptogram/mapper.rb
CHANGED
@@ -3,26 +3,24 @@ module Cryptogram
|
|
3
3
|
def initialize(alphabet:, shift:)
|
4
4
|
@shift = shift
|
5
5
|
|
6
|
-
@
|
7
|
-
@
|
6
|
+
@mapper = build_mapper(alphabet)
|
7
|
+
@swapcased_mapper = build_mapper(alphabet.join.swapcase.chars)
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
11
|
-
|
10
|
+
def map_to_shifted(char)
|
11
|
+
@mapper.dig(char) || @swapcased_mapper.dig(char) || char
|
12
12
|
end
|
13
13
|
|
14
|
-
def
|
15
|
-
|
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
|
data/lib/cryptogram/version.rb
CHANGED
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.
|
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:
|
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:
|