ctf-party 1.1.0 → 1.2.0
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/lib/ctf_party/hex.rb +57 -0
- data/lib/ctf_party/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc518fdaca11359e723f1a25e93f159a791efb951098c6ddaf0dde1af2226476
|
4
|
+
data.tar.gz: b18d37b97167e931c5e5a82303fda9ece61d8c3e460b57c8b280d80f1007be8d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/ctf_party/version.rb
CHANGED