Class: Rnp::Encrypt

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

Overview

Encryption operation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ptr) ⇒ Encrypt

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.

Returns a new instance of Encrypt.

Raises:



20
21
22
23
# 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

Instance Attribute Details

#ptrObject (readonly)

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.



17
18
19
# File 'lib/rnp/op/encrypt.rb', line 17

def ptr
  @ptr
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.



26
27
28
# File 'lib/rnp/op/encrypt.rb', line 26

def self.destroy(ptr)
  LibRnp.rnp_op_encrypt_destroy(ptr)
end

Instance Method Details

#add_password(password, s2k_hash: nil, s2k_iterations: 0, s2k_cipher: nil) ⇒ self

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.

Add a password.

Parameters:

  • password (String)

    the password

  • s2k_hash (String) (defaults to: nil)

    the hash algorithm to use for the string-to-key key derivation.

  • s2k_iterations (Integer) (defaults to: 0)

    the number of iterations for the string-to-key key derivation. A value of 0 will choose a default.

  • s2k_cipher (String) (defaults to: nil)

    the cipher algorithm used to wrap the key.

Returns:

  • (self)


79
80
81
82
83
84
# 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) ⇒ self

Add a recipient.

Parameters:

  • recipient (Key)

    the recipient

Returns:

  • (self)


38
39
40
41
# 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) ⇒ self

Add a signer.

Parameters:

  • signer (Key)

    the signer

  • hash (String) (defaults to: nil)

    the hash algorithm name

  • creation_time (Time, Integer) (defaults to: nil)

    the creation time to use for all signatures. As an integer, this is the number of seconds since the unix epoch.

  • expiration_time (Integer) (defaults to: nil)

    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.

Returns:

  • (self)


50
51
52
53
54
55
56
57
58
59
60
61
# 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) ⇒ Object

Set whether the output will be ASCII-armored.

Parameters:

  • armored (Boolean)

    true if the output should be ASCII-armored, false otherwise.



111
112
113
# File 'lib/rnp/op/encrypt.rb', line 111

def armored=(armored)
  Rnp.call_ffi(:rnp_op_encrypt_set_armor, @ptr, armored)
end

#cipher=(cipher) ⇒ Object

Set the cipher used to encrypt the input.

Parameters:

  • cipher (String)

    the cipher algorithm name



134
135
136
# File 'lib/rnp/op/encrypt.rb', line 134

def cipher=(cipher)
  Rnp.call_ffi(:rnp_op_encrypt_set_cipher, @ptr, cipher)
end

#compression=(compression) ⇒ Object

Set the compression algorithm and level.

Parameters:

  • compression (Hash<Symbol>)

Options Hash (compression):

  • :algorithm (String)

    the compression algorithm (bzip2, etc)

  • :level (Integer)

    the compression level. This should generally be between 0 (no compression) and 9 (best compression).



122
123
124
125
126
127
128
129
# 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) ⇒ Object

Note:

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

Set the creation time for signatures.

Parameters:

  • 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.



154
155
156
157
# 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

#executevoid

This method returns an undefined value.

Execute the operation.

This should only be called once.



176
177
178
# File 'lib/rnp/op/encrypt.rb', line 176

def execute
  Rnp.call_ffi(:rnp_op_encrypt_execute, @ptr)
end

#expiration_time=(expiration_time) ⇒ Object

Note:

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

Set the expiration time for signatures.

Parameters:

  • 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.



167
168
169
# 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) ⇒ Object

Note:

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

Set the hash algorithm used for calculating signatures.

Parameters:

  • hash (String)

    the hash algorithm name



143
144
145
# File 'lib/rnp/op/encrypt.rb', line 143

def hash=(hash)
  Rnp.call_ffi(:rnp_op_encrypt_set_hash, @ptr, hash)
end

#inspectObject



30
31
32
# 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) ⇒ Object

Note:

Some options are related to signatures and will have no effect if

Set a group of options.

there are no signers.

Parameters:

  • armored (Boolean) (defaults to: nil)

    true if the output should be ASCII-armored, false otherwise.

  • compression (Hash<Symbol>) (defaults to: nil)
  • cipher (String) (defaults to: nil)

    the cipher algorithm name

  • hash (String) (defaults to: nil)

    the hash algorithm name

  • creation_time (Time, Integer) (defaults to: nil)

    the creation time to use for all signatures. As an integer, this is the number of seconds since the unix epoch.

  • expiration_time (Integer) (defaults to: nil)

    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.



97
98
99
100
101
102
103
104
105
# 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