### Home Classes Methods

### In Files

- lib/rye/key.rb

### Parent

Object

### Namespace

- CLASS Rye::Key::BadFile
- CLASS Rye::Key::BadPerm

### Methods

- ::from_file
- ::generate_pkey
- ::new
- ::public_key_to_ssh2
- ::sign
- ::sign_aws
- ::supported_authentication?
- ::supported_keytype?
- #decrypt
- #dsa?
- #dump
- #encrypt
- #encrypted?
- #inspect
- #private?
- #private_key
- #public?
- #public_key
- #rsa?
- #sign
- #to_s

### Files

- LICENSE.txt
- README.rdoc

### Class/Module Index

Quicksearch

- Rye
- Rye::Box
- Rye::Box::InstanceExecHelper
- Rye::Cmd
- Rye::CommandNotFound
- Rye::Err
- Rye::Hop
- Rye::Key
- Rye::Key::BadFile
- Rye::Key::BadPerm
- Rye::NoBoxes
- Rye::NoHost
- Rye::NoPassword
- Rye::NoPty
- Rye::NotConnected
- Rye::Rap
- Rye::RyeError
- Rye::Set
- Rye::Tpl
- Rye::VERSION
- Object

No matching classes.

# Rye::Key

### 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) click to toggle 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) click to toggle 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) click to toggle 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) click to toggle source

- `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

sign(secret, string, digesttype="sha1") click to toggle 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) click to toggle 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) click to toggle source

    # File lib/rye/key.rb, line 110
    def self.supported_authentication?(val)
      ["RSA", "DSA"].member?(val || '')
    end

supported_keytype?(val) click to toggle source

    # File lib/rye/key.rb, line 114
    def self.supported_keytype?(val)
      ["PRIVATE", "PUBLIC"].member?(val || '')
    end

### Public Instance Methods

decrypt(text) click to toggle source

    # File lib/rye/key.rb, line 73
    def decrypt(text); @keypair.send("#{keytype.downcase}_decrypt", ::Base64.decode64(text)); end

dsa?() click to toggle source

    # File lib/rye/key.rb, line 78
    def dsa?; @authtype.upcase == "DSA"; end

dump() click to toggle 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) click to toggle source

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

encrypted?() click to toggle source

    # File lib/rye/key.rb, line 79
    def encrypted?; @data && @data.match(/ENCRYPTED/); end

inspect() click to toggle source

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

private?() click to toggle source

    # File lib/rye/key.rb, line 75
    def private?; @keytype.upcase == "PRIVATE"; end

private_key() click to toggle 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?() click to toggle source

    # File lib/rye/key.rb, line 76
    def public?; @keytype.upcase == "PUBLIC";  end

public_key() click to toggle 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?() click to toggle source

    # File lib/rye/key.rb, line 77
    def rsa?; @authtype.upcase == "RSA"; end

sign(string, digesttype="sha1") click to toggle source

    # File lib/rye/key.rb, line 45
    def sign(string, digesttype="sha1")
      Rye::Key.sign(@keypair.to_s, string, digesttype)
    end

to_s() click to toggle source

Reveals the key basename. Does not print the key.

    <Rye::Key:id_rsa.pub>

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.