Module: Nis::Util::Convert

Defined in:
lib/nis/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

Instance Method Summary collapse

Class Method Details

.bin2hex(bin) ⇒ String

Parameters:

  • bin (Array)

Returns:

  • (String)


40
41
42
# File 'lib/nis/util/convert.rb', line 40

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

.hex2a(hex) ⇒ String

Convert hex to string

Parameters:

  • hex (String)

Returns:

  • (String)


22
23
24
# File 'lib/nis/util/convert.rb', line 22

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

.hex2bin(hex) ⇒ String

Parameters:

  • bin (Array)

Returns:

  • (String)


28
29
30
# File 'lib/nis/util/convert.rb', line 28

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

.hex2bin_rev(hex) ⇒ String

Parameters:

  • bin (Array)

Returns:

  • (String)


34
35
36
# File 'lib/nis/util/convert.rb', line 34

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

.hex2ua(hex) ⇒ Array

Convert hex to Uint8Array

Parameters:

  • hex (String)

Returns:

  • (Array)


15
16
17
# File 'lib/nis/util/convert.rb', line 15

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

.hex2ua_rev(hex) ⇒ Array

Reversed convertion of hex to Uint8Array

Parameters:

  • hex (String)

Returns:

  • (Array)


8
9
10
# File 'lib/nis/util/convert.rb', line 8

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

.rstr2utf8(str) ⇒ String

Converts a raw javascript string into a string of single byte characters using utf8 encoding. This makes it easier to perform other encoding operations on the string.

Parameters:

  • str (String)

Returns:

  • (String)


92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/nis/util/convert.rb', line 92

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)


54
55
56
# File 'lib/nis/util/convert.rb', line 54

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)


62
63
64
65
66
67
68
69
70
# File 'lib/nis/util/convert.rb', line 62

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)


47
48
49
# File 'lib/nis/util/convert.rb', line 47

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)


76
77
78
79
80
81
82
83
84
85
86
# File 'lib/nis/util/convert.rb', line 76

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

Instance Method Details

#utf82rstr(input) ⇒ Object

Does the reverse of rstr2utf8.



106
107
108
# File 'lib/nis/util/convert.rb', line 106

def utf82rstr(input)
  raise 'Not implemented.'
end