Class: Nis::Struct::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/nis/struct/message.rb

Constant Summary collapse

TYPE_PLAIN =
1
TYPE_ENCRYPTED =
2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = '', type: :plain, private_key: nil, public_key: nil) ⇒ Message

Returns a new instance of Message.



19
20
21
22
23
24
# File 'lib/nis/struct/message.rb', line 19

def initialize(value = '', type: :plain, private_key: nil, public_key: nil)
  @value = value
  @type = (type == :encrypted) ? TYPE_ENCRYPTED : TYPE_PLAIN
  @private_key = private_key
  @public_key = public_key
end

Instance Attribute Details

#payloadString

Returns the current value of payload.

Returns:

  • (String)

    the current value of payload



6
7
8
# File 'lib/nis/struct/message.rb', line 6

def payload
  @payload
end

#private_keyObject

Returns the value of attribute private_key.



8
9
10
# File 'lib/nis/struct/message.rb', line 8

def private_key
  @private_key
end

#public_keyString

Returns the current value of public_key.

Returns:

  • (String)

    the current value of public_key



6
7
8
# File 'lib/nis/struct/message.rb', line 6

def public_key
  @public_key
end

#typeInteger

Returns the current value of type.

Returns:

  • (Integer)

    the current value of type



6
7
8
# File 'lib/nis/struct/message.rb', line 6

def type
  @type
end

#valueString

Returns the current value of value.

Returns:

  • (String)

    the current value of value



6
7
8
# File 'lib/nis/struct/message.rb', line 6

def value
  @value
end

Class Method Details

.build(attrs) ⇒ Object



13
14
15
16
17
# File 'lib/nis/struct/message.rb', line 13

def self.build(attrs)
  if attrs[:type] == TYPE_ENCRYPTED
    new(attrs[:payload], type: :encrypted)
  end
end

Instance Method Details

#==(other) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/nis/struct/message.rb', line 71

def ==(other)
  @value == other.value
end

#bytesizeInteger

Returns:

  • (Integer)


51
52
53
# File 'lib/nis/struct/message.rb', line 51

def bytesize
  payload.bytesize
end

#decrypt!Object



33
34
35
36
37
38
# File 'lib/nis/struct/message.rb', line 33

def decrypt!
  bin_sk = fix_private_key(@private_key).scan(/../).map(&:hex).reverse.pack('C*')
  bin_pk = (public_key || @public_key).scan(/../).map(&:hex).pack('C*')
  @value = Nis::Util::Ed25519.decrypt(bin_sk, bin_pk, payload)
  @type = TYPE_PLAIN
end

#encrypt!Object



26
27
28
29
30
31
# File 'lib/nis/struct/message.rb', line 26

def encrypt!
  bin_sk = fix_private_key(@private_key).scan(/../).map(&:hex).reverse.pack('C*')
  bin_pk = (public_key || @public_key).scan(/../).map(&:hex).pack('C*')
  @value = Nis::Util::Ed25519.encrypt(bin_sk, bin_pk, value)
  @type = TYPE_ENCRYPTED
end

#encrypted?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/nis/struct/message.rb', line 41

def encrypted?
  @type == TYPE_ENCRYPTED
end

#plain?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/nis/struct/message.rb', line 46

def plain?
  @type == TYPE_PLAIN
end

#to_hashHash

Returns:

  • (Hash)


61
62
63
# File 'lib/nis/struct/message.rb', line 61

def to_hash
  { payload: payload, type: @type }
end

#to_sString

Returns:

  • (String)


66
67
68
# File 'lib/nis/struct/message.rb', line 66

def to_s
  @value.to_s
end

#valid?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/nis/struct/message.rb', line 56

def valid?
  bytesize <= 1024
end