cryptogram 0.1.0 → 0.1.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.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/README.md +28 -1
- data/lib/cryptogram/ciphers/caesar.rb +10 -15
- data/lib/cryptogram/mapper.rb +28 -0
- data/lib/cryptogram/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 52be6b3851ed66b26a6d4d8310bd726f1ff9d31b
|
4
|
+
data.tar.gz: c799b07969056cf91bf201080c50412a503d53d3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '08bac23d613b18d5962dc7deccdc45be18b3455300bf86a592a8e03bee34cd20fb5db4d058844b6a9004b907f3a15811bf8834fb7333ba59794f11dad0d9a629'
|
7
|
+
data.tar.gz: 6dca54e86cb47a2995f549c543d6c59029fd945a69e7231e651017e9d66eca37d56052ca136bd89f4d26507fe803a77c6b779c03a178b8f386780b94c542f666
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,17 @@
|
|
1
1
|
# Cryptogram
|
2
2
|
|
3
|
+
[](https://badge.fury.io/rb/cryptogram)
|
4
|
+
|
5
|
+
## Builds
|
6
|
+
|
7
|
+
Master:
|
8
|
+
|
3
9
|
[](https://travis-ci.org/mkalmykov/cryptogram)
|
4
10
|
|
11
|
+
Develop:
|
12
|
+
|
13
|
+
[](https://travis-ci.org/mkalmykov/cryptogram)
|
14
|
+
|
5
15
|
Cryptogram in intended to combine various ciphers in one library.
|
6
16
|
|
7
17
|
## Installation
|
@@ -22,9 +32,26 @@ Or install it yourself as:
|
|
22
32
|
|
23
33
|
$ gem install cryptogram
|
24
34
|
|
35
|
+
## Implemented ciphers
|
36
|
+
|
37
|
+
* [Caesar cipher](https://en.wikipedia.org/wiki/Caesar_cipher)
|
38
|
+
|
25
39
|
## Usage
|
26
40
|
|
27
|
-
|
41
|
+
```ruby
|
42
|
+
require 'cryptogram'
|
43
|
+
|
44
|
+
alphabet = Array('a'..'z')
|
45
|
+
shift = 3
|
46
|
+
|
47
|
+
encryptor = Cryptogram::Ciphers::Caesar.new(alphabet: alphabet, shift: shift)
|
48
|
+
|
49
|
+
encryptor.encrypt('Pack my box with five dozen liquor jugs')
|
50
|
+
# => "Sdfn pb era zlwk ilyh grchq oltxru mxjv
|
51
|
+
|
52
|
+
encryptor.decrypt('Sdfn pb era zlwk ilyh grchq oltxru mxjv')
|
53
|
+
# => "Pack my box with five dozen liquor jugs"
|
54
|
+
```
|
28
55
|
|
29
56
|
## Development
|
30
57
|
|
@@ -1,33 +1,28 @@
|
|
1
|
+
require 'cryptogram/mapper'
|
2
|
+
|
1
3
|
module Cryptogram
|
2
4
|
module Ciphers
|
3
5
|
class Caesar
|
4
|
-
def initialize(alphabet:, shift
|
5
|
-
@
|
6
|
-
@shift = shift
|
7
|
-
@case_sensitive = case_sensitive
|
8
|
-
@mapper = build_mapper
|
6
|
+
def initialize(alphabet:, shift:)
|
7
|
+
@mapper = ::Cryptogram::Mapper.new(alphabet: alphabet, shift: shift)
|
9
8
|
end
|
10
9
|
|
11
10
|
def encrypt(string)
|
12
|
-
process(string) { |char| mapper.
|
11
|
+
process(string) { |char| mapper.map_by_key(char) }
|
13
12
|
end
|
14
13
|
|
15
14
|
def decrypt(string)
|
16
|
-
process(string) { |char| mapper.
|
15
|
+
process(string) { |char| mapper.map_by_value(char) }
|
17
16
|
end
|
18
17
|
|
19
18
|
private
|
20
19
|
|
21
|
-
attr_reader :alphabet, :shift, :
|
22
|
-
|
23
|
-
def build_mapper
|
24
|
-
shifted_alphabet = alphabet.rotate(shift)
|
25
|
-
|
26
|
-
Hash[alphabet.zip(shifted_alphabet)]
|
27
|
-
end
|
20
|
+
attr_reader :alphabet, :shift, :mapper
|
28
21
|
|
29
22
|
def process(string, &block)
|
30
|
-
|
23
|
+
raise ArgumentError unless block_given?
|
24
|
+
|
25
|
+
string.each_char.map(&block).join
|
31
26
|
end
|
32
27
|
end
|
33
28
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Cryptogram
|
2
|
+
class Mapper
|
3
|
+
def initialize(alphabet:, shift:)
|
4
|
+
@shift = shift
|
5
|
+
|
6
|
+
@shifted_mapper = build_mapper(alphabet)
|
7
|
+
@shifted_swapcased_mapper = build_mapper(alphabet.join.swapcase.chars)
|
8
|
+
end
|
9
|
+
|
10
|
+
def map_by_key(char)
|
11
|
+
shifted_mapper.dig(char) || shifted_swapcased_mapper.dig(char) || char
|
12
|
+
end
|
13
|
+
|
14
|
+
def map_by_value(char)
|
15
|
+
shifted_mapper.key(char) || shifted_swapcased_mapper.key(char) || char
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def build_mapper(alphabet)
|
21
|
+
shifted_alphabet = alphabet.rotate(shift)
|
22
|
+
|
23
|
+
Hash[alphabet.zip(shifted_alphabet)]
|
24
|
+
end
|
25
|
+
|
26
|
+
attr_reader :shift, :shifted_mapper, :shifted_swapcased_mapper
|
27
|
+
end
|
28
|
+
end
|
data/lib/cryptogram/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maksim Kalmykov
|
@@ -86,6 +86,7 @@ files:
|
|
86
86
|
- cryptogram.gemspec
|
87
87
|
- lib/cryptogram.rb
|
88
88
|
- lib/cryptogram/ciphers/caesar.rb
|
89
|
+
- lib/cryptogram/mapper.rb
|
89
90
|
- lib/cryptogram/version.rb
|
90
91
|
homepage: https://github.com/mkalmykov/cryptogram
|
91
92
|
licenses:
|