class Rnp::Encrypt

Encryption operation

Attributes

ptr[R]

@api private

Public Class Methods

destroy(ptr) click to toggle source

@api private

# File lib/rnp/op/encrypt.rb, line 26
def self.destroy(ptr)
  LibRnp.rnp_op_encrypt_destroy(ptr)
end
new(ptr) click to toggle source

@api private

# File lib/rnp/op/encrypt.rb, line 20
def initialize(ptr)
  raise Rnp::Error, 'NULL pointer' if ptr.null?
  @ptr = FFI::AutoPointer.new(ptr, self.class.method(:destroy))
end

Public Instance Methods

add_password(password, s2k_hash: nil, s2k_iterations: 0, s2k_cipher: nil) click to toggle source

Add a password.

@param password [String] the password @param s2k_hash [String] the hash algorithm to use for the

string-to-key key derivation.

@param s2k_iterations [Integer] the number of iterations for the

string-to-key key derivation. A value of 0 will choose
a default.

@param s2k_cipher [String] the cipher algorithm used to wrap the key. @note This is a separate cipher from the one used to encrypt the main

payload/stream (see {#cipher=}). This cipher may not be used in all
circumstances. For example, when encrypting with *only* a password
(no public keys), this cipher would generally not be used.
When encrypting with a combination of one or more passwords and one
or more public keys, this cipher would generally be used.

@return [self]

# File lib/rnp/op/encrypt.rb, line 79
def add_password(password, s2k_hash: nil, s2k_iterations: 0,
                 s2k_cipher: nil)
  Rnp.call_ffi(:rnp_op_encrypt_add_password, @ptr, password, s2k_hash,
               s2k_iterations, s2k_cipher)
  self
end
add_recipient(recipient) click to toggle source

Add a recipient.

@param recipient [Key] the recipient @return [self]

# File lib/rnp/op/encrypt.rb, line 38
def add_recipient(recipient)
  Rnp.call_ffi(:rnp_op_encrypt_add_recipient, @ptr, recipient.ptr)
  self
end
add_signer(signer, hash: nil, creation_time: nil, expiration_time: nil) click to toggle source

Add a signer.

@param signer [Key] the signer @param hash (see hash=) @param creation_time (see creation_time=) @param expiration_time (see expiration_time=) @return [self]

# File lib/rnp/op/encrypt.rb, line 50
def add_signer(signer, hash: nil, creation_time: nil, expiration_time: nil)
  pptr = FFI::MemoryPointer.new(:pointer)
  Rnp.call_ffi(:rnp_op_encrypt_add_signature, @ptr, signer.ptr, pptr)
  psig = pptr.read_pointer
  Sign.set_signature_options(
    psig,
    hash: hash,
    creation_time: creation_time,
    expiration_time: expiration_time
  )
  self
end
armored=(armored) click to toggle source

Set whether the output will be ASCII-armored.

@param armored [Boolean] true if the output should be

ASCII-armored, false otherwise.
# File lib/rnp/op/encrypt.rb, line 111
def armored=(armored)
  Rnp.call_ffi(:rnp_op_encrypt_set_armor, @ptr, armored)
end
cipher=(cipher) click to toggle source

Set the cipher used to encrypt the input.

@param cipher [String] the cipher algorithm name

# File lib/rnp/op/encrypt.rb, line 134
def cipher=(cipher)
  Rnp.call_ffi(:rnp_op_encrypt_set_cipher, @ptr, cipher)
end
compression=(compression) click to toggle source

Set the compression algorithm and level.

@param [Hash<Symbol>] compression @option compression [String] :algorithm the compression algorithm

(bzip2, etc)

@option compression [Integer] :level the compression level. This should

generally be between 0 (no compression) and 9 (best compression).
# File lib/rnp/op/encrypt.rb, line 122
def compression=(compression)
  if !compression.is_a?(Hash) || Set.new(compression.keys) != Set.new(%i[algorithm level])
    raise ArgumentError,
          'Compression option must be of the form: {algorithm: \'zlib\', level: 5}'
  end
  Rnp.call_ffi(:rnp_op_encrypt_set_compression, @ptr,
               compression[:algorithm], compression[:level])
end
creation_time=(creation_time) click to toggle source

Set the creation time for signatures.

@note This is only valid when there is one or more signer.

@param creation_time [Time, Integer] the creation time to use for all

signatures. As an integer, this is the number of seconds
since the unix epoch.
# File lib/rnp/op/encrypt.rb, line 154
def creation_time=(creation_time)
  creation_time = creation_time.to_i if creation_time.is_a?(::Time)
  Rnp.call_ffi(:rnp_op_encrypt_set_creation_time, @ptr, creation_time)
end
execute() click to toggle source

Execute the operation.

This should only be called once.

@return [void]

# File lib/rnp/op/encrypt.rb, line 176
def execute
  Rnp.call_ffi(:rnp_op_encrypt_execute, @ptr)
end
expiration_time=(expiration_time) click to toggle source

Set the expiration time for signatures.

@note This is only valid when there is one or more signer.

@param expiration_time [Integer] the lifetime of the signatures, as the number

of seconds. The actual expiration date/time is the creation time
plus this value. A value of 0 will create signatures that do not
expire.
# File lib/rnp/op/encrypt.rb, line 167
def expiration_time=(expiration_time)
  Rnp.call_ffi(:rnp_op_encrypt_set_expiration_time, @ptr, expiration_time)
end
hash=(hash) click to toggle source

Set the hash algorithm used for calculating signatures.

@note This is only valid when there is one or more signer.

@param hash [String] the hash algorithm name

# File lib/rnp/op/encrypt.rb, line 143
def hash=(hash)
  Rnp.call_ffi(:rnp_op_encrypt_set_hash, @ptr, hash)
end
inspect() click to toggle source
# File lib/rnp/op/encrypt.rb, line 30
def inspect
  Rnp.inspect_ptr(self)
end
options=(armored: nil, compression: nil, cipher: nil, hash: nil, creation_time: nil, expiration_time: nil) click to toggle source

Set a group of options.

@note Some options are related to signatures and will have no effect if there are no signers.

@param armored (see armored=) @param compression (see compression=) @param cipher (see cipher=) @param hash (see hash=) @param creation_time (see creation_time=) @param expiration_time (see expiration_time=)

# File lib/rnp/op/encrypt.rb, line 97
def options=(armored: nil, compression: nil, cipher: nil, hash: nil,
             creation_time: nil, expiration_time: nil)
  self.armored = armored unless armored.nil?
  self.compression = compression unless compression.nil?
  self.cipher = cipher unless cipher.nil?
  self.hash = hash unless hash.nil?
  self.creation_time = creation_time unless creation_time.nil?
  self.expiration_time = expiration_time unless expiration_time.nil?
end