ciphr 0.0.2 → 0.0.4
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/Gemfile +4 -4
- data/Gemfile.lock +6 -3
- data/LICENSE.txt +22 -22
- data/README.md +13 -4
- data/Rakefile +7 -7
- data/TODO +320 -320
- data/bin/ciphr +3 -1
- data/ciphr.gemspec +0 -0
- data/lib/ciphr.rb +2 -0
- data/lib/ciphr/function_registry.rb +0 -0
- data/lib/ciphr/functions.rb +63 -62
- data/lib/ciphr/functions/ascii.rb +25 -0
- data/lib/ciphr/functions/base_radix.rb +205 -205
- data/lib/ciphr/functions/bitwise.rb +80 -80
- data/lib/ciphr/functions/crypto.rb +42 -44
- data/lib/ciphr/functions/openssl.rb +104 -104
- data/lib/ciphr/functions/reader.rb +46 -44
- data/lib/ciphr/functions/simple.rb +121 -121
- data/lib/ciphr/functions/url.rb +38 -38
- data/lib/ciphr/functions/zlib.rb +95 -94
- data/lib/ciphr/parser.rb +0 -0
- data/lib/ciphr/stream.rb +49 -49
- data/lib/ciphr/version.rb +1 -1
- data/pkg/ciphr-0.0.3.gem +0 -0
- data/spec/ciphr_spec.rb +21 -21
- data/spec/functions_spec.rb +31 -31
- data/spec/randomizer_spec.rb +55 -55
- data/spec/spec_helper.rb +4 -4
- data/spec/stream_spec.rb +38 -38
- data/tests/output.r26.2.0.0.bin +0 -0
- data/tests/output.r26.2.1.1.bin +0 -0
- data/tests/output.r30.2.0.0.bin +0 -0
- data/tests/output.r30.2.1.1.bin +0 -0
- data/tests/output.r34.2.0.0.bin +0 -0
- data/tests/output.r34.2.1.1.bin +0 -0
- data/tests/testcase.r26.bin +0 -0
- data/tests/testcase.r30.bin +0 -0
- data/tests/testcase.r34.bin +0 -0
- metadata +5 -6
- data/pkg/ciphr-0.0.1.gem +0 -0
- data/pkg/ciphr-0.0.2.gem +0 -0
@@ -1,44 +1,46 @@
|
|
1
|
-
# strictly used by parser classes for literals/wiring
|
2
|
-
# TODO: disable registration
|
3
|
-
|
4
|
-
module Ciphr::Functions::Reader
|
5
|
-
class StringReader < Ciphr::Functions::Function
|
6
|
-
def apply
|
7
|
-
StringProc.new(options[:string])
|
8
|
-
end
|
9
|
-
|
10
|
-
class StringProc #extend Proc?
|
11
|
-
def initialize(str)
|
12
|
-
@str = str
|
13
|
-
end
|
14
|
-
|
15
|
-
def call
|
16
|
-
begin
|
17
|
-
@str
|
18
|
-
ensure
|
19
|
-
@str = nil
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
class FileReader < Ciphr::Functions::Function
|
26
|
-
def apply
|
27
|
-
f = File.open(options[:file], "r")
|
28
|
-
|
29
|
-
|
30
|
-
f.
|
31
|
-
chunk
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
end
|
1
|
+
# strictly used by parser classes for literals/wiring
|
2
|
+
# TODO: disable registration
|
3
|
+
|
4
|
+
module Ciphr::Functions::Reader
|
5
|
+
class StringReader < Ciphr::Functions::Function
|
6
|
+
def apply
|
7
|
+
StringProc.new(options[:string])
|
8
|
+
end
|
9
|
+
|
10
|
+
class StringProc #extend Proc?
|
11
|
+
def initialize(str)
|
12
|
+
@str = str
|
13
|
+
end
|
14
|
+
|
15
|
+
def call
|
16
|
+
begin
|
17
|
+
@str
|
18
|
+
ensure
|
19
|
+
@str = nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class FileReader < Ciphr::Functions::Function
|
26
|
+
def apply
|
27
|
+
f = File.open(options[:file], "r")
|
28
|
+
f.binmode
|
29
|
+
Proc.new do
|
30
|
+
chunk = f.read(256)
|
31
|
+
f.close if ! chunk
|
32
|
+
chunk
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class IoReader < Ciphr::Functions::Function
|
38
|
+
def apply
|
39
|
+
input = args[0]
|
40
|
+
input.binmode
|
41
|
+
Proc.new do
|
42
|
+
input.read(256)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -1,121 +1,121 @@
|
|
1
|
-
module Ciphr::Functions::Simple
|
2
|
-
class Cat < Ciphr::Functions::Function
|
3
|
-
def self.variants
|
4
|
-
[[['cat','catenate'], {}]]
|
5
|
-
end
|
6
|
-
|
7
|
-
def self.params
|
8
|
-
[:input]
|
9
|
-
end
|
10
|
-
|
11
|
-
def apply
|
12
|
-
inputs = @args
|
13
|
-
i = 0
|
14
|
-
chunk = nil
|
15
|
-
Proc.new do
|
16
|
-
chunk = inputs[i].read(256)
|
17
|
-
if ! chunk
|
18
|
-
i += 1
|
19
|
-
chunk = inputs[i] && inputs[i].read(256)
|
20
|
-
end
|
21
|
-
#while !(chunk = inputs[i].read(256)) && i < inputs.size
|
22
|
-
# i++
|
23
|
-
#end
|
24
|
-
chunk
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
class Repack < Ciphr::Functions::Function
|
30
|
-
def apply
|
31
|
-
input, ch1in, ch2in = @args
|
32
|
-
content, ch1, ch2 = [input.read, ch1in.read, ch2in.read]
|
33
|
-
Proc.new do
|
34
|
-
if content
|
35
|
-
begin
|
36
|
-
content.unpack(ch1).pack(ch2)
|
37
|
-
ensure
|
38
|
-
content = nil
|
39
|
-
end
|
40
|
-
else
|
41
|
-
nil
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def self.variants
|
47
|
-
[ [['repack'], {}] ]
|
48
|
-
end
|
49
|
-
|
50
|
-
def self.params
|
51
|
-
[:input,:ch1,:ch2]
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
class Translate < Ciphr::Functions::Function
|
56
|
-
def apply
|
57
|
-
input, ch1in, ch2in = @args
|
58
|
-
ch1, ch2 = [ch1in.read, ch2in.read]
|
59
|
-
Proc.new do
|
60
|
-
inchunk = input.read(1)
|
61
|
-
if inchunk
|
62
|
-
inchunk.tr(ch1, ch2)
|
63
|
-
else
|
64
|
-
nil
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
def self.variants
|
70
|
-
[ [['tr','translate'], {}] ]
|
71
|
-
end
|
72
|
-
|
73
|
-
def self.params
|
74
|
-
[:input,:ch1,:ch2]
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
class Replace < Ciphr::Functions::Function
|
79
|
-
def apply
|
80
|
-
input, searchin, replacein = @args
|
81
|
-
search, replace = [searchin.read, replacein.read]
|
82
|
-
buf = ""
|
83
|
-
Proc.new do
|
84
|
-
if buf.size == search.size && search.size > 0
|
85
|
-
buf = ""
|
86
|
-
replace
|
87
|
-
else
|
88
|
-
inchunk = input.read(1)
|
89
|
-
if inchunk
|
90
|
-
if inchunk == search[buf.size]
|
91
|
-
buf += inchunk
|
92
|
-
""
|
93
|
-
else
|
94
|
-
buf += inchunk
|
95
|
-
input.prepend(buf[1,buf.size])
|
96
|
-
ret = buf[0]
|
97
|
-
buf = ""
|
98
|
-
ret
|
99
|
-
end
|
100
|
-
else
|
101
|
-
if buf.size > 0
|
102
|
-
ret = buf
|
103
|
-
buf = ""
|
104
|
-
ret
|
105
|
-
else
|
106
|
-
nil
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
def self.variants
|
114
|
-
[ [['repl','replace'], {}] ]
|
115
|
-
end
|
116
|
-
|
117
|
-
def self.params
|
118
|
-
[:input,:search,:replace]
|
119
|
-
end
|
120
|
-
end
|
121
|
-
end
|
1
|
+
module Ciphr::Functions::Simple
|
2
|
+
class Cat < Ciphr::Functions::Function
|
3
|
+
def self.variants
|
4
|
+
[[['cat','catenate'], {}]]
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.params
|
8
|
+
[:input]
|
9
|
+
end
|
10
|
+
|
11
|
+
def apply
|
12
|
+
inputs = @args
|
13
|
+
i = 0
|
14
|
+
chunk = nil
|
15
|
+
Proc.new do
|
16
|
+
chunk = inputs[i].read(256)
|
17
|
+
if ! chunk
|
18
|
+
i += 1
|
19
|
+
chunk = inputs[i] && inputs[i].read(256)
|
20
|
+
end
|
21
|
+
#while !(chunk = inputs[i].read(256)) && i < inputs.size
|
22
|
+
# i++
|
23
|
+
#end
|
24
|
+
chunk
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class Repack < Ciphr::Functions::Function
|
30
|
+
def apply
|
31
|
+
input, ch1in, ch2in = @args
|
32
|
+
content, ch1, ch2 = [input.read, ch1in.read, ch2in.read]
|
33
|
+
Proc.new do
|
34
|
+
if content
|
35
|
+
begin
|
36
|
+
content.unpack(ch1).pack(ch2)
|
37
|
+
ensure
|
38
|
+
content = nil
|
39
|
+
end
|
40
|
+
else
|
41
|
+
nil
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.variants
|
47
|
+
[ [['repack'], {}] ]
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.params
|
51
|
+
[:input,:ch1,:ch2]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class Translate < Ciphr::Functions::Function
|
56
|
+
def apply
|
57
|
+
input, ch1in, ch2in = @args
|
58
|
+
ch1, ch2 = [ch1in.read, ch2in.read]
|
59
|
+
Proc.new do
|
60
|
+
inchunk = input.read(1)
|
61
|
+
if inchunk
|
62
|
+
inchunk.tr(ch1, ch2)
|
63
|
+
else
|
64
|
+
nil
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.variants
|
70
|
+
[ [['tr','translate'], {}] ]
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.params
|
74
|
+
[:input,:ch1,:ch2]
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
class Replace < Ciphr::Functions::Function
|
79
|
+
def apply
|
80
|
+
input, searchin, replacein = @args
|
81
|
+
search, replace = [searchin.read, replacein.read]
|
82
|
+
buf = ""
|
83
|
+
Proc.new do
|
84
|
+
if buf.size == search.size && search.size > 0
|
85
|
+
buf = ""
|
86
|
+
replace
|
87
|
+
else
|
88
|
+
inchunk = input.read(1)
|
89
|
+
if inchunk
|
90
|
+
if inchunk == search[buf.size]
|
91
|
+
buf += inchunk
|
92
|
+
""
|
93
|
+
else
|
94
|
+
buf += inchunk
|
95
|
+
input.prepend(buf[1,buf.size])
|
96
|
+
ret = buf[0]
|
97
|
+
buf = ""
|
98
|
+
ret
|
99
|
+
end
|
100
|
+
else
|
101
|
+
if buf.size > 0
|
102
|
+
ret = buf
|
103
|
+
buf = ""
|
104
|
+
ret
|
105
|
+
else
|
106
|
+
nil
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def self.variants
|
114
|
+
[ [['repl','replace'], {}] ]
|
115
|
+
end
|
116
|
+
|
117
|
+
def self.params
|
118
|
+
[:input,:search,:replace]
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
data/lib/ciphr/functions/url.rb
CHANGED
@@ -1,38 +1,38 @@
|
|
1
|
-
require 'cgi'
|
2
|
-
|
3
|
-
module Ciphr::Functions::URL
|
4
|
-
#TODO: differentiate between URL and CGI encoding (with '+' char)
|
5
|
-
class UrlEncoding < Ciphr::Functions::InvertibleFunction
|
6
|
-
def apply
|
7
|
-
input = @args[0]
|
8
|
-
if !invert
|
9
|
-
Proc.new do
|
10
|
-
chunk = input.read(1)
|
11
|
-
chunk && CGI.escape(chunk)
|
12
|
-
end
|
13
|
-
else
|
14
|
-
Proc.new do
|
15
|
-
chunk = input.read(1)
|
16
|
-
if (chunk == "%")
|
17
|
-
chunk += input.read(2)
|
18
|
-
chunk && CGI.unescape(chunk)
|
19
|
-
elsif chunk == '+'
|
20
|
-
' '
|
21
|
-
else
|
22
|
-
chunk
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.variants
|
29
|
-
[
|
30
|
-
[['url','uri','cgi'],{}]
|
31
|
-
]
|
32
|
-
end
|
33
|
-
|
34
|
-
def self.params
|
35
|
-
[:input]
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
1
|
+
require 'cgi'
|
2
|
+
|
3
|
+
module Ciphr::Functions::URL
|
4
|
+
#TODO: differentiate between URL and CGI encoding (with '+' char)
|
5
|
+
class UrlEncoding < Ciphr::Functions::InvertibleFunction
|
6
|
+
def apply
|
7
|
+
input = @args[0]
|
8
|
+
if !invert
|
9
|
+
Proc.new do
|
10
|
+
chunk = input.read(1)
|
11
|
+
chunk && CGI.escape(chunk)
|
12
|
+
end
|
13
|
+
else
|
14
|
+
Proc.new do
|
15
|
+
chunk = input.read(1)
|
16
|
+
if (chunk == "%")
|
17
|
+
chunk += input.read(2)
|
18
|
+
chunk && CGI.unescape(chunk)
|
19
|
+
elsif chunk == '+'
|
20
|
+
' '
|
21
|
+
else
|
22
|
+
chunk
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.variants
|
29
|
+
[
|
30
|
+
[['url','uri','cgi'],{}]
|
31
|
+
]
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.params
|
35
|
+
[:input]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/ciphr/functions/zlib.rb
CHANGED
@@ -1,94 +1,95 @@
|
|
1
|
-
require 'zlib'
|
2
|
-
|
3
|
-
module Ciphr::Functions::ZLib
|
4
|
-
class Deflate < Ciphr::Functions::InvertibleFunction
|
5
|
-
def apply
|
6
|
-
input = @args[0]
|
7
|
-
zstream = invert ? Zlib::Inflate.new : Zlib::Deflate.new
|
8
|
-
Proc.new do
|
9
|
-
chunk = input.read(256)
|
10
|
-
if chunk
|
11
|
-
if invert
|
12
|
-
zstream.inflate(chunk)
|
13
|
-
else
|
14
|
-
zstream.deflate(chunk,Zlib::SYNC_FLUSH)
|
15
|
-
end
|
16
|
-
else
|
17
|
-
begin
|
18
|
-
#zstream.finish if invert
|
19
|
-
ensure
|
20
|
-
zstream.close
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def self.variants
|
27
|
-
[
|
28
|
-
[['deflate'], {}]
|
29
|
-
]
|
30
|
-
end
|
31
|
-
|
32
|
-
def self.params
|
33
|
-
[:input]
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
|
38
|
-
class Gzip < Ciphr::Functions::InvertibleFunction
|
39
|
-
class UncloseableIOProxy # hack to prevent GzipWriter from closing StringIO
|
40
|
-
def initialize(delegate)
|
41
|
-
@delegate = delegate
|
42
|
-
end
|
43
|
-
|
44
|
-
def method_missing(meth, *args, &block)
|
45
|
-
if meth.to_s != "close"
|
46
|
-
@delegate.send(meth, *args, &block)
|
47
|
-
else
|
48
|
-
nil
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
def apply
|
54
|
-
input = @args[0]
|
55
|
-
sio = StringIO.new
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
sio.
|
67
|
-
sio.
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
gz
|
72
|
-
|
73
|
-
|
74
|
-
sio.
|
75
|
-
sio.
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
end
|
1
|
+
require 'zlib'
|
2
|
+
|
3
|
+
module Ciphr::Functions::ZLib
|
4
|
+
class Deflate < Ciphr::Functions::InvertibleFunction
|
5
|
+
def apply
|
6
|
+
input = @args[0]
|
7
|
+
zstream = invert ? Zlib::Inflate.new : Zlib::Deflate.new
|
8
|
+
Proc.new do
|
9
|
+
chunk = input.read(256)
|
10
|
+
if chunk
|
11
|
+
if invert
|
12
|
+
zstream.inflate(chunk)
|
13
|
+
else
|
14
|
+
zstream.deflate(chunk,Zlib::SYNC_FLUSH)
|
15
|
+
end
|
16
|
+
else
|
17
|
+
begin
|
18
|
+
#zstream.finish if invert
|
19
|
+
ensure
|
20
|
+
zstream.close
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.variants
|
27
|
+
[
|
28
|
+
[['deflate'], {}]
|
29
|
+
]
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.params
|
33
|
+
[:input]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
class Gzip < Ciphr::Functions::InvertibleFunction
|
39
|
+
class UncloseableIOProxy # hack to prevent GzipWriter from closing StringIO
|
40
|
+
def initialize(delegate)
|
41
|
+
@delegate = delegate
|
42
|
+
end
|
43
|
+
|
44
|
+
def method_missing(meth, *args, &block)
|
45
|
+
if meth.to_s != "close"
|
46
|
+
@delegate.send(meth, *args, &block)
|
47
|
+
else
|
48
|
+
nil
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def apply
|
54
|
+
input = @args[0]
|
55
|
+
sio = StringIO.new
|
56
|
+
sio.binmode
|
57
|
+
gz = !invert ? Zlib::GzipWriter.new(UncloseableIOProxy.new(sio)) : Zlib::GzipReader.new(input)
|
58
|
+
Proc.new do
|
59
|
+
if invert # unzip
|
60
|
+
gz.read(256)
|
61
|
+
else # zip
|
62
|
+
chunk = input.read(256)
|
63
|
+
if chunk
|
64
|
+
gz.write chunk
|
65
|
+
sio.rewind
|
66
|
+
ret = sio.read
|
67
|
+
sio.rewind
|
68
|
+
sio.truncate(0)
|
69
|
+
ret
|
70
|
+
elsif gz
|
71
|
+
gz.close
|
72
|
+
gz = nil
|
73
|
+
sio.rewind
|
74
|
+
ret = sio.read
|
75
|
+
sio.rewind
|
76
|
+
sio.truncate(0)
|
77
|
+
ret
|
78
|
+
else
|
79
|
+
nil
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.variants
|
86
|
+
[
|
87
|
+
[['gzip','gz'], {}]
|
88
|
+
]
|
89
|
+
end
|
90
|
+
|
91
|
+
def self.params
|
92
|
+
[:input]
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|