crypto-toolbox 0.1.1 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3f804212b6478e63ae1c80d653086b759193cc34
4
- data.tar.gz: bf5c217909f1d87299448ef04487b7af66b14c6e
3
+ metadata.gz: fe24da56b4f073b5f98d3272c04a23c2aadaaf7a
4
+ data.tar.gz: 49231fab5588a34344c78a9a55df286699f374e8
5
5
  SHA512:
6
- metadata.gz: 1bb8c435152b2cb257c63b5d7510da9b806dae086aac299033c35ca47aeacee2aaa9932173390e1285e70e146ea23604fc688fba83e8e25527d1e583ad4b6e08
7
- data.tar.gz: b81ede33a54fa6e3d407b5f37a136af03379d3eba525755e67f479a54e1dfdb90b7a4f4a833fcc4a0eadfc65861d8821c99f3ec5150e2abb91b18dc94682108b
6
+ metadata.gz: 1c10b6b880631787c73340e66cadd4a1abae268e73914b80d09e9463c8b3bf3201e5d9e94d7c4bc64110aa90d60ae7d72a6ba4f9c0f9ee8c01756003f791bd6b
7
+ data.tar.gz: 06c2b6a4e7c8d14118b4e2981014d4d70f0efde4f0ea73c3d0be2101633ee513c3e03de812e2211eee98cbd2a3a8da15ff4cb436bb836b321623a0ce590ab044
@@ -55,7 +55,7 @@ Letter Array include?(A): 76997.0 i/s - 42.73x slower
55
55
  end
56
56
 
57
57
  def convert_shift(shift)
58
- ("A".."Z").to_a.each_with_index.inject({}){|memo,(val,index)| memo[val] = index; memo }[shift]
58
+ ("A".."Z").to_a.each.with_index.with_object({}){|(val,index),hsh| hsh[val] = index }[shift]
59
59
  end
60
60
  end
61
61
 
@@ -1,32 +1,37 @@
1
1
  require 'aes'
2
2
  require 'openssl'
3
3
  require 'forwardable'
4
+
5
+ require 'crypto-toolbox/crypt_buffer/concerns/arithmetic.rb'
4
6
  require 'crypto-toolbox/crypt_buffer/concerns/byte_expander.rb'
5
- require 'crypto-toolbox/crypt_buffer/concerns/byte_manipulation.rb'
6
7
  require 'crypto-toolbox/crypt_buffer/concerns/comparable.rb'
7
8
  require 'crypto-toolbox/crypt_buffer/concerns/convertable.rb'
8
- require 'crypto-toolbox/crypt_buffer/concerns/xor.rb'
9
+ require 'crypto-toolbox/crypt_buffer/concerns/padding.rb'
9
10
  require 'crypto-toolbox/crypt_buffer/concerns/pretty_print.rb'
10
-
11
+ require 'crypto-toolbox/crypt_buffer/concerns/xor.rb'
11
12
 
12
13
  class CryptBuffer
13
14
  class OutOfRangeError < RuntimeError; end
14
-
15
-
15
+
16
+ include CryptBufferConcern::Arithmetic
17
+ include CryptBufferConcern::ByteExpander
16
18
  include CryptBufferConcern::Convertable
17
19
  include CryptBufferConcern::Comparable
18
- include CryptBufferConcern::Xor
19
- include CryptBufferConcern::ByteManipulation
20
+ include CryptBufferConcern::Padding
20
21
  include CryptBufferConcern::PrettyPrint
21
- include CryptBufferConcern::ByteExpander
22
+ include CryptBufferConcern::Xor
23
+
22
24
 
25
+ include Enumerable
23
26
  extend Forwardable
24
- def_delegators :@bytes, :[], :empty?,:include?, :length
25
-
27
+ def_delegators :@bytes, :[], :empty?,:include?, :each, :length
26
28
 
29
+
27
30
  attr_accessor :bytes
28
31
  alias_method :b, :bytes
29
32
 
33
+
34
+
30
35
 
31
36
  def initialize(input)
32
37
  @bytes = bytes_from_any(input)
@@ -43,12 +48,6 @@ class CryptBuffer
43
48
  CryptBuffer.new(hexstr)
44
49
  end
45
50
 
46
- include Enumerable
47
- def each(&block)
48
- @bytes.each(&block)
49
- end
50
-
51
-
52
51
  # Returns an array of the nth least sigificant by bit of each byte
53
52
  def nth_bits(n)
54
53
  raise OutOfRangeError if n < 0
@@ -61,6 +60,8 @@ class CryptBuffer
61
60
  self.bytes.each_slice(n).map{|chunk| CryptBuffer(chunk) }
62
61
  end
63
62
 
63
+
64
+
64
65
 
65
66
  private
66
67
  def sanitize_modulus(mod)
@@ -70,55 +71,60 @@ class CryptBuffer
70
71
  def xor_multiple(byte,bytes)
71
72
  ([byte] + bytes).reduce(:^)
72
73
  end
74
+
73
75
  def bytes_from_any(input)
74
76
  case input
