Class: Rnp::Encrypt

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

Overview

Encryption operation

Instance Method Summary collapse

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)


78
79
80
81
82
83
# File 'lib/rnp/op/encrypt.rb', line 78

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, opts = {}) ⇒ self

Add a signer.

Parameters:

  • signer (Key)

    the signer

  • opts (Hash) (defaults to: {})

    set several options in one place

Options Hash (opts):

  • :hash (String) — default: see #hash=
  • :creation_time (Time) — default: see #creation_time=
  • :expiration_time (Time) — default: see #expiration_time=

Returns:

  • (self)


51
52
53
54
55
56
57
58
59
60
# File 'lib/rnp/op/encrypt.rb', line 51

def add_signer(signer, opts = {})
  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,
    **opts,
  )
  self
end

#aead=(mode) ⇒ Object

Set the AEAD algorithm for encryption.

Parameters:

  • mode (String)

    the AEAD algorithm to use for encryption



139
140
141
# File 'lib/rnp/op/encrypt.rb', line 139

def aead=(mode)
  Rnp.call_ffi(:rnp_op_encrypt_set_aead, @ptr, mode.to_s)
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.



109
110
111
# File 'lib/rnp/op/encrypt.rb', line 109

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



132
133
134
# File 'lib/rnp/op/encrypt.rb', line 132

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



120
121
122
123
124
125
126
127
# File 'lib/rnp/op/encrypt.rb', line 120

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.



159
160
161
162
# File 'lib/rnp/op/encrypt.rb', line 159

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.



181
182
183
# File 'lib/rnp/op/encrypt.rb', line 181

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.



172
173
174
# File 'lib/rnp/op/encrypt.rb', line 172

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



148
149
150
# File 'lib/rnp/op/encrypt.rb', line 148

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=(opts) ⇒ Object

Note:

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

Set a group of options.

there are no signers.

Parameters:

  • opts (Hash)

    set several options in one place

Options Hash (opts):

  • :armored (Boolean) — default: see #armored=
  • :compression (String) — default: see #compression=
  • :cipher (String) — default: see #cipher=
  • :hash (String) — default: see #hash=
  • :creation_time (Time) — default: see #creation_time=
  • :expiration_time (Time) — default: see #expiration_time=


97
98
99
100
101
102
103
# File 'lib/rnp/op/encrypt.rb', line 97

def options=(opts)
  %i{armored compression cipher aead hash creation_time
     expiration_time}.each do |prop|
    value = opts[prop]
    send("#{prop}=", value) unless value.nil?
  end
end