Class: Tanker::Core::EncryptionSession

Inherits:
Object
  • Object
show all
Defined in:
lib/tanker/core/encryption_session.rb

Instance Method Summary collapse

Constructor Details

#initialize(csession) ⇒ EncryptionSession

Returns a new instance of EncryptionSession.



8
9
10
11
12
13
14
# File 'lib/tanker/core/encryption_session.rb', line 8

def initialize(csession)
  @csession = csession
  csession_addr = @csession.address
  ObjectSpace.define_finalizer(@csession) do |_|
    CTanker.tanker_encryption_session_close(FFI::Pointer.new(:void, csession_addr)).get
  end
end

Instance Method Details

#encrypt_common(data) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/tanker/core/encryption_session.rb', line 33

def encrypt_common(data)
  inbuf = FFI::MemoryPointer.from_string(data)

  encrypted_size = CTanker.tanker_encryption_session_encrypted_size(@csession, data.bytesize)
  outbuf = FFI::MemoryPointer.new(:char, encrypted_size)

  CTanker.tanker_encryption_session_encrypt(@csession, outbuf, inbuf, data.bytesize).get

  outbuf.read_string encrypted_size
end

#encrypt_data(data) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/tanker/core/encryption_session.rb', line 16

def encrypt_data(data)
  unless data.is_a?(String)
    raise TypeError, "expected data to be an ASCII-8BIT binary String, but got a #{data.class}"
  end
  unless data.encoding == Encoding::ASCII_8BIT
    raise ArgumentError, "expected data to be an ASCII-8BIT binary String, but it was #{data.encoding} encoded"
  end

  encrypt_common(data)
end

#encrypt_stream(stream) ⇒ Object



44
45
46
# File 'lib/tanker/core/encryption_session.rb', line 44

def encrypt_stream(stream)
  Stream.do_stream_action(stream) { |cb| CTanker.tanker_encryption_session_stream_encrypt(@csession, cb, nil) }
end

#encrypt_utf8(str) ⇒ Object



27
28
29
30
31
# File 'lib/tanker/core/encryption_session.rb', line 27

def encrypt_utf8(str)
  ASSERT_UTF8.call(str)

  encrypt_common str
end

#resource_idObject



48
49
50
# File 'lib/tanker/core/encryption_session.rb', line 48

def resource_id
  CTanker.tanker_encryption_session_get_resource_id(@csession).get_string
end