Class: HrrRbSsh::DataType::Mpint

Inherits:
Object
  • Object
show all
Defined in:
lib/hrr_rb_ssh/data_type.rb

Class Method Summary collapse

Class Method Details

.decode(io) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/hrr_rb_ssh/data_type.rb', line 113

def self.decode io
  length = io.read(4).unpack("N")[0]
  hex_str = io.read(length).unpack("H*")[0]
  # get temporal integer value
  value = hex_str.hex
  if length == 0
    0
  elsif value[(length * 8) - 1] == 0
    value
  else
    num_bytes = if hex_str.start_with?("ff") then length - 1 else length end
    - (((~ value) & ((1 << (num_bytes * 8)) - 1)) + 1)
  end
end

.encode(arg) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/hrr_rb_ssh/data_type.rb', line 92

def self.encode arg
  unless arg.kind_of? ::Integer
    raise ArgumentError, "must be a kind of Integer, but got #{arg.inspect}"
  end
  bn = ::OpenSSL::BN.new(arg)
  if bn < 0
    # get 2's complement
    tc = bn.to_i & ((1 << (bn.num_bytes * 8)) - 1)
    # get hex representation
    hex_str = "%x" % tc

    if tc[(bn.num_bytes * 8) - 1] == 1
      [bn.num_bytes, hex_str].pack("NH*")
    else
      [bn.num_bytes + 1, "ff" + hex_str].pack("NH*")
    end
  else
    bn.to_s(0)
  end
end