Class: Sodium::SecretBuffer

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/sodium/secret_buffer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ SecretBuffer

Returns a new instance of SecretBuffer.



13
14
15
16
17
# File 'lib/sodium/secret_buffer.rb', line 13

def initialize(size)
  @size = Integer(size)
  @to_ptr = Sodium.malloc(@size)
  ObjectSpace.define_finalizer(@to_ptr, self.class.free(@to_ptr.address))
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



10
11
12
# File 'lib/sodium/secret_buffer.rb', line 10

def size
  @size
end

#to_ptrObject (readonly)

Returns the value of attribute to_ptr.



10
11
12
# File 'lib/sodium/secret_buffer.rb', line 10

def to_ptr
  @to_ptr
end

Instance Method Details

#freeObject



19
20
21
22
23
24
25
26
# File 'lib/sodium/secret_buffer.rb', line 19

def free
  Sodium::Mprotect.readonly(@to_ptr)
  Sodium.free(@to_ptr)
  ObjectSpace.undefine_finalizer @to_ptr
  remove_instance_variable(:@size)
  remove_instance_variable(:@to_ptr)
  true
end

#noaccessObject



28
29
30
# File 'lib/sodium/secret_buffer.rb', line 28

def noaccess
  Sodium::Mprotect.noaccess(@to_ptr)
end

#readonlyObject



32
33
34
# File 'lib/sodium/secret_buffer.rb', line 32

def readonly
  Sodium::Mprotect.readonly(@to_ptr)
end

#readwriteObject



36
37
38
# File 'lib/sodium/secret_buffer.rb', line 36

def readwrite
  Sodium::Mprotect.readwrite(@to_ptr)
end