Object
# File lib/rye/key.rb, line 36 def self.from_file(path) raise BadFile, path unless File.exists?(path || '') pkey = self.new File.read(path), File.basename(path) file_perms = (File.stat(path).mode & 600) raise BadPerm, path if file_perms != 0 && pkey.private? pkey end
# File lib/rye/key.rb, line 27 def self.generate_pkey(authtype="RSA", bits=1024) unless Rye::Key.supported_authentication?(authtype) raise OpenSSL::PKey::PKeyError, "Unknown authentication: #{authttype}" end bits &&= bits.to_i klass = authtype.upcase == "RSA" ? OpenSSL::PKey::RSA : OpenSSL::PKey::DSA pk = klass.new(bits) end
# File lib/rye/key.rb, line 21 def initialize(data, name=nil) @data = data @name = name || 'default' parse_data end
pubkey an instance of OpenSSL::PKey::RSA or OpenSSL::PKey::DSA
Returns a public key in SSH format (suitable for ~/.ssh/authorized_keys)
# File lib/rye/key.rb, line 83 def self.public_key_to_ssh2(pubkey) authtype = pubkey.class.to_s.split('::').last.downcase b64pub = ::Base64.encode64(pubkey.to_blob).strip.gsub(/[\r\n]/, '') "ssh-%s %s" % [authtype, b64pub] # => ssh-rsa AAAAB3NzaC1...= end
# File lib/rye/key.rb, line 49 def self.sign(secret, string, digesttype="sha1") @@digest ||= {} @@digest[digest] ||= OpenSSL::Digest::Digest.new(digesttype) sig = OpenSSL::HMAC.hexdigest(@@digest[digest], secret, string).strip end
# File lib/rye/key.rb, line 54 def self.sign_aws(secret, string) ::Base64.encode64(self.sign(secret, string, "sha1")).strip end
# File lib/rye/key.rb, line 73 def decrypt(text); @keypair.send("#{keytype.downcase}_decrypt", ::Base64.decode64(text)); end
# File lib/rye/key.rb, line 78 def dsa?; @authtype.upcase == "DSA"; end
# File lib/rye/key.rb, line 89 def dump puts @keypair.public_key.to_text puts @keypair.public_key.to_pem end
Encrypt text with this public or private key. The key must
# File lib/rye/key.rb, line 72 def encrypt(text); ::Base64.encode64(@keypair.send("#{keytype.downcase}_encrypt", text)); end
# File lib/rye/key.rb, line 79 def encrypted?; @data && @data.match(/ENCRYPTED/); end
Reveals some metadata about the key. Does not print the key.
<Rye::Key:id_rsa.pub authtype="RSA" keytype="PRIVATE">
# File lib/rye/key.rb, line 106 def inspect '<%s:%s authtype="%s" keytype="%s">' % [self.class.to_s, name, @authtype, @keytype] end
# File lib/rye/key.rb, line 75 def private?; @keytype.upcase == "PRIVATE"; end
# File lib/rye/key.rb, line 58 def private_key raise OpenSSL::PKey::PKeyError, "No private key" if public? || !@keypair @keypair.to_s end
# File lib/rye/key.rb, line 76 def public?; @keytype.upcase == "PUBLIC"; end
# File lib/rye/key.rb, line 63 def public_key raise OpenSSL::PKey::PKeyError, "No public key" if !@keypair pubkey = public? ? @keypair : @keypair.public_key # Add the to_ssh2 method to the instance of OpenSSL::PKey::*SA only def pubkey.to_ssh2; Rye::Key.public_key_to_ssh2(self); end pubkey end
# File lib/rye/key.rb, line 77 def rsa?; @authtype.upcase == "RSA"; end
Generated with the Darkfish Rdoc Generator 2.