Class: RubyHome::HAP::Session::Encrypter

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_home/hap/encrypter.rb

Constant Summary collapse

MAX_FRAME_LENGTH =
1024.freeze
NONCE_32_BIT_FIX_COMMENT_PART =
[0].pack('L').freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, count: 0) ⇒ Encrypter

Returns a new instance of Encrypter.



8
9
10
11
# File 'lib/ruby_home/hap/encrypter.rb', line 8

def initialize(key, count: 0)
  @key = key
  @count = count
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



35
36
37
# File 'lib/ruby_home/hap/encrypter.rb', line 35

def count
  @count
end

Instance Method Details

#encrypt(data) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby_home/hap/encrypter.rb', line 13

def encrypt(data)
  encrypted_data = []
  read_pointer = 0

  while read_pointer < data.length
    encrypted_frame = ""

    frame = data[read_pointer...read_pointer+MAX_FRAME_LENGTH]
    length_of_encrypted_data = frame.length
    little_endian_length_of_encrypted_data = [length_of_encrypted_data].pack('v')

    encrypted_frame += little_endian_length_of_encrypted_data
    encrypted_frame += chacha20poly1305ietf.encrypt(nonce, frame, little_endian_length_of_encrypted_data)
    encrypted_data << encrypted_frame

    read_pointer += length_of_encrypted_data
    increment_count!
  end

  encrypted_data.join
end