Module: BSV::Primitives::Hex
- Defined in:
- lib/bsv/primitives/hex.rb
Overview
Strict hex encoding/decoding utilities.
Ruby’s Array#pack(‘H*’) silently drops non-hex characters and truncates odd-length strings. This module rejects both, raising ArgumentError on invalid input so consumer-facing parse paths fail loudly rather than producing garbage.
Internal paths that serialise/deserialise trusted hex (e.g. round-tripping our own unpack1(‘H*’) output) can continue using pack(‘H*’) directly — the validation overhead isn’t warranted when the hex is known-good.
Class Method Summary collapse
-
.decode(str, name: 'hex value') ⇒ String
Decode a hex string to binary bytes.
-
.encode(bytes) ⇒ String
Encode binary bytes as lowercase hex.
-
.valid?(str) ⇒ Boolean
Test whether
stris valid hex (even-length, hex-only). -
.validate!(str, name: 'hex value') ⇒ String
Validate
stras hex, raising on failure.
Class Method Details
.decode(str, name: 'hex value') ⇒ String
Decode a hex string to binary bytes.
63 64 65 66 |
# File 'lib/bsv/primitives/hex.rb', line 63 def self.decode(str, name: 'hex value') validate!(str, name: name) [str].pack('H*') end |
.encode(bytes) ⇒ String
Encode binary bytes as lowercase hex.
72 73 74 |
# File 'lib/bsv/primitives/hex.rb', line 72 def self.encode(bytes) bytes.unpack1('H*') end |
.valid?(str) ⇒ Boolean
Test whether str is valid hex (even-length, hex-only).
32 33 34 35 36 |
# File 'lib/bsv/primitives/hex.rb', line 32 def self.valid?(str) str.is_a?(String) && str.match?(HEX_RE) rescue Encoding::CompatibilityError false end |
.validate!(str, name: 'hex value') ⇒ String
Validate str as hex, raising on failure.
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/bsv/primitives/hex.rb', line 44 def self.validate!(str, name: 'hex value') return str if valid?(str) reason = if !str.is_a?(String) "expected String, got #{str.class}" elsif str.length.odd? 'odd length' else 'contains non-hex characters' end raise ArgumentError, "invalid #{name}: #{reason} (#{str.inspect})" end |