Class: Nis::Struct::Message
- Inherits:
-
Object
- Object
- Nis::Struct::Message
- Defined in:
- lib/nis/struct/message.rb
Constant Summary collapse
- TYPE_PLAIN =
1- TYPE_ENCRYPTED =
2
Instance Attribute Summary collapse
-
#payload ⇒ String
The current value of payload.
-
#private_key ⇒ Object
readonly
Returns the value of attribute private_key.
-
#public_key ⇒ String
The current value of public_key.
-
#type ⇒ Integer
The current value of type.
-
#value ⇒ String
The current value of value.
Instance Method Summary collapse
- #==(other) ⇒ Boolean
- #bytesize ⇒ Integer
- #decrypt! ⇒ Object
- #encrypt! ⇒ Object
- #encrypted? ⇒ Boolean
-
#initialize(value = '', type: :plain, private_key: nil, public_key: nil) ⇒ Message
constructor
A new instance of Message.
- #plain? ⇒ Boolean
- #to_hash ⇒ Hash
- #to_s ⇒ String
- #valid? ⇒ Boolean
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
#payload ⇒ String
Returns the current value of payload.
6 7 8 |
# File 'lib/nis/struct/message.rb', line 6 def payload @payload end |
#private_key ⇒ Object (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_key ⇒ String
Returns the current value of public_key.
6 7 8 |
# File 'lib/nis/struct/message.rb', line 6 def public_key @public_key end |
#type ⇒ Integer
Returns the current value of type.
6 7 8 |
# File 'lib/nis/struct/message.rb', line 6 def type @type end |
#value ⇒ String
Returns 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
64 65 66 |
# File 'lib/nis/struct/message.rb', line 64 def ==(other) @value == other.value end |
#bytesize ⇒ 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
34 35 36 |
# File 'lib/nis/struct/message.rb', line 34 def encrypted? @type == TYPE_ENCRYPTED end |
#plain? ⇒ Boolean
39 40 41 |
# File 'lib/nis/struct/message.rb', line 39 def plain? @type == TYPE_PLAIN end |
#to_hash ⇒ Hash
54 55 56 |
# File 'lib/nis/struct/message.rb', line 54 def to_hash { payload: payload, type: @type } end |
#to_s ⇒ String
59 60 61 |
# File 'lib/nis/struct/message.rb', line 59 def to_s @value.to_s end |
#valid? ⇒ Boolean
49 50 51 |
# File 'lib/nis/struct/message.rb', line 49 def valid? bytesize <= 512 end |