Class: Botan::PK::Encrypt

Inherits:
Object
  • Object
show all
Defined in:
lib/botan/pk/op/encrypt.rb

Overview

Public Key Encrypt Operation

See PublicKey#encrypt for a simpler interface.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, padding: nil) ⇒ Encrypt

Returns a new instance of Encrypt.

Parameters:

  • key (Botan::PK::PublicKey)

    the public key

  • padding (String) (defaults to: nil)

    the padding method name



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/botan/pk/op/encrypt.rb', line 22

def initialize(key:, padding: nil)
  padding ||= Botan::DEFAULT_EME
  unless key.instance_of?(PublicKey)
    raise Botan::Error, 'Encryption requires an instance of PublicKey'
  end
  ptr = FFI::MemoryPointer.new(:pointer)
  flags = 0
  Botan.call_ffi(:botan_pk_op_encrypt_create,
                 ptr, key.ptr, padding, flags)
  ptr = ptr.read_pointer
  if ptr.null?
    raise Botan::Error, 'botan_pk_op_encrypt_create returned NULL'
  end
  @ptr = FFI::AutoPointer.new(ptr, self.class.method(:destroy))
end

Class Method Details

.destroy(ptr) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



39
40
41
# File 'lib/botan/pk/op/encrypt.rb', line 39

def self.destroy(ptr)
  LibBotan.botan_pk_op_encrypt_destroy(ptr)
end

Instance Method Details

#encrypt(msg, rng: Botan::RNG.new) ⇒ String

Encrypts the provided data.

Parameters:

  • msg (String)

    the data

  • rng (Botan::PK::RNG) (defaults to: Botan::RNG.new)

    the RNG to use

Returns:

  • (String)

    the encrypted data



48
49
50
51
52
53
54
# File 'lib/botan/pk/op/encrypt.rb', line 48

def encrypt(msg, rng: Botan::RNG.new)
  msg_buf = FFI::MemoryPointer.from_data(msg)
  Botan.call_ffi_with_buffer(lambda { |b, bl|
    LibBotan.botan_pk_op_encrypt(@ptr, rng.ptr, b, bl,
                                 msg_buf, msg_buf.size)
  })
end

#inspectObject



56
57
58
# File 'lib/botan/pk/op/encrypt.rb', line 56

def inspect
  Botan.inspect_ptr(self)
end