Sha256: 8cf907146f6d80fd03fb87124d1f6bf58a59865397b537d9a53db46790304444

Contents?: true

Size: 889 Bytes

Versions: 2

Compression:

Stored size: 889 Bytes

Contents

require 'digest'
require 'sshkey'

module GitHandler
  class PublicKey
    COMMAND_OPTIONS = [
      'no-port-forwarding',
      'no-X11-forwarding',
      'no-agent-forwarding',
      'no-pty'
    ]

    attr_reader :content

    def initialize(content=nil)
      @content = cleanup_content(content)
      if @content.empty?
        raise ArgumentError, 'Key content is empty!'
      end
      unless valid?
        raise ArgumentError, "Is not a valid public key!"
      end
    end

    def valid?
      SSHKey.valid_ssh_public_key?(@content)
    end

    def md5
      Digest::MD5.hexdigest(@content)
    end

    def sha1
      Digest::SHA1.hexdigest(@content)
    end

    def to_system_key(command)
      "command=\"#{command}\",#{COMMAND_OPTIONS.join(",")} #{@content}"
    end

    private

    def cleanup_content(str)
      str.to_s.strip.gsub(/(\r|\n)*/m, "")
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
git_handler-0.2.1 lib/git_handler/public_key.rb
git_handler-0.2.0 lib/git_handler/public_key.rb