Top Level Namespace
Defined Under Namespace
Modules: AddressCodec, BinaryCodec, Core
Instance Attribute Summary collapse
Instance Method Summary
collapse
Instance Attribute Details
#base_x ⇒ Object
2
|
# File 'lib/core/core.rb', line 2
require_relative 'base_x'
|
Instance Method Details
#bin_to_hex(bin) ⇒ Object
18
19
20
|
# File 'lib/core/core.rb', line 18
def bin_to_hex(bin)
bin.unpack("H*").first.upcase
end
|
#bytes_to_hex(bytes) ⇒ Object
10
11
12
|
# File 'lib/core/core.rb', line 10
def bytes_to_hex(bytes)
bytes.pack('C*').unpack1('H*').upcase
end
|
#check_byte_length(bytes, expected_length) ⇒ Object
40
41
42
43
44
45
46
|
# File 'lib/core/core.rb', line 40
def check_byte_length(bytes, expected_length)
if bytes.respond_to?(:byte_length)
bytes.byte_length == expected_length
else
bytes.length == expected_length
end
end
|
#concat_args(*args) ⇒ Object
48
49
50
51
52
|
# File 'lib/core/core.rb', line 48
def concat_args(*args)
args.flat_map do |arg|
is_scalar?(arg) ? [arg] : arg.to_a
end
end
|
#hex_to_bin(hex) ⇒ Object
22
23
24
25
|
# File 'lib/core/core.rb', line 22
def hex_to_bin(hex)
raise ArgumentError, 'Invalid hex string' unless valid_hex?(hex)
[hex].pack("H*")
end
|
#hex_to_bytes(hex) ⇒ Object
13
14
15
16
|
# File 'lib/core/core.rb', line 13
def hex_to_bytes(hex)
raise ArgumentError, 'Invalid hex string' unless valid_hex?(hex)
[hex].pack('H*').bytes
end
|
#hex_to_string(hex, encoding = 'utf-8') ⇒ Object
27
28
29
30
|
# File 'lib/core/core.rb', line 27
def hex_to_string(hex, encoding = 'utf-8')
raise ArgumentError, 'Invalid hex string' unless valid_hex?(hex)
hex_to_bin(hex).force_encoding(encoding).encode('utf-8')
end
|
#int_to_bytes(number, width = 1, byteorder = :big) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/core/core.rb', line 58
def int_to_bytes(number, width = 1, byteorder = :big)
bytes = []
while number > 0
bytes << (number & 0xFF) number >>= 8 end
while bytes.size < width
bytes << 0
end
bytes.reverse! if byteorder == :big
bytes
end
|
#is_scalar?(val) ⇒ Boolean
54
55
56
|
# File 'lib/core/core.rb', line 54
def is_scalar?(val)
val.is_a?(Numeric)
end
|
#random_bytes(size) ⇒ Object
6
7
8
|
# File 'lib/core/core.rb', line 6
def random_bytes(size)
SecureRandom.random_bytes(size).bytes
end
|
#string_to_hex(string) ⇒ Object
32
33
34
|
# File 'lib/core/core.rb', line 32
def string_to_hex(string)
string.unpack1('H*').upcase
end
|
#valid_hex?(str) ⇒ Boolean
36
37
38
|
# File 'lib/core/core.rb', line 36
def valid_hex?(str)
str =~ /\A[0-9a-fA-F]*\z/ && str.length.even?
end
|