ciphers 0.1.0 → 0.2.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 +4 -4
- data/README.md +4 -1
- data/lib/ciphers.rb +13 -1
- data/lib/ciphers/autokey.rb +40 -0
- data/lib/ciphers/version.rb +1 -1
- data/lib/ciphers/vigenere.rb +0 -13
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 78798fc9ac89c2c90a9a8c2a475fb5166547eaf6
|
4
|
+
data.tar.gz: 2700a2447d77e52df94ac4abc02c306812398e6f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2e4e3c619be9e125b3bcb5a09a93632adcdf67f1c46f47f38a5226e4ceea2e5c9e85445653287434bcd362936b5c770c07c70c0991eb131df84018d4ae30ab0b
|
7
|
+
data.tar.gz: 618b5dc0975b2e480ee331a2c44ae25b5128c5eb408f202495d9f19d871d8569b0112243fb056b14cd8cc8f65b64eb1e84422c6a84d1de8d6ff0df37ce979bef
|
data/README.md
CHANGED
@@ -1,13 +1,16 @@
|
|
1
1
|
# Ciphers
|
2
2
|
|
3
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.
|
4
|
+
Vigenère and Autokey ciphers, but more are coming as I have the time and inclination.
|
5
5
|
Because this mostly exists for my own purposes (the fun of programming it and
|
6
6
|
CTFs), I am not currently accepting PRs for new features or ciphers. Feel free
|
7
7
|
to fork away, though, and if you have any suggestions for what I should
|
8
8
|
implement next, I'd love to hear them! This especially applies if the
|
9
9
|
recommendation comes with a fun puzzle that implementing it will solve!
|
10
10
|
|
11
|
+
I generally feel free to break the API on minor revisions prior to a 1.0. After
|
12
|
+
that, I follow semantic versioning.
|
13
|
+
|
11
14
|
## Contributing
|
12
15
|
|
13
16
|
Bug reports and feature/cipher requests are welcome
|
data/lib/ciphers.rb
CHANGED
@@ -1,6 +1,18 @@
|
|
1
1
|
require "ciphers/version"
|
2
2
|
require "ciphers/vigenere"
|
3
|
-
|
3
|
+
require "ciphers/autokey"
|
4
4
|
|
5
5
|
module Ciphers
|
6
|
+
LATIN = ('A'..'Z').to_a
|
7
|
+
|
8
|
+
def self.alphabet_from(string)
|
9
|
+
raise ArgumentError.new("Argument must be at most #{LATIN.length} characters long") unless string.length <= 26
|
10
|
+
chars = string.upcase.chars
|
11
|
+
raise ArgumentError.new("Argument must not contain duplicate letters") unless chars.uniq == chars
|
12
|
+
if chars.length < 26
|
13
|
+
chars + (LATIN - chars)
|
14
|
+
else
|
15
|
+
chars
|
16
|
+
end
|
17
|
+
end
|
6
18
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Ciphers
|
2
|
+
class Autokey
|
3
|
+
def initialize(alphabet: Ciphers::LATIN, key: )
|
4
|
+
alphabet = alphabet.chars if alphabet.is_a? String
|
5
|
+
@alphabet = alphabet.to_a.freeze
|
6
|
+
@key = key.upcase.freeze
|
7
|
+
end
|
8
|
+
|
9
|
+
def encrypt(string)
|
10
|
+
string.each_char.with_index.with_object('') do |(char, i), ret|
|
11
|
+
ret << char and next unless alphabet.include?(char)
|
12
|
+
key_char = "#{key}#{string}"[i]
|
13
|
+
row = table.fetch key_char
|
14
|
+
col = alphabet.index(char)
|
15
|
+
ret << row[col]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def decrypt(string)
|
20
|
+
string.each_char.with_index.with_object('') do |(char, i), ret|
|
21
|
+
ret << char and next unless alphabet.include?(char)
|
22
|
+
key_char = "#{key}#{ret}"[i]
|
23
|
+
row = table.fetch key_char
|
24
|
+
col = row.index(char)
|
25
|
+
ret << alphabet[col]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
attr_reader :alphabet, :key
|
32
|
+
|
33
|
+
def table
|
34
|
+
@table = {}.tap do |hash|
|
35
|
+
alphabet.each_with_index {|char, i| hash[char] = alphabet.rotate(i) }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
data/lib/ciphers/version.rb
CHANGED
data/lib/ciphers/vigenere.rb
CHANGED
@@ -1,18 +1,5 @@
|
|
1
1
|
module Ciphers
|
2
|
-
LATIN = ('A'..'Z').to_a
|
3
|
-
|
4
2
|
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
3
|
def initialize(alphabet: Ciphers::LATIN, key: )
|
17
4
|
alphabet = alphabet.chars if alphabet.is_a? String
|
18
5
|
@alphabet = alphabet.to_a.freeze
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ciphers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Forrest Fleming
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-07-
|
11
|
+
date: 2016-07-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -83,6 +83,7 @@ files:
|
|
83
83
|
- bin/setup
|
84
84
|
- ciphers.gemspec
|
85
85
|
- lib/ciphers.rb
|
86
|
+
- lib/ciphers/autokey.rb
|
86
87
|
- lib/ciphers/version.rb
|
87
88
|
- lib/ciphers/vigenere.rb
|
88
89
|
homepage: https://github.com/ffleming/ciphers
|