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.
@@ -1,80 +1,80 @@
1
- module Ciphr::Functions::Bitwise
2
- class BinaryTruncBitwise < Ciphr::Functions::Function
3
- def apply
4
- input,keyinput = @args
5
- Proc.new do
6
- keychunk = keyinput.read(256)
7
- inchunk = input.read(256)
8
- if inchunk && keychunk
9
- a,b=[inchunk,keychunk].sort_by{|x| x.size}
10
- a.bytes.each_with_index.map{|c,i|c.send(@options[:op], b.bytes.to_a[i%b.size])}.pack("c*")
11
- else
12
- nil
13
- end
14
- end
15
- end
16
-
17
- def self.variants
18
- [
19
- ['and-trunc', {:op=>:&}],
20
- ['or-trunc', {:op=>:|}],
21
- [['xor-trunc'], {:op=>:'^'}]
22
- ]
23
- end
24
-
25
- def self.params
26
- [:input, :input]
27
- end
28
- end
29
-
30
- class BinaryBitwise < Ciphr::Functions::Function
31
- def apply
32
- input,keyinput = @args
33
- keyb, inputb = [keyinput.read.bytes.to_a, input.read.bytes.to_a].sort_by{|a| a.size }
34
- Proc.new do
35
- if inputb
36
- resb = inputb.each_with_index.map{|c,i|c.send(@options[:op], keyb[i%keyb.size])}
37
- res = resb.pack("c*")
38
- inputb = nil
39
- res
40
- else
41
- nil
42
- end
43
- end
44
- end
45
-
46
- def self.variants
47
- [
48
- ['and', {:op=>:&}],
49
- ['or', {:op=>:|}],
50
- [['xor'], {:op=>:'^'}]
51
- ]
52
- end
53
-
54
- def self.params
55
- [:input, :input]
56
- end
57
- end
58
-
59
- class UnaryBitwise < Ciphr::Functions::Function
60
- def apply
61
- input = @args[0]
62
- Proc.new do
63
- inchunk = input.read(1)
64
- if inchunk
65
- inchunk.bytes.map{|b| b = ~b }.pack("c*")
66
- else
67
- nil
68
- end
69
- end
70
- end
71
-
72
- def self.variants
73
- [ ['not', {}] ]
74
- end
75
-
76
- def self.params
77
- [:input]
78
- end
79
- end
80
- end
1
+ module Ciphr::Functions::Bitwise
2
+ class BinaryTruncBitwise < Ciphr::Functions::Function
3
+ def apply
4
+ input,keyinput = @args
5
+ Proc.new do
6
+ keychunk = keyinput.read(256)
7
+ inchunk = input.read(256)
8
+ if inchunk && keychunk
9
+ a,b=[inchunk,keychunk].sort_by{|x| x.size}
10
+ a.bytes.each_with_index.map{|c,i|c.send(@options[:op], b.bytes.to_a[i%b.size])}.pack("c*")
11
+ else
12
+ nil
13
+ end
14
+ end
15
+ end
16
+
17
+ def self.variants
18
+ [
19
+ ['and-trunc', {:op=>:&}],
20
+ ['or-trunc', {:op=>:|}],
21
+ [['xor-trunc'], {:op=>:'^'}]
22
+ ]
23
+ end
24
+
25
+ def self.params
26
+ [:input, :input]
27
+ end
28
+ end
29
+
30
+ class BinaryBitwise < Ciphr::Functions::Function
31
+ def apply
32
+ input,keyinput = @args
33
+ keyb, inputb = [keyinput.read.bytes.to_a, input.read.bytes.to_a].sort_by{|a| a.size }
34
+ Proc.new do
35
+ if inputb
36
+ resb = inputb.each_with_index.map{|c,i|c.send(@options[:op], keyb[i%keyb.size])}
37
+ res = resb.pack("c*")
38
+ inputb = nil
39
+ res
40
+ else
41
+ nil
42
+ end
43
+ end
44
+ end
45
+
46
+ def self.variants
47
+ [
48
+ ['and', {:op=>:&}],
49
+ ['or', {:op=>:|}],
50
+ [['xor'], {:op=>:'^'}]
51
+ ]
52
+ end
53
+
54
+ def self.params
55
+ [:input, :input]
56
+ end
57
+ end
58
+
59
+ class UnaryBitwise < Ciphr::Functions::Function
60
+ def apply
61
+ input = @args[0]
62
+ Proc.new do
63
+ inchunk = input.read(1)
64
+ if inchunk
65
+ inchunk.bytes.map{|b| b = ~b }.pack("c*")
66
+ else
67
+ nil
68
+ end
69
+ end
70
+ end
71
+
72
+ def self.variants
73
+ [ ['not', {}] ]
74
+ end
75
+
76
+ def self.params
77
+ [:input]
78
+ end
79
+ end
80
+ end
@@ -1,44 +1,42 @@
1
- module Ciphr::Functions::Crypto
2
- class RC4Cipher < Ciphr::Functions::InvertibleFunction
3
- def apply
4
- input, key = @args
5
- keybytes = key.read.unpack('c*')
6
- s = (0..255).to_a
7
- j = 0
8
- (0..255).each do |i|
9
- j = (j + s[i] + keybytes[i % keybytes.size]) % 256
10
- swp = s[i]
11
- s[i] = s[j]
12
- s[j] = swp
13
- end
14
- i = 0
15
- j = 0
16
-
17
- $stderr.puts("key: #{keybytes.inspect}")
18
-
19
- Proc.new do
20
- byte = input.read(1)
21
- if byte
22
- i = (i + 1) % 256
23
- j = (j + s[i]) % 256
24
- swp = s[i]
25
- s[i] = s[j]
26
- s[j] = swp
27
- k = s[(s[i] + s[j]) % 256]
28
- m = [(byte.unpack('c*')[0] ^ k)].pack('c*')
29
- m
30
- else
31
- nil
32
- end
33
- end
34
- end
35
-
36
- def self.variants
37
- [[['rc4-ruby'],{}]]
38
- end
39
-
40
- def self.params
41
- [:input, :key]
42
- end
43
- end
44
- end
1
+ module Ciphr::Functions::Crypto
2
+ class RC4Cipher < Ciphr::Functions::InvertibleFunction
3
+ def apply
4
+ input, key = @args
5
+ keybytes = key.read.unpack('c*')
6
+ s = (0..255).to_a
7
+ j = 0
8
+ (0..255).each do |i|
9
+ j = (j + s[i] + keybytes[i % keybytes.size]) % 256
10
+ swp = s[i]
11
+ s[i] = s[j]
12
+ s[j] = swp
13
+ end
14
+ i = 0
15
+ j = 0
16
+
17
+ Proc.new do
18
+ byte = input.read(1)
19
+ if byte
20
+ i = (i + 1) % 256
21
+ j = (j + s[i]) % 256
22
+ swp = s[i]
23
+ s[i] = s[j]
24
+ s[j] = swp
25
+ k = s[(s[i] + s[j]) % 256]
26
+ m = [(byte.unpack('c*')[0] ^ k)].pack('c*')
27
+ m
28
+ else
29
+ nil
30
+ end
31
+ end
32
+ end
33
+
34
+ def self.variants
35
+ [[['rc4-ruby'],{}]]
36
+ end
37
+
38
+ def self.params
39
+ [:input, :key]
40
+ end
41
+ end
42
+ end
@@ -1,104 +1,104 @@
1
- require 'openssl'
2
-
3
- module Ciphr::Functions::OpenSSL
4
- OPENSSL_DIGESTS = %w(md4 md5 sha sha1 sha224 sha256 sha384 sha512) # no md2
5
- #TODO: fail/ignore gracefully with error/warning if openssl unavailable
6
-
7
- class OpenSslDigest < Ciphr::Functions::Function
8
- def self.variants
9
- OPENSSL_DIGESTS.map{|d| [d, {:variant => d}]}
10
- end
11
-
12
- def self.params
13
- [:input]
14
- end
15
-
16
- def apply
17
- input = args[0]
18
- digester = OpenSSL::Digest.new(@options[:variant])
19
- while chunk = input.read(256)
20
- digester.update(chunk)
21
- end
22
- digest = digester.digest
23
- Proc.new do
24
- d = digest
25
- digest = nil
26
- d
27
- end
28
- end
29
- end
30
-
31
- class OpenSslHmac < OpenSslDigest
32
- def self.variants
33
- OPENSSL_DIGESTS.map{|d| [["hmac-#{d}", "hmac#{d}"], {:variant => d}]}
34
- end
35
-
36
- def self.params
37
- [:input, :key]
38
- end
39
-
40
- # reuse code from Digest.apply
41
- def apply
42
- input, key = @args
43
- digester = OpenSSL::HMAC.new(key.read, @options[:variant])
44
- while chunk = input.read(256)
45
- digester.update(chunk)
46
- end
47
- digest = digester.digest
48
- Proc.new do
49
- d = digest
50
- digest = nil
51
- d
52
- end
53
- end
54
- end
55
-
56
-
57
-
58
- class OpenSslCipher < Ciphr::Functions::InvertibleFunction
59
- def apply
60
- input, key = @args
61
- cipher = OpenSSL::Cipher.new(@options[:variant])
62
- cipher.send(invert ? :decrypt : :encrypt)
63
- cipher.key = key.read
64
- random_iv = cipher.random_iv
65
- if random_iv.size > 0
66
- cipher.iv = invert ? input.read(random_iv.size) : random_iv
67
- end
68
- Proc.new do
69
- if ! invert && random_iv
70
- begin
71
- random_iv
72
- ensure
73
- random_iv = nil
74
- end
75
- else
76
- chunk = input.read(256)
77
- if cipher
78
- if chunk
79
- cipher.update(chunk)
80
- else
81
- begin
82
- cipher.final
83
- ensure
84
- cipher = nil
85
- end
86
- end
87
- else
88
- nil
89
- end
90
- end
91
- end
92
- end
93
-
94
- def self.variants
95
- OpenSSL::Cipher.ciphers.map{|c| c.downcase}.uniq.map do |c|
96
- [[c, c.gsub(/-/, "")], {:variant => c}]
97
- end
98
- end
99
-
100
- def self.params
101
- [:input, :key]
102
- end
103
- end
104
- end
1
+ require 'openssl'
2
+
3
+ module Ciphr::Functions::OpenSSL
4
+ OPENSSL_DIGESTS = %w(md4 md5 sha sha1 sha224 sha256 sha384 sha512) # no md2
5
+ #TODO: fail/ignore gracefully with error/warning if openssl unavailable
6
+
7
+ class OpenSslDigest < Ciphr::Functions::Function
8
+ def self.variants
9
+ OPENSSL_DIGESTS.map{|d| [d, {:variant => d}]}
10
+ end
11
+
12
+ def self.params
13
+ [:input]
14
+ end
15
+
16
+ def apply
17
+ input = args[0]
18
+ digester = OpenSSL::Digest.new(@options[:variant])
19
+ while chunk = input.read(256)
20
+ digester.update(chunk)
21
+ end
22
+ digest = digester.digest
23
+ Proc.new do
24
+ d = digest
25
+ digest = nil
26
+ d
27
+ end
28
+ end
29
+ end
30
+
31
+ class OpenSslHmac < OpenSslDigest
32
+ def self.variants
33
+ OPENSSL_DIGESTS.map{|d| [["hmac-#{d}", "hmac#{d}"], {:variant => d}]}
34
+ end
35
+
36
+ def self.params
37
+ [:input, :key]
38
+ end
39
+
40
+ # reuse code from Digest.apply
41
+ def apply
42
+ input, key = @args
43
+ digester = OpenSSL::HMAC.new(key.read, @options[:variant])
44
+ while chunk = input.read(256)
45
+ digester.update(chunk)
46
+ end
47
+ digest = digester.digest
48
+ Proc.new do
49
+ d = digest
50
+ digest = nil
51
+ d
52
+ end
53
+ end
54
+ end
55
+
56
+
57
+
58
+ class OpenSslCipher < Ciphr::Functions::InvertibleFunction
59
+ def apply
60
+ input, key = @args
61
+ cipher = OpenSSL::Cipher.new(@options[:variant])
62
+ cipher.send(invert ? :decrypt : :encrypt)
63
+ cipher.key = key.read
64
+ random_iv = cipher.random_iv
65
+ if random_iv.size > 0
66
+ cipher.iv = invert ? input.read(random_iv.size) : random_iv
67
+ end
68
+ Proc.new do
69
+ if ! invert && random_iv
70
+ begin
71
+ random_iv
72
+ ensure
73
+ random_iv = nil
74
+ end
75
+ else
76
+ chunk = input.read(256)
77
+ if cipher
78
+ if chunk
79
+ cipher.update(chunk)
80
+ else
81
+ begin
82
+ cipher.final
83
+ ensure
84
+ cipher = nil
85
+ end
86
+ end
87
+ else
88
+ nil
89
+ end
90
+ end
91
+ end
92
+ end
93
+
94
+ def self.variants
95
+ OpenSSL::Cipher.ciphers.map{|c| c.downcase}.uniq.map do |c|
96
+ [[c, c.gsub(/-/, "")], {:variant => c}]
97
+ end
98
+ end
99
+
100
+ def self.params
101
+ [:input, :key]
102
+ end
103
+ end
104
+ end