ctf-party 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aa3a85e4dd2db7d636cd1e6f66ca0b32fb34567886901b1102eae35fcb1fbb1c
4
- data.tar.gz: aa6c2db8fb07651887d5711d0bfb6b544d422c4f091aaf1893cc6d345e467cc6
3
+ metadata.gz: cc518fdaca11359e723f1a25e93f159a791efb951098c6ddaf0dde1af2226476
4
+ data.tar.gz: b18d37b97167e931c5e5a82303fda9ece61d8c3e460b57c8b280d80f1007be8d
5
5
  SHA512:
6
- metadata.gz: b1eebe8e48be92a2f4ce8141f6c41a8f4023a3db5d4091358b41a41f5f3b4d123552e382c323d80e29db256818934b2ca8f036e23af10f56a4c1a04861a4f9d9
7
- data.tar.gz: 067e1e47c7bad059917fa3774c9d5f37eb9ba37dcd0d8df0320a01bffeccae90a1958d8de8d41d6308040f5cc21e07025a5a34f4b6ef9ce8165ebb7246349134
6
+ metadata.gz: 9faca804bb4b67d01553d11516ba50eecfceff4d2ffad6c2a38c1db072b31e90b7740226637f45fca2b431d342be21457a4bc4ffd5f25fea9d015ff93ed89625
7
+ data.tar.gz: fd5d70b588e54e6c863532ea2b11955d551242331b0e8c68f611ff6287bbb1259e6ebaf824b73310026c3ff93e4234c606abe0e118f3dba2d774d4d9a66a3d1a
data/lib/ctf_party/hex.rb CHANGED
@@ -131,4 +131,61 @@ class String
131
131
  def from_hex!(opts = {})
132
132
  replace(from_hex(opts))
133
133
  end
134
+
135
+ # Encode an hexadecimal string to a binary string
136
+ # @param opts [Hash] optional parameters
137
+ # @option opts [String] :prefix Prefix of the input. Default value is a void
138
+ # string. Example of values: +0x+, +\x+.
139
+ # @return [String] the binary encoded string
140
+ # @example
141
+ # 'ab'.hex2bin # => "10101011"
142
+ # '\xf3'.hex2bin(prefix: '\x') # => "11110011"
143
+ def hex2bin(opts = {})
144
+ opts[:prefix] ||= ''
145
+ # remove prefix
146
+ out = sub(opts[:prefix], '')
147
+ # convert
148
+ return out.to_i(16).to_s(2)
149
+ end
150
+
151
+ # Encode an hexadecimal string to a binary string in place as described
152
+ # for {String#hex2bin}.
153
+ # @example
154
+ # a = 'ff'
155
+ # a.hex2bin!
156
+ # a # => => "11111111"
157
+ def hex2bin!(opts = {})
158
+ replace(hex2bin(opts))
159
+ end
160
+
161
+ # Encode an binary string to a hexadecimal string
162
+ # @param opts [Hash] optional parameters
163
+ # @option opts [String] :prefix Prefix of the output. Default value is a void
164
+ # string. Example of values: +0x+, +\x+.
165
+ # @option opts [Symbol] :case Char case of the ouput. Default value +:lower+.
166
+ # Other valid value +:upper+.
167
+ # @return [String] the hexadecimal encoded string
168
+ # @example
169
+ # '11110011'.bin2hex # => "f3"
170
+ # '11110011'.bin2hex({prefix: '0x', case: :upper}) # => "0xF3"
171
+ def bin2hex(opts = {})
172
+ opts[:prefix] ||= ''
173
+ opts[:case] ||= :lower
174
+ # convert
175
+ out = to_i(2).to_s(16)
176
+ # char case management
177
+ out = out.upcase if opts[:case] == :upper
178
+ # adding prefix must be done after case change
179
+ return opts[:prefix] + out
180
+ end
181
+
182
+ # Encode an binary string to a hexadecimal string in place as described
183
+ # for {String#bin2hex}.
184
+ # @example
185
+ # a = '11110011'
186
+ # a.bin2hex!
187
+ # a # => "f3"
188
+ def bin2hex!(opts = {})
189
+ replace(bin2hex(opts))
190
+ end
134
191
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Version
4
- VERSION = '1.1.0'
4
+ VERSION = '1.2.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ctf-party
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandre ZANNI