Module: Merkle::Util
- Included in:
- AbstractTree, Config, Proof
- Defined in:
- lib/merkle/util.rb
Instance Method Summary collapse
-
#bin_to_hex(data) ⇒ String
Convert binary string
datato hex string. -
#combine_sorted(config, left, right) ⇒ String
Combine two elements(
leftandright) with sort configuration. -
#hex_string?(data) ⇒ Boolean
Check whether
datais hex string or not. -
#hex_to_bin(data) ⇒ String
Convert hex string
datato binary.
Instance Method Details
#bin_to_hex(data) ⇒ String
Convert binary string data to hex string.
26 27 28 29 |
# File 'lib/merkle/util.rb', line 26 def bin_to_hex(data) raise ArgumentError, 'data must be string' unless data.is_a?(String) hex_string?(data) ? data : data.unpack1('H*') end |
#combine_sorted(config, left, right) ⇒ String
Combine two elements(left and right) with sort configuration.
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/merkle/util.rb', line 37 def combine_sorted(config, left, right) raise ArgumentError, "config must be Merkle::Config" unless config.is_a?(Merkle::Config) raise ArgumentError, "left must be string" unless left.is_a?(String) raise ArgumentError, "right must be string" unless right.is_a?(String) if config.sort_hashes lh = left.unpack1('H*') rh = right.unpack1('H*') lh < rh ? left + right : right + left else left + right end end |
#hex_string?(data) ⇒ Boolean
Check whether data is hex string or not.
8 9 10 11 |
# File 'lib/merkle/util.rb', line 8 def hex_string?(data) raise ArgumentError, 'data must be string' unless data.is_a?(String) data.match?(/\A[0-9a-fA-F]+\z/) end |
#hex_to_bin(data) ⇒ String
Convert hex string data to binary.
17 18 19 20 |
# File 'lib/merkle/util.rb', line 17 def hex_to_bin(data) raise ArgumentError, 'data must be string' unless data.is_a?(String) hex_string?(data) ? [data].pack('H*') : data end |