class Rye::Key

  1. lib/rye/key.rb
Parent: Rye

Classes and Modules

  1. Rye::Key::BadFile
  2. Rye::Key::BadPerm

Attributes

authtype [R]

Authentication type: RSA or DSA

keytype [R]

Key type: public or private

name [R]

A nickname for this key. If a path was specified this defaults to the basename.

Public Class methods

from_file (path)
[show source]
# 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
generate_pkey (authtype="RSA", bits=1024)
[show source]
# 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
new (data, name=nil)
[show source]
# File lib/rye/key.rb, line 21
def initialize(data, name=nil)
  @data = data
  @name = name || 'default'
  parse_data
end
public_key_to_ssh2 (pubkey)
  • pubkey an instance of OpenSSL::PKey::RSA or OpenSSL::PKey::DSA

Returns a public key in SSH format (suitable for ~/.ssh/authorized_keys)

[show source]
# 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
sign (secret, string, digesttype="sha1")
[show source]
# 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
sign_aws (secret, string)
[show source]
# File lib/rye/key.rb, line 54
def self.sign_aws(secret, string)
  ::Base64.encode64(self.sign(secret, string, "sha1")).strip
end
supported_authentication? (val)
[show source]
# File lib/rye/key.rb, line 110
def self.supported_authentication?(val)
  ["RSA", "DSA"].member?(val || '')
end
supported_keytype? (val)
[show source]
# File lib/rye/key.rb, line 114
def self.supported_keytype?(val)
  ["PRIVATE", "PUBLIC"].member?(val || '')
end

Public Instance methods

decrypt (text)
[show source]
# File lib/rye/key.rb, line 73
def decrypt(text); @keypair.send("#{keytype.downcase}_decrypt", ::Base64.decode64(text)); end
dsa? ()
[show source]
# File lib/rye/key.rb, line 78
def dsa?; @authtype.upcase == "DSA"; end
dump ()
[show source]
# File lib/rye/key.rb, line 89
def dump
  puts @keypair.public_key.to_text
  puts @keypair.public_key.to_pem
end
encrypt (text)

Encrypt text with this public or private key. The key must

[show source]
# File lib/rye/key.rb, line 72
def encrypt(text); ::Base64.encode64(@keypair.send("#{keytype.downcase}_encrypt", text)); end
encrypted? ()
[show source]
# File lib/rye/key.rb, line 79
def encrypted?; @data && @data.match(/ENCRYPTED/); end
inspect ()

Reveals some metadata about the key. Does not print the key.

<Rye::Key:id_rsa.pub authtype="RSA" keytype="PRIVATE">
[show source]
# File lib/rye/key.rb, line 106
def inspect
  '<%s:%s authtype="%s" keytype="%s">' % [self.class.to_s, name, @authtype, @keytype]
end
private? ()
[show source]
# File lib/rye/key.rb, line 75
def private?; @keytype.upcase == "PRIVATE"; end
private_key ()
[show source]
# File lib/rye/key.rb, line 58
def private_key
  raise OpenSSL::PKey::PKeyError, "No private key" if public? || !@keypair
  @keypair.to_s
end
public? ()
[show source]
# File lib/rye/key.rb, line 76
def public?; @keytype.upcase == "PUBLIC";  end
public_key ()
[show source]
# 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
rsa? ()
[show source]
# File lib/rye/key.rb, line 77
def rsa?; @authtype.upcase == "RSA"; end
sign (string, digesttype="sha1")
[show source]
# File lib/rye/key.rb, line 45
def sign(string, digesttype="sha1")
  Rye::Key.sign(@keypair.to_s, string, digesttype)
end
to_s ()

Reveals the key basename. Does not print the key.

<Rye::Key:id_rsa.pub>
[show source]
# File lib/rye/key.rb, line 98
def to_s
  '<%s:%s>' % [self.class.to_s, name]
end