Module: Nem::Util::Convert

Defined in:
lib/nem/util/convert.rb

Constant Summary collapse

HEX_ENCODE_ARRAY =
%w[0 1 2 3 4 5 6 7 8 9 a b c d e f]

Class Method Summary collapse

Class Method Details

.bin2hex(bin) ⇒ String

Parameters:

  • bin (Array)

Returns:

  • (String)


41
42
43
# File 'lib/nem/util/convert.rb', line 41

def self.bin2hex(bin)
  bin.map { |v| '%02x' % v }.join
end

.hex2a(hex) ⇒ String

Convert hex to string

Parameters:

  • hex (String)

Returns:

  • (String)


23
24
25
# File 'lib/nem/util/convert.rb', line 23

def self.hex2a(hex)
  hex.scan(/../).inject('') { |memo, el| memo << el.hex.chr }
end

.hex2bin(hex) ⇒ String

Parameters:

  • bin (Array)

Returns:

  • (String)


29
30
31
# File 'lib/nem/util/convert.rb', line 29

def self.hex2bin(hex)
  hex2ua(hex).pack('C*')
end

.hex2bin_rev(hex) ⇒ String

Parameters:

  • bin (Array)

Returns:

  • (String)


35
36
37
# File 'lib/nem/util/convert.rb', line 35

def self.hex2bin_rev(hex)
  hex2ua_rev(hex).pack('C*')
end

.hex2ua(hex) ⇒ Array

Convert hex to Uint8Array

Parameters:

  • hex (String)

Returns:

  • (Array)


16
17
18
# File 'lib/nem/util/convert.rb', line 16

def self.hex2ua(hex)
  hex.scan(/../).map(&:hex)
end

.hex2ua_rev(hex) ⇒ Array

Reversed convertion of hex to Uint8Array

Parameters:

  • hex (String)

Returns:

  • (Array)


9
10
11
# File 'lib/nem/util/convert.rb', line 9

def self.hex2ua_rev(hex)
  hex2ua(hex).reverse
end

.hex_to_utf8(hex_str) ⇒ string

Convert hex to UTF-8

Parameters:

  • str (string)

Returns:

  • (string)


55
56
57
# File 'lib/nem/util/convert.rb', line 55

def self.hex_to_utf8(hex_str)
  [hex_str].pack('H*').force_encoding('UTF-8')
end

.rstr2utf8(str) ⇒ String

Parameters:

  • str (String)

Returns:

  • (String)


98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/nem/util/convert.rb', line 98

def self.rstr2utf8(str)
  str.unpack('U*').inject('') do |memo, c|
    memo << case
            when c < 128
              c.chr
            when 128 < c && c < 2048
              (c >> 6 | 192).chr + (c & 63 | 128).chr
      else
              (c >> 12 | 224).chr + (c >> 6 & 63 | 128).chr + (c & 63 | 128).chr
    end
  end
end

.ua2hex(ua) ⇒ string

Convert an Array to hex

Parameters:

  • ua (Array)
    • An Uint8Array

Returns:

  • (string)


62
63
64
# File 'lib/nem/util/convert.rb', line 62

def self.ua2hex(ua)
  ua.inject('') { |memo, el| memo << "#{HEX_ENCODE_ARRAY[el >> 4]}#{HEX_ENCODE_ARRAY[el & 0x0f]}" }
end

.ua2words(ua, ua_length) ⇒ WordArray

Convert an Uint8Array to WordArray

Parameters:

  • ua (Array)
    • An Uint8Array

  • uaLength (number)
    • The Uint8Array length

Returns:

  • (WordArray)


70
71
72
73
74
75
76
77
78
# File 'lib/nem/util/convert.rb', line 70

def self.ua2words(ua, ua_length)
  ua[0, ua_length].each_slice(4).map do |chunk|
    x = chunk[0] * 0x1000000 +
      chunk[1] * 0x10000 +
      chunk[2] * 0x100 +
      chunk[3] * 0x1
    x > 0x7fffffff ? x - 0x100000000 : x
  end
end

.utf8_to_hex(str) ⇒ string

Convert UTF-8 to hex

Parameters:

  • str (string)

Returns:

  • (string)


48
49
50
# File 'lib/nem/util/convert.rb', line 48

def self.utf8_to_hex(str)
  rstr2utf8(str).bytes.inject('') { |memo, b| memo << b.to_s(16) }
end

.words2ua(words) ⇒ Array

Convert a wordArray to Uint8Array

Parameters:

  • destUa (Array)
    • A destination Uint8Array

  • cryptowords (Array)
    • A wordArray

Returns:

  • (Array)


84
85
86
87
88
89
90
91
92
93
94
# File 'lib/nem/util/convert.rb', line 84

def self.words2ua(words)
  words.inject([]) do |memo, v|
    temp = []
    v += 0x100000000 if v < 0
    temp[0] = (v >> 24)
    temp[1] = (v >> 16) & 0xff
    temp[2] = (v >> 8) & 0xff
    temp[3] = (v) & 0xff
    memo + temp
  end
end