Class: Rnp::Generate

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

Overview

Key generation operation

Instance Method Summary collapse

Instance Method Details

#bits=(len) ⇒ Object

Set the bit length of the key.

Parameters:

  • len (Integer)

    the desired bit length



60
61
62
# File 'lib/rnp/op/generate.rb', line 60

def bits=(len)
  Rnp.call_ffi(:rnp_op_generate_set_bits, @ptr, len)
end

#curve=(curve) ⇒ Object

Note:

This is only valid for ECC keys which permit specifying a curve.

Set the desired curve for this ECC key.

Parameters:

  • curve (String)

    the curve



78
79
80
# File 'lib/rnp/op/generate.rb', line 78

def curve=(curve)
  Rnp.call_ffi(:rnp_op_generate_set_curve, @ptr, curve.to_s)
end

#executeKey

Execute the operation.

This should only be called once.

Returns:

  • (Key)

    the generated key



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

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

#hash=(hash) ⇒ Object

Set the hash algorithm used in the self-signature of the key.

Parameters:

  • hash (String)

    the hash algorithm name



85
86
87
# File 'lib/rnp/op/generate.rb', line 85

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

#inspectObject



29
30
31
# File 'lib/rnp/op/generate.rb', line 29

def inspect
  Rnp.inspect_ptr(self)
end

#keyKey

Retrieve the key.

This should only be called after #execute.

Returns:



190
191
192
193
194
195
# File 'lib/rnp/op/generate.rb', line 190

def key
  pptr = FFI::MemoryPointer.new(:pointer)
  Rnp.call_ffi(:rnp_op_generate_get_key, @ptr, pptr)
  pkey = pptr.read_pointer
  Key.new(pkey) unless pkey.null?
end

#lifetime=(secs) ⇒ Object

Set the number of seconds for this key to remain valid.

This determines the expiration time (creation time + lifetime).

Parameters:

  • secs (Integer)

    the number of seconds until this key will be considered expired. A value of 0 indicates no expiration. Note that there is an upper limit of 2^32-1.



133
134
135
# File 'lib/rnp/op/generate.rb', line 133

def lifetime=(secs)
  Rnp.call_ffi(:rnp_op_generate_set_expiration, @ptr, secs)
end

#options=(opts) ⇒ Object

Set a group of options.

Parameters:

  • opts (Hash)

    set several options in one place

Options Hash (opts):

  • :bits (Integer) — default: see #bits=
  • :qbits (Integer) — default: see #qbits=
  • :curve (Integer) — default: see #curve=
  • :hash (String) — default: see #hash=
  • :s2k_hash (String) — default: see #s2k_hash=
  • :s2k_iterations (Integer) — default: see #s2k_iterations=
  • :s2k_cipher (String) — default: see #s2k_cipher=
  • :password (String) — default: see #password=
  • :protection_mode (String) — default: see #protection_mode=
  • :lifetime (Integer) — default: see #lifetime=
  • :userid (String) — default: see #userid=
  • :usage (String) — default: see #usage=


48
49
50
51
52
53
54
55
# File 'lib/rnp/op/generate.rb', line 48

def options=(opts)
  %i{bits qbits curve hash s2k_hash s2k_iterations
     s2k_cipher password protection_mode lifetime
     userid usage preferences}.each do |prop|
    value = opts[prop]
    send("#{prop}=", value) unless value.nil?
  end
end

#password=(password) ⇒ Object

Set the password used to protect the key.

Parameters:

  • password (String)

    the password



113
114
115
# File 'lib/rnp/op/generate.rb', line 113

def password=(password)
  Rnp.call_ffi(:rnp_op_generate_set_protection_password, @ptr, password)
end

#preferences=(prefs) ⇒ Object

Parameters:

  • prefs (Hash)

    set several preferences in one place

Options Hash (prefs):

  • :hashes (Array<String>, Array<Symbol>)
  • :compression (Array<String>, Array<Symbol>)
  • :ciphers (Array<String>, Array<Symbol>)
  • :key_server (String)


163
164
165
166
167
168
169
170
171
172
173
# File 'lib/rnp/op/generate.rb', line 163

def preferences=(prefs)
  %i{hashes compression ciphers}.each do |param|
    Rnp.call_ffi(pref_ffi_call(param, clear: true), @ptr)
    prefs[param].each do |pref|
      Rnp.call_ffi(pref_ffi_call(param, add: true),
                   @ptr, pref.to_s)
    end
  end
  Rnp.call_ffi(:rnp_op_generate_set_pref_keyserver, @ptr,
               prefs[:key_server])
end

#protection_mode=(mode) ⇒ Object

Note:

This is only valid for keys saved in the G10 format.

Set the protection mode for this key.

Parameters:

  • mode (String)

    the protection mode (OCB, CBC, etc)



122
123
124
# File 'lib/rnp/op/generate.rb', line 122

def protection_mode=(mode)
  Rnp.call_ffi(:rnp_op_generate_set_protection_mode, @ptr, mode.to_s)
end

#qbits=(len) ⇒ Object

Note:

This is only valid for DSA keys.

Set the bit length of the q parameter for a DSA key.

Parameters:

  • len (Integer)

    the desired bit length



69
70
71
# File 'lib/rnp/op/generate.rb', line 69

def qbits=(len)
  Rnp.call_ffi(:rnp_op_generate_set_dsa_qbits, @ptr, len)
end

#s2k_cipher=(cipher) ⇒ Object

Set the cipher used to protect the key.

Parameters:

  • cipher (String)

    the cipher algorithm name



106
107
108
# File 'lib/rnp/op/generate.rb', line 106

def s2k_cipher=(cipher)
  Rnp.call_ffi(:rnp_op_generate_set_protection_cipher, @ptr, cipher.to_s)
end

#s2k_hash=(hash) ⇒ Object

Set the hash algorithm used to protect the key.

Parameters:

  • hash (String)

    the hash algorithm name



92
93
94
# File 'lib/rnp/op/generate.rb', line 92

def s2k_hash=(hash)
  Rnp.call_ffi(:rnp_op_generate_set_protection_hash, @ptr, hash.to_s)
end

#s2k_iterations=(iter) ⇒ Object

Set the s2k iteration count used to protect the key.

Parameters:

  • iter (Integer)

    the hash algorithm name



99
100
101
# File 'lib/rnp/op/generate.rb', line 99

def s2k_iterations=(iter)
  Rnp.call_ffi(:rnp_op_generate_set_protection_iterations, @ptr, iter)
end

#usage=(usage) ⇒ Object

Set the usage for this key.

Parameters:

  • usage (Array<Symbol>, Array<String>, Symbol, String)

    the usage (:sign, etc)



148
149
150
151
152
153
154
# File 'lib/rnp/op/generate.rb', line 148

def usage=(usage)
  usage = [usage] unless usage.respond_to?(:each)
  Rnp.call_ffi(:rnp_op_generate_clear_usage, @ptr)
  usage.each do |usg|
    Rnp.call_ffi(:rnp_op_generate_add_usage, @ptr, usg.to_s)
  end
end

#userid=(userid) ⇒ Object

Set the userid for this key.

Parameters:

  • userid (String)

    the userid



140
141
142
# File 'lib/rnp/op/generate.rb', line 140

def userid=(userid)
  Rnp.call_ffi(:rnp_op_generate_set_userid, @ptr, userid)
end