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
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.
Class Method Summary collapse
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.
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
#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
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_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 |
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
71 72 73 |
# File 'lib/nis/struct/message.rb', line 71 def ==(other) @value == other.value end |
#bytesize ⇒ 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
41 42 43 |
# File 'lib/nis/struct/message.rb', line 41 def encrypted? @type == TYPE_ENCRYPTED end |
#plain? ⇒ Boolean
46 47 48 |
# File 'lib/nis/struct/message.rb', line 46 def plain? @type == TYPE_PLAIN end |
#to_hash ⇒ Hash
61 62 63 |
# File 'lib/nis/struct/message.rb', line 61 def to_hash { payload: payload, type: @type } end |
#to_s ⇒ String
66 67 68 |
# File 'lib/nis/struct/message.rb', line 66 def to_s @value.to_s end |
#valid? ⇒ Boolean
56 57 58 |
# File 'lib/nis/struct/message.rb', line 56 def valid? bytesize <= 1024 end |