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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Message.



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

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 (readonly)

Returns the value of attribute private_key.



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

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

Instance Method Details

#==(other) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/nis/struct/message.rb', line 64

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

#bytesizeInteger

Returns:

  • (Integer)


44
45
46
# File 'lib/nis/struct/message.rb', line 44

def bytesize
  payload.bytesize
end

#decrypt!Object



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

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



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

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)


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

def encrypted?
  @type == TYPE_ENCRYPTED
end

#plain?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/nis/struct/message.rb', line 39

def plain?
  @type == TYPE_PLAIN
end

#to_hashHash

Returns:

  • (Hash)


54
55
56
# File 'lib/nis/struct/message.rb', line 54

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

#to_sString

Returns:

  • (String)


59
60
61
# File 'lib/nis/struct/message.rb', line 59

def to_s
  @value.to_s
end

#valid?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/nis/struct/message.rb', line 49

def valid?
  bytesize <= 512
end