Parent

Included Modules

Class Index [+]

Quicksearch

Net::SSH::KeyFactory

A factory class for returning new Key classes. It is used for obtaining OpenSSL key instances via their SSH names, and for loading both public and private keys. It used used primarily by Net::SSH itself, internally, and will rarely (if ever) be directly used by consumers of the library.

  klass = Net::SSH::KeyFactory.get("rsa")
  assert klass.is_a?(OpenSSL::PKey::RSA)

  key = Net::SSH::KeyFactory.load_public_key("~/.ssh/id_dsa.pub")

Constants

MAP

Specifies the mapping of SSH names to OpenSSL key classes.

Public Class Methods

get(name) click to toggle source

Fetch an OpenSSL key instance by its SSH name. It will be a new, empty key of the given type.

    # File lib/net/ssh/key_factory.rb, line 31
31:       def get(name)
32:         MAP.fetch(name).new
33:       end
load_data_private_key(data, passphrase=nil, ask_passphrase=true, filename="") click to toggle source

Loads a private key. It will correctly determine whether the file describes an RSA or DSA key, and will load it appropriately. The new key is returned. If the key itself is encrypted (requiring a passphrase to use), the user will be prompted to enter their password unless passphrase works.

    # File lib/net/ssh/key_factory.rb, line 50
50:       def load_data_private_key(data, passphrase=nil, ask_passphrase=true, filename="")
51:         if data.match(/-----BEGIN DSA PRIVATE KEY-----/)
52:           key_type = OpenSSL::PKey::DSA
53:         elsif data.match(/-----BEGIN RSA PRIVATE KEY-----/)
54:           key_type = OpenSSL::PKey::RSA
55:         elsif data.match(/-----BEGIN EC PRIVATE KEY-----/) && defined?(OpenSSL::PKey::EC)
56:           key_type = OpenSSL::PKey::EC
57:         elsif data.match(/-----BEGIN (.*) PRIVATE KEY-----/)
58:           raise OpenSSL::PKey::PKeyError, "not a supported key type '#{$1}'"
59:         else
60:           raise OpenSSL::PKey::PKeyError, "not a private key (#{filename})"
61:         end
62: 
63:         encrypted_key = data.match(/ENCRYPTED/)
64:         tries = 0
65: 
66:         begin
67:           return key_type.new(data, passphrase || 'invalid')
68:         rescue OpenSSL::PKey::RSAError, OpenSSL::PKey::DSAError, OpenSSL::PKey::ECError => e
69:           if encrypted_key && ask_passphrase
70:             tries += 1
71:             if tries <= 3
72:               passphrase = prompt("Enter passphrase for #{filename}:", false)
73:               retry
74:             else
75:               raise
76:             end
77:           else
78:             raise
79:           end
80:         end
81:       end
load_data_public_key(data, filename="") click to toggle source

Loads a public key. It will correctly determine whether the file describes an RSA or DSA key, and will load it appropriately. The new public key is returned.

     # File lib/net/ssh/key_factory.rb, line 94
 94:       def load_data_public_key(data, filename="")
 95:         type, blob = data.split(/ /)
 96: 
 97:         raise Net::SSH::Exception, "public key at #{filename} is not valid" if blob.nil?
 98: 
 99:         blob = blob.unpack("m*").first
100:         reader = Net::SSH::Buffer.new(blob)
101:         reader.read_key or raise OpenSSL::PKey::PKeyError, "not a public key #{filename.inspect}"
102:       end
load_private_key(filename, passphrase=nil, ask_passphrase=true) click to toggle source

Loads a private key from a file. It will correctly determine whether the file describes an RSA or DSA key, and will load it appropriately. The new key is returned. If the key itself is encrypted (requiring a passphrase to use), the user will be prompted to enter their password unless passphrase works.

    # File lib/net/ssh/key_factory.rb, line 40
40:       def load_private_key(filename, passphrase=nil, ask_passphrase=true)
41:         data = File.read(File.expand_path(filename))
42:         load_data_private_key(data, passphrase, ask_passphrase, filename)
43:       end
load_public_key(filename) click to toggle source

Loads a public key from a file. It will correctly determine whether the file describes an RSA or DSA key, and will load it appropriately. The new public key is returned.

    # File lib/net/ssh/key_factory.rb, line 86
86:       def load_public_key(filename)
87:         data = File.read(File.expand_path(filename))
88:         load_data_public_key(data, filename)
89:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.