ciphers 0.1.0
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 +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/README.md +15 -0
- data/Rakefile +6 -0
- data/bin/console +7 -0
- data/bin/setup +8 -0
- data/ciphers.gemspec +26 -0
- data/lib/ciphers/version.rb +3 -0
- data/lib/ciphers/vigenere.rb +57 -0
- data/lib/ciphers.rb +6 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7e36bf3e83fae5fbae6343c0dc17ab9bc0438bb1
|
4
|
+
data.tar.gz: 868bf3e3bb6581a752c68330f138cf65fc3df547
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3ccccddcb51caff8fe6ad9684ed2b7e8d2aa141b4838f3b6c4d83b864aac8469ea68c636684dc16da9a08e044220da6355fb5c5411887410aed8a34d6aabc6c6
|
7
|
+
data.tar.gz: 7f3d7114024abd0cc0542c192a11d116033d2949861a62e7a27316dddecf8a64ceac02a7e6f80c92cb512d460652f43842a47c68572b337cd811282f3d637c07
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# Ciphers
|
2
|
+
|
3
|
+
This gem implements simple cryptographic ciphers. As of now, it can only do
|
4
|
+
Vigenère ciphers, but more are coming as I have the time and inclination.
|
5
|
+
Because this mostly exists for my own purposes (the fun of programming it and
|
6
|
+
CTFs), I am not currently accepting PRs for new features or ciphers. Feel free
|
7
|
+
to fork away, though, and if you have any suggestions for what I should
|
8
|
+
implement next, I'd love to hear them! This especially applies if the
|
9
|
+
recommendation comes with a fun puzzle that implementing it will solve!
|
10
|
+
|
11
|
+
## Contributing
|
12
|
+
|
13
|
+
Bug reports and feature/cipher requests are welcome
|
14
|
+
[here](https://github.com/ffleming/ciphers)
|
15
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/ciphers.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ciphers/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ciphers"
|
8
|
+
spec.version = Ciphers::VERSION
|
9
|
+
spec.authors = ["Forrest Fleming"]
|
10
|
+
spec.email = ["ffleming@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Implementation of various ciphers"
|
13
|
+
spec.description = "Implementation of various classic cryptographic ciphers"
|
14
|
+
spec.homepage = "https://github.com/ffleming/ciphers"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
spec.licenses = ['MIT']
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
spec.add_development_dependency "pry-byebug", "~> 3.4"
|
26
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Ciphers
|
2
|
+
LATIN = ('A'..'Z').to_a
|
3
|
+
|
4
|
+
class Vigenere
|
5
|
+
def self.alphabet_from(string)
|
6
|
+
raise ArgumentError.new("Argument must be at most #{LATIN.length} characters long") unless string.length <= 26
|
7
|
+
chars = string.upcase.chars
|
8
|
+
raise ArgumentError.new("Argument must not contain duplicate letters") unless chars.uniq == chars
|
9
|
+
if chars.length < 26
|
10
|
+
chars + (LATIN - chars)
|
11
|
+
else
|
12
|
+
chars
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(alphabet: Ciphers::LATIN, key: )
|
17
|
+
alphabet = alphabet.chars if alphabet.is_a? String
|
18
|
+
@alphabet = alphabet.to_a.freeze
|
19
|
+
@key = key.upcase.freeze
|
20
|
+
end
|
21
|
+
|
22
|
+
def encrypt(string)
|
23
|
+
i = 0
|
24
|
+
string.each_char.with_object('') do |char, ret|
|
25
|
+
ret << char and next unless alphabet.include?(char)
|
26
|
+
key_char = key[ i % key.length ]
|
27
|
+
row = table.fetch key_char
|
28
|
+
col = alphabet.index(char)
|
29
|
+
i += 1
|
30
|
+
ret << row[col]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def decrypt(string)
|
35
|
+
i = 0
|
36
|
+
string.each_char.with_object('') do |char, ret|
|
37
|
+
ret << char and next unless alphabet.include?(char)
|
38
|
+
key_char = key[ i % key.length ]
|
39
|
+
row = table.fetch key_char
|
40
|
+
col = row.index(char)
|
41
|
+
i += 1
|
42
|
+
ret << alphabet[col]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
attr_reader :alphabet, :key
|
49
|
+
|
50
|
+
def table
|
51
|
+
@table = {}.tap do |hash|
|
52
|
+
alphabet.each_with_index {|char, i| hash[char] = alphabet.rotate(i) }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
data/lib/ciphers.rb
ADDED
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ciphers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Forrest Fleming
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-07-18 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.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
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.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry-byebug
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.4'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.4'
|
69
|
+
description: Implementation of various classic cryptographic ciphers
|
70
|
+
email:
|
71
|
+
- ffleming@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- ciphers.gemspec
|
85
|
+
- lib/ciphers.rb
|
86
|
+
- lib/ciphers/version.rb
|
87
|
+
- lib/ciphers/vigenere.rb
|
88
|
+
homepage: https://github.com/ffleming/ciphers
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.4.8
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Implementation of various ciphers
|
112
|
+
test_files: []
|