Module: WOTS::Util
- Included in:
- Param, PrivateKey, PrivateKey, PublicKey, PublicKey, Signature
- Defined in:
- lib/wots/util.rb
Instance Method Summary collapse
-
#bin_to_hex(data) ⇒ String
Convert binary string
datato hex string. -
#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.
28 29 30 31 |
# File 'lib/wots/util.rb', line 28 def bin_to_hex(data) raise ArgumentError, 'data must be string' unless data.is_a?(String) data.unpack1('H*') end |
#hex_string?(data) ⇒ Boolean
Check whether data is hex string or not.
An odd-length string is NOT a valid hex string, since Array#pack would
silently zero-pad it and make two distinct inputs collide.
10 11 12 13 |
# File 'lib/wots/util.rb', line 10 def hex_string?(data) raise ArgumentError, 'data must be string' unless data.is_a?(String) data.length.even? && data.match?(/\A[0-9a-fA-F]+\z/) end |
#hex_to_bin(data) ⇒ String
Convert hex string data to binary.
19 20 21 22 |
# File 'lib/wots/util.rb', line 19 def hex_to_bin(data) raise ArgumentError, 'data must be hex string' unless hex_string?(data) [data].pack('H*') end |