shift_cipher 0.1.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +33 -9
- data/bin/shift_cipher +56 -0
- data/lib/shift_cipher/caesar.rb +80 -0
- data/lib/shift_cipher/version.rb +1 -1
- data/lib/shift_cipher.rb +1 -1
- data/shift_cipher.gemspec +2 -4
- metadata +10 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 62a19e3156396b6ae9b8e03e92147b6afa6939eb
|
4
|
+
data.tar.gz: 0a127d219db38403c298c6a6ec059c7db5fca804
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6afa30e0afb2236a92659fa775b6bede6652d6d668628e0e05b0fbc55aa6f9342d18fac8611033a94297a8d81d6bb9422b0be759ef66d233677d3e63beb8f6c0
|
7
|
+
data.tar.gz: 726d3a52c26e4fb3dffbb1ecef70110ec70e38c11bccb33109daa69f5db79c08fe683cbf91fc8e6e874829ca5596e93affb5b3696a110894215d023a8d9c18dc
|
data/README.md
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# ShiftCipher
|
2
2
|
|
3
|
-
A simple shift cipher, also known as a
|
3
|
+
A simple shift cipher, also known as a Caesar cipher
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
|
-
```
|
9
|
+
```
|
10
10
|
gem 'shift_cipher'
|
11
11
|
```
|
12
12
|
|
@@ -20,28 +20,52 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
Initialise the
|
23
|
+
Initialise the Caesar cipher
|
24
24
|
|
25
|
-
```ruby
|
26
|
-
cipher = ShiftCipher::Caeser.new(3) # initialised with an offset of 3 ('a' = 'd')
|
27
25
|
```
|
26
|
+
cipher = ShiftCipher::Caesar.new(3) # initialised with an offset of 3 ('a' = 'd')
|
27
|
+
```
|
28
|
+
|
28
29
|
or
|
29
|
-
|
30
|
-
|
30
|
+
|
31
|
+
```
|
32
|
+
cipher = ShiftCipher::Caesar.new(-3) # initialised with a negative offset of 3 ('a' = 'x')
|
33
|
+
```
|
34
|
+
|
35
|
+
or
|
36
|
+
|
37
|
+
|
38
|
+
```
|
39
|
+
cipher = ShiftCipher::Caesar.new('d') # initialised with an offset of 3 ('a' = 'd')
|
31
40
|
```
|
32
41
|
|
33
42
|
Encrypt a message:
|
34
|
-
|
43
|
+
|
44
|
+
```
|
35
45
|
encrypted_message = cipher.encrypt('hello world')
|
36
46
|
p encrypted_message # => "khoor zruog"
|
37
47
|
```
|
38
48
|
|
39
49
|
Decrypt a message:
|
40
|
-
|
50
|
+
|
51
|
+
```
|
41
52
|
decrypted_message = cipher.decrypt('khoor zruog')
|
42
53
|
p decrypted_message # => "hello world"
|
43
54
|
```
|
44
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
|
+
|
45
69
|
## Development
|
46
70
|
|
47
71
|
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
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# == Synopsis
|
4
|
+
# This is a simple shift (Caesar) cipher for Ruby
|
5
|
+
#
|
6
|
+
# == Examples
|
7
|
+
# Encode text
|
8
|
+
# shift_cipher -o 4 "hello world" => "khoor zruog"
|
9
|
+
# Decode text
|
10
|
+
# shift_cipher -o 4 -d "khoor zruog" => "hello world"
|
11
|
+
#
|
12
|
+
# Other examples:
|
13
|
+
# We can set the starting letter of the shifted alphabet rather than the offset. The following is
|
14
|
+
# equivalent to shift_cipher -o 3 "hello world"
|
15
|
+
# shift_cipher -s "c" "hello world" => "jgnnq yqtnf"
|
16
|
+
#
|
17
|
+
# == Usage
|
18
|
+
# shift_cipher [options] text
|
19
|
+
#
|
20
|
+
# For help use: shift_cipher -h
|
21
|
+
#
|
22
|
+
# == Options
|
23
|
+
# -h, --help Displays help message
|
24
|
+
# -o, --offset OFFSET Sets the alphabet offset
|
25
|
+
# -s, --start START Sets the starting letter of the shifted alphabet
|
26
|
+
# -d, --decrypt Decrypts the given message Decrypt
|
27
|
+
#
|
28
|
+
# == Author
|
29
|
+
# elsapet
|
30
|
+
#
|
31
|
+
# == Copyright
|
32
|
+
# Copyright (c) 2015 elsapet
|
33
|
+
# Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
|
34
|
+
|
35
|
+
|
36
|
+
require 'shift_cipher'
|
37
|
+
require 'optparse'
|
38
|
+
|
39
|
+
options = {}
|
40
|
+
|
41
|
+
OptionParser.new do |opts|
|
42
|
+
opts.on("-o [OFFSET]", "--offset [OFFSET]", "-s [OFFSET]", "--start [OFFSET]") do |o|
|
43
|
+
options[:offset] = o
|
44
|
+
end
|
45
|
+
opts.on("-d", "--decrypt", "Decrypt") do
|
46
|
+
options[:decrypt] = true
|
47
|
+
end
|
48
|
+
end.parse!
|
49
|
+
|
50
|
+
cipher = options[:offset] ? ShiftCipher::Caesar.new(options[:offset]) : ShiftCipher::Caesar.new
|
51
|
+
|
52
|
+
if options[:decrypt]
|
53
|
+
p cipher.decrypt(ARGV.last)
|
54
|
+
else
|
55
|
+
p cipher.encrypt(ARGV.last)
|
56
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module ShiftCipher
|
2
|
+
class Caesar
|
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.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
|
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
|
data/lib/shift_cipher/version.rb
CHANGED
data/lib/shift_cipher.rb
CHANGED
data/shift_cipher.gemspec
CHANGED
@@ -7,16 +7,14 @@ Gem::Specification.new do |spec|
|
|
7
7
|
spec.name = "shift_cipher"
|
8
8
|
spec.version = ShiftCipher::VERSION
|
9
9
|
spec.authors = ["elsapet"]
|
10
|
-
spec.email = ["ebraae@gmail.com"]
|
11
10
|
|
12
11
|
spec.summary = "encrypt and de-crypt messages"
|
13
|
-
spec.description = "encrypt and de-crypt messages using a simple
|
12
|
+
spec.description = "encrypt and de-crypt messages using a simple Caesar cipher, given an offset"
|
14
13
|
spec.homepage = "https://github.com/elsapet/shift_cipher"
|
15
14
|
spec.license = "MIT"
|
16
15
|
|
17
16
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
-
spec.
|
19
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
17
|
+
spec.executables = ["shift_cipher"]
|
20
18
|
spec.require_paths = ["lib"]
|
21
19
|
|
22
20
|
spec.add_development_dependency "bundler", "~> 1.9"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shift_cipher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- elsapet
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06-
|
11
|
+
date: 2015-06-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -72,11 +72,11 @@ dependencies:
|
|
72
72
|
- - "~>"
|
73
73
|
- !ruby/object:Gem::Version
|
74
74
|
version: 0.10.0
|
75
|
-
description: encrypt and de-crypt messages using a simple
|
75
|
+
description: encrypt and de-crypt messages using a simple Caesar cipher, given an
|
76
76
|
offset
|
77
|
-
email:
|
78
|
-
|
79
|
-
|
77
|
+
email:
|
78
|
+
executables:
|
79
|
+
- shift_cipher
|
80
80
|
extensions: []
|
81
81
|
extra_rdoc_files: []
|
82
82
|
files:
|
@@ -89,7 +89,9 @@ files:
|
|
89
89
|
- Rakefile
|
90
90
|
- bin/console
|
91
91
|
- bin/setup
|
92
|
+
- bin/shift_cipher
|
92
93
|
- lib/shift_cipher.rb
|
94
|
+
- lib/shift_cipher/caesar.rb
|
93
95
|
- lib/shift_cipher/caeser.rb
|
94
96
|
- lib/shift_cipher/version.rb
|
95
97
|
- shift_cipher.gemspec
|
@@ -113,7 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
113
115
|
version: '0'
|
114
116
|
requirements: []
|
115
117
|
rubyforge_project:
|
116
|
-
rubygems_version: 2.
|
118
|
+
rubygems_version: 2.2.1
|
117
119
|
signing_key:
|
118
120
|
specification_version: 4
|
119
121
|
summary: encrypt and de-crypt messages
|