Class: I2P::Certificate

Inherits:
Structure show all
Defined in:
lib/i2p/data/certificate.rb,
lib/i2p/sdk.rb

Overview

**I2P certificate data structure.**

Defines a certificate that can be attached to various I2P structures, such as ‘RouterIdentity` and `Destination`, allowing routers and clients to help manage denial of service attacks and the network utilization.

Constant Summary collapse

TYPE_NULL =

Null certificate

Since:

  • 0.1.3

0
TYPE_HASHCASH =

Hashcash certificate

Since:

  • 0.1.3

1
TYPE_HIDDEN =

Hidden certificate

Since:

  • 0.1.3

2
TYPE_SIGNED =

Signed certificate

Since:

  • 0.1.3

3
TYPE_MULTIPLE =

Since:

  • 0.1.3

4
LENGTH_SIGNED_WITH_HASH =

TODO

Since:

  • 0.1.3

nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Structure

#==, #eql?, parse, #to_base64

Constructor Details

#initialize(type = TYPE_NULL, payload = String.new) ⇒ Certificate

Initializes a new certificate instance.

Parameters:

  • type (Integer, #to_i) (defaults to: TYPE_NULL)
  • payload (String, #to_s) (defaults to: String.new)

Since:

  • 0.1.3



45
46
47
# File 'lib/i2p/data/certificate.rb', line 45

def initialize(type = TYPE_NULL, payload = String.new)
  @type, @payload = type.to_i, payload
end

Instance Attribute Details

#payloadString

Returns:

  • (String)

Since:

  • 0.1.3



38
39
40
# File 'lib/i2p/data/certificate.rb', line 38

def payload
  @payload
end

#typeInteger

Returns:

  • (Integer)

Since:

  • 0.1.3



34
35
36
# File 'lib/i2p/data/certificate.rb', line 34

def type
  @type
end

Class Method Details

.read(input) ⇒ Certificate

Reads a certificate from the given ‘input` stream.

Parameters:

  • input (IO, StringIO)

Returns:

Since:

  • 0.1.3



25
26
27
28
29
30
# File 'lib/i2p/data/certificate.rb', line 25

def self.read(input)
  type    = input.read(1).unpack('c').first
  length  = input.read(2).unpack('n').first
  payload = length.zero? ? String.new : input.read(length)
  self.new(type, payload)
end

Instance Method Details

#sizeInteger Also known as: bytesize

Returns the byte size of this certificate.

Returns:

  • (Integer)

Since:

  • 0.1.3



53
54
55
# File 'lib/i2p/data/certificate.rb', line 53

def size
  1 + 2 + (payload ? payload.size : 0)
end

#to_javaObject

Returns an instance of the Java class ‘net.i2p.data.Certificate`.

**This method only works with JRuby, not with MRI or YARV.**



30
31
32
# File 'lib/i2p/sdk.rb', line 30

def to_java
  I2P::SDK::Certificate.new(type.to_i, payload.to_s.to_java_bytes)
end

#to_sString

Returns the binary string representation of this certificate.

Returns:

  • (String)

Since:

  • 0.1.3



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/i2p/data/certificate.rb', line 62

def to_s
  StringIO.open do |buffer|
    buffer.write([type].pack('c'))
    if payload && !payload.empty?
      buffer.write([payload.size].pack('n'))
      buffer.write(payload.to_s)
    else
      buffer.write([0].pack('n'))
    end
    buffer.string
  end
end