shift_cipher 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +1 -1
- data/README.md +3 -16
- data/bin/shift_cipher +0 -0
- data/lib/shift_cipher/caesar.rb +37 -50
- data/lib/shift_cipher/version.rb +1 -1
- data/shift_cipher.gemspec +4 -4
- metadata +17 -24
- data/lib/shift_cipher/caeser.rb +0 -80
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 87ae282c5d202c988b9b263d2f36ee535670c8fe64e6505d87189ee43d0bf66f
|
4
|
+
data.tar.gz: 7bf8d7cf8071fa62afc459d6fdfbb621beca9c831bd42881cacd1f370e72c2fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f06e85cc27dc998ab730ae3dabe9bc479234bcda5755a90d3d323e6a835cdb690ea605e634a1078482d327e5d873dc91e6717262b5c23fb69b9d103592dea19d
|
7
|
+
data.tar.gz: 96ebd739e5923a9f51f628ae4f1702c8cce3412785c391b07260ce236b6e5ec45a4301175703a8aec4bea9bffa907d78b3470707802a5cbb75f379e82232f386
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -43,29 +43,16 @@ Encrypt a message:
|
|
43
43
|
|
44
44
|
```
|
45
45
|
encrypted_message = cipher.encrypt('hello world')
|
46
|
-
p encrypted_message # => "
|
46
|
+
p encrypted_message # => "khoorzruog"
|
47
47
|
```
|
48
48
|
|
49
49
|
Decrypt a message:
|
50
50
|
|
51
51
|
```
|
52
|
-
decrypted_message = cipher.decrypt('
|
53
|
-
p decrypted_message # => "
|
52
|
+
decrypted_message = cipher.decrypt('khoorzruog')
|
53
|
+
p decrypted_message # => "helloworld"
|
54
54
|
```
|
55
55
|
|
56
|
-
### Using the CLI tool:
|
57
|
-
|
58
|
-
````
|
59
|
-
shift_cipher [options] text
|
60
|
-
````
|
61
|
-
|
62
|
-
#### Options
|
63
|
-
```-h, --help``` Displays help message
|
64
|
-
```-o, --offset OFFSET``` Sets the alphabet offset
|
65
|
-
```-s, --start START``` Sets the starting letter of the shifted alphabet
|
66
|
-
```-d, --decrypt``` Decrypts the given message Decrypt
|
67
|
-
|
68
|
-
|
69
56
|
## Development
|
70
57
|
|
71
58
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/bin/shift_cipher
CHANGED
File without changes
|
data/lib/shift_cipher/caesar.rb
CHANGED
@@ -1,58 +1,46 @@
|
|
1
1
|
module ShiftCipher
|
2
2
|
class Caesar
|
3
|
-
|
4
|
-
|
5
|
-
def initialize(start = 0)
|
6
|
-
|
7
|
-
# set default
|
8
|
-
@offset = 0
|
9
|
-
|
10
|
-
# have we got a string?
|
11
|
-
if start.respond_to?(:ord)
|
3
|
+
DEFAULT_OFFSET = 0
|
12
4
|
|
13
|
-
|
14
|
-
|
15
|
-
@offset = start.downcase.ord - 97
|
16
|
-
elsif ('a'..'z').include?(start)
|
17
|
-
@offset = start.ord - 97
|
5
|
+
FIRST_UNICODE_CODEPOINT = 97
|
6
|
+
LAST_UNICODE_CODEPOINT = 123
|
18
7
|
|
19
|
-
|
20
|
-
elsif (1..26).include?(start.to_i.abs)
|
21
|
-
@offset = start.to_i
|
22
|
-
end
|
23
|
-
|
24
|
-
# have we got a number
|
25
|
-
elsif start.respond_to?(:to_i)
|
26
|
-
# is it in range?
|
27
|
-
if (1..26).include?(start.to_i)
|
28
|
-
@offset = start.to_i
|
29
|
-
end
|
30
|
-
end
|
8
|
+
attr_accessor :offset
|
31
9
|
|
32
|
-
|
33
|
-
|
10
|
+
def initialize(start = 0)
|
11
|
+
@offset = calculate_offset(start)
|
34
12
|
end
|
35
13
|
|
36
14
|
def encrypt(message)
|
37
|
-
shift(message,
|
15
|
+
shift(message, offset)
|
38
16
|
end
|
39
17
|
|
40
18
|
def decrypt(message)
|
41
|
-
shift(message, -
|
19
|
+
shift(message, -offset)
|
42
20
|
end
|
43
21
|
|
44
22
|
private
|
23
|
+
|
45
24
|
def shift(message, directional_offset)
|
46
|
-
|
47
|
-
|
25
|
+
return unless message
|
26
|
+
|
27
|
+
message.downcase.split("").map do |character|
|
48
28
|
if is_alpha?(character)
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
shifted_message += character
|
29
|
+
character_for(character.ord + directional_offset)
|
30
|
+
elsif is_numeric?(character)
|
31
|
+
character
|
53
32
|
end
|
54
|
-
end
|
55
|
-
|
33
|
+
end.join("").squeeze(" ")
|
34
|
+
end
|
35
|
+
|
36
|
+
def calculate_offset(character)
|
37
|
+
if ("A".."z").include?(character)
|
38
|
+
character.downcase.ord - FIRST_UNICODE_CODEPOINT
|
39
|
+
elsif (0..26).include?(character.to_i.abs)
|
40
|
+
character.to_i
|
41
|
+
else
|
42
|
+
DEFAULT_OFFSET
|
43
|
+
end
|
56
44
|
end
|
57
45
|
|
58
46
|
def is_alpha?(character)
|
@@ -63,18 +51,17 @@ module ShiftCipher
|
|
63
51
|
character.match(/^[[:digit:]]$/)
|
64
52
|
end
|
65
53
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
def
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
shifted_ord = 97 + (shifted_ord - 123)
|
54
|
+
# Given the shifted order for a char, returns the corresponding character
|
55
|
+
# If the offset takes us past 123 ('z'), we wrap around to 97 ('A')
|
56
|
+
#
|
57
|
+
# Returns Char
|
58
|
+
def character_for(unicode_codepoint)
|
59
|
+
if unicode_codepoint < FIRST_UNICODE_CODEPOINT
|
60
|
+
unicode_codepoint = LAST_UNICODE_CODEPOINT - (FIRST_UNICODE_CODEPOINT - unicode_codepoint)
|
61
|
+
elsif unicode_codepoint >= LAST_UNICODE_CODEPOINT
|
62
|
+
unicode_codepoint = FIRST_UNICODE_CODEPOINT + (unicode_codepoint - LAST_UNICODE_CODEPOINT)
|
76
63
|
end
|
77
|
-
|
64
|
+
unicode_codepoint.chr
|
78
65
|
end
|
79
66
|
end
|
80
|
-
end
|
67
|
+
end
|
data/lib/shift_cipher/version.rb
CHANGED
data/shift_cipher.gemspec
CHANGED
@@ -17,10 +17,10 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.executables = ["shift_cipher"]
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
|
-
spec.add_development_dependency "bundler"
|
21
|
-
spec.add_development_dependency "rake"
|
20
|
+
spec.add_development_dependency "bundler"
|
21
|
+
spec.add_development_dependency "rake"
|
22
22
|
|
23
23
|
# testing
|
24
|
-
spec.add_development_dependency "rspec"
|
25
|
-
spec.add_development_dependency "pry"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
spec.add_development_dependency "pry"
|
26
26
|
end
|
metadata
CHANGED
@@ -1,77 +1,71 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shift_cipher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- elsapet
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '3.2'
|
48
45
|
- - ">="
|
49
46
|
- !ruby/object:Gem::Version
|
50
|
-
version:
|
47
|
+
version: '0'
|
51
48
|
type: :development
|
52
49
|
prerelease: false
|
53
50
|
version_requirements: !ruby/object:Gem::Requirement
|
54
51
|
requirements:
|
55
|
-
- - "~>"
|
56
|
-
- !ruby/object:Gem::Version
|
57
|
-
version: '3.2'
|
58
52
|
- - ">="
|
59
53
|
- !ruby/object:Gem::Version
|
60
|
-
version:
|
54
|
+
version: '0'
|
61
55
|
- !ruby/object:Gem::Dependency
|
62
56
|
name: pry
|
63
57
|
requirement: !ruby/object:Gem::Requirement
|
64
58
|
requirements:
|
65
|
-
- - "
|
59
|
+
- - ">="
|
66
60
|
- !ruby/object:Gem::Version
|
67
|
-
version: 0
|
61
|
+
version: '0'
|
68
62
|
type: :development
|
69
63
|
prerelease: false
|
70
64
|
version_requirements: !ruby/object:Gem::Requirement
|
71
65
|
requirements:
|
72
|
-
- - "
|
66
|
+
- - ">="
|
73
67
|
- !ruby/object:Gem::Version
|
74
|
-
version: 0
|
68
|
+
version: '0'
|
75
69
|
description: encrypt and de-crypt messages using a simple Caesar cipher, given an
|
76
70
|
offset
|
77
71
|
email:
|
@@ -92,7 +86,6 @@ files:
|
|
92
86
|
- bin/shift_cipher
|
93
87
|
- lib/shift_cipher.rb
|
94
88
|
- lib/shift_cipher/caesar.rb
|
95
|
-
- lib/shift_cipher/caeser.rb
|
96
89
|
- lib/shift_cipher/version.rb
|
97
90
|
- shift_cipher.gemspec
|
98
91
|
homepage: https://github.com/elsapet/shift_cipher
|
@@ -115,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
115
108
|
version: '0'
|
116
109
|
requirements: []
|
117
110
|
rubyforge_project:
|
118
|
-
rubygems_version: 2.
|
111
|
+
rubygems_version: 2.7.6
|
119
112
|
signing_key:
|
120
113
|
specification_version: 4
|
121
114
|
summary: encrypt and de-crypt messages
|
data/lib/shift_cipher/caeser.rb
DELETED
@@ -1,80 +0,0 @@
|
|
1
|
-
module ShiftCipher
|
2
|
-
class Caeser
|
3
|
-
attr_accessor :offset
|
4
|
-
|
5
|
-
def initialize(start = 0)
|
6
|
-
|
7
|
-
# set default
|
8
|
-
@offset = 0
|
9
|
-
|
10
|
-
# have we got a string?
|
11
|
-
if start.respond_to?(:ord)
|
12
|
-
|
13
|
-
# is it alphabetic?
|
14
|
-
if ('A'..'Z').include?(start)
|
15
|
-
@offset = start.downcase.ord - 97
|
16
|
-
elsif ('a'..'z').include?(start)
|
17
|
-
@offset = start.ord - 97
|
18
|
-
|
19
|
-
# is it numeric & in range?
|
20
|
-
elsif (1..26).include?(start.to_i)
|
21
|
-
@offset = start.to_i
|
22
|
-
end
|
23
|
-
|
24
|
-
# have we got a number
|
25
|
-
elsif start.respond_to?(:to_i)
|
26
|
-
# is it in range?
|
27
|
-
if (1..26).include?(start.to_i)
|
28
|
-
@offset = start.to_i
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
# raise an error ...
|
33
|
-
# what about case when start set to 0?
|
34
|
-
end
|
35
|
-
|
36
|
-
def encrypt(message)
|
37
|
-
shift(message, @offset)
|
38
|
-
end
|
39
|
-
|
40
|
-
def decrypt(message)
|
41
|
-
shift(message, - @offset)
|
42
|
-
end
|
43
|
-
|
44
|
-
private
|
45
|
-
def shift(message, directional_offset)
|
46
|
-
shifted_message = ""
|
47
|
-
message.downcase.split("").each do |character|
|
48
|
-
if is_alpha?(character)
|
49
|
-
shifted_character = shift_character(character, directional_offset)
|
50
|
-
shifted_message += shifted_character
|
51
|
-
elsif is_numeric?(character) or is_whitespace?(character)
|
52
|
-
shifted_message += character
|
53
|
-
end
|
54
|
-
end if message
|
55
|
-
shifted_message.squeeze(' ')
|
56
|
-
end
|
57
|
-
|
58
|
-
def is_alpha?(character)
|
59
|
-
character.match(/^[[:alpha:]]$/)
|
60
|
-
end
|
61
|
-
|
62
|
-
def is_numeric?(character)
|
63
|
-
character.match(/^[[:digit:]]$/)
|
64
|
-
end
|
65
|
-
|
66
|
-
def is_whitespace?(character)
|
67
|
-
character.match(/^\s$/)
|
68
|
-
end
|
69
|
-
|
70
|
-
def shift_character(character, offset)
|
71
|
-
shifted_ord = character.ord + offset
|
72
|
-
if shifted_ord < 97
|
73
|
-
shifted_ord = 123 - (97 - shifted_ord)
|
74
|
-
elsif shifted_ord > 122
|
75
|
-
shifted_ord = 97 + (shifted_ord - 123)
|
76
|
-
end
|
77
|
-
shifted_ord.chr
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|