75
- when Array
76
- input
77
- when String
78
- if input.match(/^0x[0-9a-fA-F]+$/).nil?
79
- str2bytes(input)
80
- else
81
- hex2bytes(normalize_hex(input))
82
- end
83
- when CryptBuffer
84
- input.b
85
- when Fixnum
86
- # integers as strings dont have a 0x prefix
87
- if input.to_s(16).match(/^[0-9a-fA-F]+$/)
88
- # assume 0x prefixed integer
89
- hex2bytes(normalize_hex(input.to_s(16)))
90
- else
91
- # regular number
92
- [input].pack('C*').bytes
93
- end
94
- else
95
- raise "Unsupported input: #{input.inspect} of class #{input.class}"
77
+ when Array
78
+ input
79
+ when String
80
+ str2bytes(input)
81
+ when CryptBuffer
82
+ input.b
83
+ when Fixnum
84
+ int2bytes(input)
85
+ else
86
+ raise "Unsupported input: #{input.inspect} of class #{input.class}"
96
87
  end
97
88
  end
98
89
 
99
- def self.pad_hex_char(str)
100
- (str.length == 1) ? "0#{str}" : "#{str}"
101
- end
102
90
  def normalize_hex(str)
103
91
  tmp = self.class.pad_hex_char(str)
104
92
  tmp.gsub(/(^0x|\s)/,"").upcase
105
93
  end
106
-
94
+
95
+ def self.pad_hex_char(str)
96
+ (str.length == 1) ? "0#{str}" : "#{str}"
97
+ end
98
+
107
99
  def strip_hex_prefix(hex)
108
100
  raise "remove 0x from hexinput"
109
101
  end
110
-
102
+
103
+ def int2bytes(input)
104
+ # integers as strings dont have a 0x prefix
105
+ if input.to_s(16).match(/^[0-9a-fA-F]+$/)
106
+ # assume 0x prefixed integer
107
+ hex2bytes(normalize_hex(input.to_s(16)))
108
+ else
109
+ # regular number
110
+ [input].pack('C*').bytes
111
+ end
112
+ end
113
+
111
114
  def hex2bytes(hexstr)
112
115
  hexstr.scan(/../).map{|h| h.to_i(16) }
113
116
  end
114
117
 
115
118
  def str2bytes(str)
116
- str.bytes.to_a
119
+ if str.match(/^0x[0-9a-fA-F]+$/).nil?
120
+ str.bytes.to_a
121
+ else
122
+ hex2bytes(normalize_hex(str))
123
+ end
117
124
  end
118
-
119
-
120
125
  end
121
126
 
127
+
122
128
  def CryptBuffer(input)
123
129
  CryptBuffer.new(input)
124
130
  end
@@ -1,5 +1,5 @@
1
1
  module CryptBufferConcern
2
- module ByteManipulation
2
+ module Arithmetic
3
3
 
4
4
  def modulus(mod)
5
5
  real_mod = sanitize_modulus(mod)
@@ -0,0 +1,61 @@
1
+ module CryptBufferConcern
2
+ module Padding
3
+ # This module extends functionality the CryptBuffer to
4
+ # handle PKCS7 padding.
5
+ # It has the ability to detect, replace, add and strip a
6
+ # padding from a CryptBuffer to return a new one without
7
+ # mutating the existing buffer.
8
+ #
9
+ # The purpose is making crypto analysis of cbc and other
10
+ # cipher modes that use pkcs7 padding easier.
11
+
12
+ # Return any existing padding
13
+ def padding
14
+ last = bytes.last
15
+ subset = subset_padding
16
+
17
+ if subset.all?{|e| e == last }
18
+ self.class.new(subset)
19
+ else
20
+ self.class.new([])
21
+ end
22
+ end
23
+
24
+ # Strip the existing padding if present
25
+ def strip_padding
26
+ subset = bytes
27
+
28
+ if padding?
29
+ pad = padding
30
+ len = pad.length
31
+ subset = bytes[0,bytes.length - len]
32
+ end
33
+ self.class.new(subset)
34
+ end
35
+
36
+
37
+ def padding?
38
+ !padding.empty?
39
+ end
40
+
41
+ # pad an existing buffer with the given amount of bytes
42
+ # If a padding already exists, replace: decides whether or not
43
+ # to replace it
44
+ def pad(n,replace: true)
45
+ if padding? && replace
46
+ strip_padding.pad(n)
47
+ else
48
+ pad = [n] * n
49
+ return CryptBuffer(bytes + pad )
50
+ end
51
+ end
52
+
53
+ private
54
+ def subset_padding
55
+ last = bytes.last
56
+ return [] if last.nil?
57
+ # e.g. 5: take from -5, 5 elems
58
+ bytes[-1 * last, last]
59
+ end
60
+ end
61
+ 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.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dennis Sivia
@@ -52,10 +52,11 @@ 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/arithmetic.rb
55
56
  - lib/crypto-toolbox/crypt_buffer/concerns/byte_expander.rb
56
- - lib/crypto-toolbox/crypt_buffer/concerns/byte_manipulation.rb
57
57
  - lib/crypto-toolbox/crypt_buffer/concerns/comparable.rb
58
58
  - lib/crypto-toolbox/crypt_buffer/concerns/convertable.rb
59
+ - lib/crypto-toolbox/crypt_buffer/concerns/padding.rb
59
60
  - lib/crypto-toolbox/crypt_buffer/concerns/pretty_print.rb
60
61
  - lib/crypto-toolbox/crypt_buffer/concerns/xor.rb
61
62
  - lib/crypto-toolbox/key_filter.rb