vigenere_cipher 1.0.0.pre
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/lib/vigenere_cipher.rb +64 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 043b255deefad569788fffff7bc26d847958254a
|
4
|
+
data.tar.gz: eddff4ca8a0e55ab0958486d5117296ada373028
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3e98eac79bfbd81645cc0fda1f6eb595baca74effab65a98bc3135d92277ef0a217d815d5d8da0acb49cf701ff4a920a21f4af99178959f961da3e0f14af1232
|
7
|
+
data.tar.gz: 09ca7b6e1e66e424de22b1a1ef0679c595efb7bc1f1edea29efbe6d3a88a4246a5b59759024da0994095f606054614d8f7f7c235c00d96c38f766f5745b55fa5
|
@@ -0,0 +1,64 @@
|
|
1
|
+
class VigenereCipher
|
2
|
+
include Math
|
3
|
+
|
4
|
+
attr_accessor :table_recta
|
5
|
+
|
6
|
+
def initialize(word = nil)
|
7
|
+
word ||= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
8
|
+
@table_recta = word
|
9
|
+
end
|
10
|
+
|
11
|
+
def encode(plain_text, key)
|
12
|
+
|
13
|
+
return nil if key_validate(key) == false
|
14
|
+
|
15
|
+
key_index = 0;
|
16
|
+
plain_text.each_char.inject("") do |s, c|
|
17
|
+
index = get_cipher_char_index_in_table_recta(c, key, key_index)
|
18
|
+
if index.nil?
|
19
|
+
return nil
|
20
|
+
else
|
21
|
+
key_index = next_key_index(key, key_index)
|
22
|
+
s += @table_recta[index]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def decode(cipher_text, key)
|
28
|
+
|
29
|
+
return nil if key_validate(key) == false
|
30
|
+
|
31
|
+
key_index = 0;
|
32
|
+
cipher_text.each_char.inject("") do |s, c|
|
33
|
+
index = get_plain_char_index_in_table_recta(c, key, key_index)
|
34
|
+
if index.nil?
|
35
|
+
return nil
|
36
|
+
else
|
37
|
+
key_index = next_key_index(key, key_index)
|
38
|
+
s += @table_recta[index]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
def key_validate(key)
|
45
|
+
(key.each_char.find{|c| @table_recta.include?(c) == false}) ? false : true
|
46
|
+
end
|
47
|
+
|
48
|
+
def next_key_index(key, current_index)
|
49
|
+
key_index_max = key.length - 1
|
50
|
+
(current_index == key_index_max) ? 0 : current_index.next
|
51
|
+
end
|
52
|
+
|
53
|
+
def get_cipher_char_index_in_table_recta(plain_char, key, key_index)
|
54
|
+
unless @table_recta.index(plain_char).nil?
|
55
|
+
(@table_recta.index(plain_char) + @table_recta.index(key[key_index])) % (@table_recta.length)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def get_plain_char_index_in_table_recta(cipher_char, key, key_index)
|
60
|
+
unless @table_recta.index(cipher_char).nil?
|
61
|
+
(@table_recta.index(cipher_char) - @table_recta.index(key[key_index])) % (@table_recta.length)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: vigenere_cipher
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0.pre
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- tmaekawa
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-10 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: |
|
14
|
+
A gem for vigenère cipher.
|
15
|
+
For detail, See https://github.com/tmaekawa/vigenere_cipher
|
16
|
+
email:
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/vigenere_cipher.rb
|
22
|
+
homepage: https://github.com/tmaekawa/vigenere_cipher
|
23
|
+
licenses:
|
24
|
+
- MIT
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - '>'
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 1.3.1
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 2.0.3
|
43
|
+
signing_key:
|
44
|
+
specification_version: 4
|
45
|
+
summary: Vigenère Cipher library
|
46
|
+
test_files: []
|