crypto-toolbox 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/crypto-toolbox.rb +2 -0
- data/lib/crypto-toolbox/crypt_buffer.rb +12 -153
- data/lib/crypto-toolbox/crypt_buffer/concerns/byte_expander.rb +16 -0
- data/lib/crypto-toolbox/crypt_buffer/concerns/byte_manipulation.rb +31 -0
- data/lib/crypto-toolbox/crypt_buffer/concerns/comparable.rb +7 -0
- data/lib/crypto-toolbox/crypt_buffer/concerns/convertable.rb +31 -0
- data/lib/crypto-toolbox/crypt_buffer/concerns/pretty_print.rb +13 -0
- data/lib/crypto-toolbox/crypt_buffer/concerns/xor.rb +50 -0
- metadata +7 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3f804212b6478e63ae1c80d653086b759193cc34
|
4
|
+
data.tar.gz: bf5c217909f1d87299448ef04487b7af66b14c6e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1bb8c435152b2cb257c63b5d7510da9b806dae086aac299033c35ca47aeacee2aaa9932173390e1285e70e146ea23604fc688fba83e8e25527d1e583ad4b6e08
|
7
|
+
data.tar.gz: b81ede33a54fa6e3d407b5f37a136af03379d3eba525755e67f479a54e1dfdb90b7a4f4a833fcc4a0eadfc65861d8821c99f3ec5150e2abb91b18dc94682108b
|
data/lib/crypto-toolbox.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
require 'crypto-toolbox/crypt_buffer.rb'
|
2
|
+
|
2
3
|
require 'crypto-toolbox/key_filter.rb'
|
3
4
|
require 'crypto-toolbox/spell_checker.rb'
|
4
5
|
|
5
6
|
require 'crypto-toolbox/analyzers/vigenere_xor.rb'
|
7
|
+
|
6
8
|
require 'crypto-toolbox/ciphers/caesar.rb'
|
7
9
|
require 'crypto-toolbox/ciphers/rot13.rb'
|
@@ -1,165 +1,24 @@
|
|
1
1
|
require 'aes'
|
2
2
|
require 'openssl'
|
3
3
|
require 'forwardable'
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
end
|
12
|
-
|
13
|
-
module ByteExpander
|
14
|
-
private
|
15
|
-
def expand_bytes(input,total)
|
16
|
-
if input.length >= total
|
17
|
-
input
|
18
|
-
else
|
19
|
-
n = total / input.length
|
20
|
-
rest = total % input.length
|
21
|
-
|
22
|
-
# expand the input to the full length of the internal data
|
23
|
-
(input * n) + input[0,rest]
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
module Convertable
|
29
|
-
def hex
|
30
|
-
bytes2hex(bytes).upcase
|
31
|
-
end
|
32
|
-
|
33
|
-
alias_method :h, :hex
|
34
|
-
|
35
|
-
def chars
|
36
|
-
map{|b| b.to_i.chr}
|
37
|
-
end
|
38
|
-
alias_method :c, :chars
|
39
|
-
|
40
|
-
def str
|
41
|
-
chars.join
|
42
|
-
end
|
43
|
-
alias_method :s, :str
|
44
|
-
|
45
|
-
def bits
|
46
|
-
map{|b| "%08d" % b.to_s(2) }
|
47
|
-
end
|
48
|
-
|
49
|
-
def to_s
|
50
|
-
str
|
51
|
-
end
|
52
|
-
private
|
53
|
-
def bytes2hex(bytes)
|
54
|
-
bytes.map{|b| b.to_s(16)}.map{|hs| hs.length == 1 ? "0#{hs}" : hs }.join
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
module Xorable
|
59
|
-
|
60
|
-
def xor_at(input,pos)
|
61
|
-
return self if input.nil? || (pos.abs > length)
|
62
|
-
|
63
|
-
case input
|
64
|
-
when Array
|
65
|
-
# map our current data to xor all inputs with the given bytepos.
|
66
|
-
# all other bytes are kept as they were
|
67
|
-
tmp = bytes.map.with_index{|b,i| i == pos ? xor_multiple(b,input) : b }
|
68
|
-
CryptBuffer(tmp)
|
69
|
-
else
|
70
|
-
tmp = bytes
|
71
|
-
tmp[pos] = tmp[pos] ^ input
|
72
|
-
CryptBuffer(tmp)
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
def xor(input,expand_input: false)
|
77
|
-
if expand_input
|
78
|
-
xor_all_with(input)
|
79
|
-
else
|
80
|
-
xor_bytes(bytes_from_any(input))
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
def xor_all_with(input)
|
85
|
-
expanded = expand_bytes(bytes_from_any(input),self.bytes.length)
|
86
|
-
xor_bytes(expanded)
|
87
|
-
end
|
88
|
-
|
89
|
-
|
90
|
-
def xor_space
|
91
|
-
xor(0x20,expand_input: true)
|
92
|
-
end
|
93
|
-
private
|
94
|
-
|
95
|
-
def xor_bytes(byt)
|
96
|
-
len = [self.bytes.size,byt.size].min
|
97
|
-
result = self.bytes[0...len].map.with_index{|b,i| b ^ byt[i] } + self.bytes[len,self.bytes.length - len]
|
98
|
-
self.class.new(result)
|
99
|
-
end
|
100
|
-
|
101
|
-
def xor_hex(hex)
|
102
|
-
x = hex2bytes(hex)
|
103
|
-
xor_bytes(x)
|
104
|
-
end
|
105
|
-
|
106
|
-
end
|
107
|
-
|
108
|
-
module ByteManipulation
|
109
|
-
|
110
|
-
def modulus(mod)
|
111
|
-
real_mod = sanitize_modulus(mod)
|
112
|
-
CryptBuffer( bytes.map{|b| b % real_mod } )
|
113
|
-
end
|
114
|
-
|
115
|
-
def mod_sub(n,mod: 256)
|
116
|
-
tmp = bytes.map do |byte|
|
117
|
-
val = byte.to_bn.mod_sub(n,mod).to_i
|
118
|
-
end
|
119
|
-
CryptBuffer(tmp)
|
120
|
-
end
|
121
|
-
|
122
|
-
def sub(n)
|
123
|
-
CryptBuffer( bytes.map{|byte| byte -n } )
|
124
|
-
end
|
125
|
-
|
126
|
-
def add(n, mod: 256, offset: 0)
|
127
|
-
real_mod = [256,mod].min
|
128
|
-
|
129
|
-
tmp = bytes.map do |b|
|
130
|
-
val = (b + n) % real_mod
|
131
|
-
val >= offset ? val : val+offset
|
132
|
-
end
|
133
|
-
CryptBuffer(tmp)
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
|
138
|
-
module PrettyPrint
|
139
|
-
def pp
|
140
|
-
puts pretty_hexstr
|
141
|
-
end
|
142
|
-
|
143
|
-
private
|
144
|
-
def pretty_hexstr
|
145
|
-
str = h.scan(/.{2}/).to_a.join(" ")
|
146
|
-
"0x#{h.upcase} (#{str.upcase})"
|
147
|
-
end
|
148
|
-
end
|
149
|
-
end
|
150
|
-
|
4
|
+
require 'crypto-toolbox/crypt_buffer/concerns/byte_expander.rb'
|
5
|
+
require 'crypto-toolbox/crypt_buffer/concerns/byte_manipulation.rb'
|
6
|
+
require 'crypto-toolbox/crypt_buffer/concerns/comparable.rb'
|
7
|
+
require 'crypto-toolbox/crypt_buffer/concerns/convertable.rb'
|
8
|
+
require 'crypto-toolbox/crypt_buffer/concerns/xor.rb'
|
9
|
+
require 'crypto-toolbox/crypt_buffer/concerns/pretty_print.rb'
|
151
10
|
|
152
11
|
|
153
12
|
class CryptBuffer
|
154
13
|
class OutOfRangeError < RuntimeError; end
|
155
14
|
|
156
15
|
|
157
|
-
include
|
158
|
-
include
|
159
|
-
include
|
160
|
-
include
|
161
|
-
include
|
162
|
-
include
|
16
|
+
include CryptBufferConcern::Convertable
|
17
|
+
include CryptBufferConcern::Comparable
|
18
|
+
include CryptBufferConcern::Xor
|
19
|
+
include CryptBufferConcern::ByteManipulation
|
20
|
+
include CryptBufferConcern::PrettyPrint
|
21
|
+
include CryptBufferConcern::ByteExpander
|
163
22
|
|
164
23
|
extend Forwardable
|
165
24
|
def_delegators :@bytes, :[], :empty?,:include?, :length
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module CryptBufferConcern
|
2
|
+
module ByteExpander
|
3
|
+
private
|
4
|
+
def expand_bytes(input,total)
|
5
|
+
if input.length >= total
|
6
|
+
input
|
7
|
+
else
|
8
|
+
n = total / input.length
|
9
|
+
rest = total % input.length
|
10
|
+
|
11
|
+
# expand the input to the full length of the internal data
|
12
|
+
(input * n) + input[0,rest]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module CryptBufferConcern
|
2
|
+
module ByteManipulation
|
3
|
+
|
4
|
+
def modulus(mod)
|
5
|
+
real_mod = sanitize_modulus(mod)
|
6
|
+
CryptBuffer( bytes.map{|b| b % real_mod } )
|
7
|
+
end
|
8
|
+
|
9
|
+
def mod_sub(n,mod: 256)
|
10
|
+
tmp = bytes.map do |byte|
|
11
|
+
val = byte.to_bn.mod_sub(n,mod).to_i
|
12
|
+
end
|
13
|
+
CryptBuffer(tmp)
|
14
|
+
end
|
15
|
+
|
16
|
+
def sub(n)
|
17
|
+
CryptBuffer( bytes.map{|byte| byte -n } )
|
18
|
+
end
|
19
|
+
|
20
|
+
def add(n, mod: 256, offset: 0)
|
21
|
+
real_mod = [256,mod].min
|
22
|
+
|
23
|
+
tmp = bytes.map do |b|
|
24
|
+
val = (b + n) % real_mod
|
25
|
+
val >= offset ? val : val+offset
|
26
|
+
end
|
27
|
+
CryptBuffer(tmp)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module CryptBufferConcern
|
2
|
+
module Convertable
|
3
|
+
def hex
|
4
|
+
bytes2hex(bytes).upcase
|
5
|
+
end
|
6
|
+
|
7
|
+
alias_method :h, :hex
|
8
|
+
|
9
|
+
def chars
|
10
|
+
map{|b| b.to_i.chr}
|
11
|
+
end
|
12
|
+
alias_method :c, :chars
|
13
|
+
|
14
|
+
def str
|
15
|
+
chars.join
|
16
|
+
end
|
17
|
+
alias_method :s, :str
|
18
|
+
|
19
|
+
def bits
|
20
|
+
map{|b| "%08d" % b.to_s(2) }
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_s
|
24
|
+
str
|
25
|
+
end
|
26
|
+
private
|
27
|
+
def bytes2hex(bytes)
|
28
|
+
bytes.map{|b| b.to_s(16)}.map{|hs| hs.length == 1 ? "0#{hs}" : hs }.join
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module CryptBufferConcern
|
2
|
+
module Xor
|
3
|
+
def xor_at(input,pos)
|
4
|
+
return self if input.nil? || (pos.abs > length)
|
5
|
+
|
6
|
+
case input
|
7
|
+
when Array
|
8
|
+
# map our current data to xor all inputs with the given bytepos.
|
9
|
+
# all other bytes are kept as they were
|
10
|
+
tmp = bytes.map.with_index{|b,i| i == pos ? xor_multiple(b,input) : b }
|
11
|
+
CryptBuffer(tmp)
|
12
|
+
else
|
13
|
+
tmp = bytes
|
14
|
+
tmp[pos] = tmp[pos] ^ input
|
15
|
+
CryptBuffer(tmp)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def xor(input,expand_input: false)
|
20
|
+
if expand_input
|
21
|
+
xor_all_with(input)
|
22
|
+
else
|
23
|
+
xor_bytes(bytes_from_any(input))
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def xor_all_with(input)
|
28
|
+
expanded = expand_bytes(bytes_from_any(input),self.bytes.length)
|
29
|
+
xor_bytes(expanded)
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
def xor_space
|
34
|
+
xor(0x20,expand_input: true)
|
35
|
+
end
|
36
|
+
private
|
37
|
+
|
38
|
+
def xor_bytes(byt)
|
39
|
+
len = [self.bytes.size,byt.size].min
|
40
|
+
result = self.bytes[0...len].map.with_index{|b,i| b ^ byt[i] } + self.bytes[len,self.bytes.length - len]
|
41
|
+
self.class.new(result)
|
42
|
+
end
|
43
|
+
|
44
|
+
def xor_hex(hex)
|
45
|
+
x = hex2bytes(hex)
|
46
|
+
xor_bytes(x)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: crypto-toolbox
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dennis Sivia
|
@@ -52,6 +52,12 @@ files:
|
|
52
52
|
- lib/crypto-toolbox/ciphers/caesar.rb
|
53
53
|
- lib/crypto-toolbox/ciphers/rot13.rb
|
54
54
|
- lib/crypto-toolbox/crypt_buffer.rb
|
55
|
+
- lib/crypto-toolbox/crypt_buffer/concerns/byte_expander.rb
|
56
|
+
- lib/crypto-toolbox/crypt_buffer/concerns/byte_manipulation.rb
|
57
|
+
- lib/crypto-toolbox/crypt_buffer/concerns/comparable.rb
|
58
|
+
- lib/crypto-toolbox/crypt_buffer/concerns/convertable.rb
|
59
|
+
- lib/crypto-toolbox/crypt_buffer/concerns/pretty_print.rb
|
60
|
+
- lib/crypto-toolbox/crypt_buffer/concerns/xor.rb
|
55
61
|
- lib/crypto-toolbox/key_filter.rb
|
56
62
|
- lib/crypto-toolbox/spell_checker.rb
|
57
63
|
homepage: https://github.com/scepticulous/crypto-toolbox
|