shift_ciphers 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f59faf5d08db9b955f06c617b9b0191523bed8039b6c1a03f822cdcd0129c580
4
+ data.tar.gz: d50f539e74132d01760f19cd6a7a0982f779f166e6d165070f03637528ce7986
5
+ SHA512:
6
+ metadata.gz: 31cbc305b50e2010c10c0581e8ed8f04733558479fdcdf199349395aa7ec479f22bd3d5d3c71a51daf15a023828b2514c678d47cd6c50b78981c6ec8734a62a5
7
+ data.tar.gz: ce84442a71e158009e8e85cd9e65c854da49ca959299eb6dc6bc47b2d05fe54886a90d4ba50cfbce83a5e5057b76de5770d1beae678ec7c73671f89acc735434
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Tomasz Więch
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,27 @@
1
+ Shift Ciphers [![Gem Version](https://badge.fury.io/rb/shift_ciphers.svg)](http://badge.fury.io/rb/shift_ciphers) [![Build Status](https://travis-ci.org/TeWu/shift_ciphers.svg?branch=master)](https://travis-ci.org/TeWu/shift_ciphers)
2
+ =======
3
+
4
+ **Shift Ciphers** gem is simple, yet complete, implementation of [Caesar](https://en.wikipedia.org/wiki/Caesar_cipher) and [Vigenère](https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher) ciphers.
5
+
6
+ Installation
7
+ -------
8
+
9
+ gem install shift_ciphers
10
+
11
+ Basic usage
12
+ -------
13
+
14
+ ```ruby
15
+ require 'shift_ciphers'
16
+
17
+ caesar = ShiftCiphers::Caesar.new
18
+ caesar_cyphertext = caesar.encrypt("ATTACKATDAWN")
19
+ p caesar_cyphertext
20
+ p caesar.decrypt(caesar_cyphertext)
21
+
22
+ key = "5ecr3t"
23
+ vigenere_cyphertext = ShiftCiphers::Vigenere.encrypt("ATTACKATDAWN", key)
24
+ p key
25
+ p vigenere_cyphertext
26
+ p ShiftCiphers::Vigenere.decrypt(vigenere_cyphertext, key)
27
+ ```
@@ -0,0 +1,41 @@
1
+ require 'shift_ciphers/shift_ciphers'
2
+
3
+ module ShiftCiphers
4
+ class Caesar
5
+ DEFAULT_OFFSET = 13
6
+ attr_accessor :offset, :alphabet
7
+
8
+ def initialize(offset = DEFAULT_OFFSET, alphabet = ALPHABET)
9
+ @offset = offset
10
+ @alphabet = alphabet
11
+ end
12
+
13
+ def encrypt(plaintext)
14
+ self.class.encrypt(plaintext, offset, alphabet)
15
+ end
16
+
17
+ def decrypt(cyphertext)
18
+ self.class.decrypt(cyphertext, offset, alphabet)
19
+ end
20
+
21
+ class << self
22
+ def encrypt(plaintext, offset = DEFAULT_OFFSET, alphabet = ALPHABET)
23
+ process(plaintext, offset, :encrypt, alphabet)
24
+ end
25
+
26
+ def decrypt(cyphertext, offset = DEFAULT_OFFSET, alphabet = ALPHABET)
27
+ process(cyphertext, offset, :decrypt, alphabet)
28
+ end
29
+
30
+ protected
31
+
32
+ def process(text, offset, direction = :encrypt, alphabet = ALPHABET)
33
+ offset *= (direction == :encrypt ? 1 : -1)
34
+ text.each_char.reduce("") do |cyphertext, char|
35
+ char_idx = alphabet.index(char)
36
+ cyphertext << alphabet[(char_idx + offset) % alphabet.size]
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,6 @@
1
+ module ShiftCiphers
2
+ ALPHABET = ((0..9).to_a.map(&:to_s) +
3
+ ('a'..'z').to_a +
4
+ ('A'..'Z').to_a
5
+ ).join + " !@#$%^&*()-_=+{}[];:'\",./<>?"
6
+ end
@@ -0,0 +1,10 @@
1
+ module ShiftCiphers
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ PATCH = 1
6
+ LABEL = nil
7
+ end
8
+
9
+ VERSION = ([Version::MAJOR, Version::MINOR, Version::PATCH, Version::LABEL].compact * '.').freeze
10
+ end
@@ -0,0 +1,41 @@
1
+ require 'shift_ciphers/shift_ciphers'
2
+
3
+ module ShiftCiphers
4
+ class Vigenere
5
+ attr_accessor :key, :alphabet
6
+
7
+ def initialize(key, alphabet = ALPHABET)
8
+ @key = key
9
+ @alphabet = alphabet
10
+ end
11
+
12
+ def encrypt(plaintext)
13
+ self.class.encrypt(plaintext, key, alphabet)
14
+ end
15
+
16
+ def decrypt(cyphertext)
17
+ self.class.decrypt(cyphertext, key, alphabet)
18
+ end
19
+
20
+ class << self
21
+ def encrypt(plaintext, key, alphabet = ALPHABET)
22
+ process(plaintext, key, :encrypt, alphabet)
23
+ end
24
+
25
+ def decrypt(cyphertext, key, alphabet = ALPHABET)
26
+ process(cyphertext, key, :decrypt, alphabet)
27
+ end
28
+
29
+ protected
30
+
31
+ def process(text, key, direction = :encrypt, alphabet = ALPHABET)
32
+ key_chars = key.chars.cycle
33
+ text.each_char.reduce("") do |cyphertext, char|
34
+ char_idx = alphabet.index(char)
35
+ offset = alphabet.index(key_chars.next) * (direction == :encrypt ? 1 : -1)
36
+ cyphertext << alphabet[(char_idx + offset) % alphabet.size]
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ require 'shift_ciphers/version'
2
+ require 'shift_ciphers/caesar'
3
+ require 'shift_ciphers/vigenere'
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'shift_ciphers/version'
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "shift_ciphers"
9
+ s.version = ShiftCiphers::VERSION
10
+ s.authors = ["Tomasz Więch"]
11
+ s.email = ["tewu.dev@gmail.com"]
12
+
13
+ s.summary = "Implementation of Caesar and Vigenere ciphers"
14
+ s.description = "Implementation of Caesar and Vigenere ciphers"
15
+ s.homepage = "https://github.com/TeWu/shift-ciphers"
16
+ s.license = "MIT"
17
+
18
+ s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(doc|test|spec|features|bin)/|Rakefile|Gemfile*|CHANGELOG}) || f.start_with?('.') }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.required_ruby_version = ">= 2.5.0"
22
+
23
+ s.add_development_dependency "bundler", "~> 1.16"
24
+ s.add_development_dependency "pry", "~> 0.11"
25
+ s.add_development_dependency "rspec", "~> 3.8"
26
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shift_ciphers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tomasz Więch
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-10-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.8'
55
+ description: Implementation of Caesar and Vigenere ciphers
56
+ email:
57
+ - tewu.dev@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE
63
+ - README.md
64
+ - lib/shift_ciphers.rb
65
+ - lib/shift_ciphers/caesar.rb
66
+ - lib/shift_ciphers/shift_ciphers.rb
67
+ - lib/shift_ciphers/version.rb
68
+ - lib/shift_ciphers/vigenere.rb
69
+ - shift_ciphers.gemspec
70
+ homepage: https://github.com/TeWu/shift-ciphers
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 2.5.0
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.7.7
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Implementation of Caesar and Vigenere ciphers
94
+ test_files: []