class SSHKeygen::Generator

Lightweight SSH key generator

Public Class Methods

new(bits, type, passphrase, comment) click to toggle source
# File lib/ssh_keygen/provider.rb, line 21
def initialize(bits, type, passphrase, comment)
  # set instance attributes
  @passphrase = passphrase
  @comment = comment
  @type = type

  case @type
  when 'rsa'
    @key = ::OpenSSL::PKey::RSA.new(bits)
  else
    fail "Invalid key type #{new_resource.type}"
  end
end

Public Instance Methods

key_fingerprint() click to toggle source

Fingerprint (SHA1 digest, colon delimited)

# File lib/ssh_keygen/provider.rb, line 69
def key_fingerprint
  OpenSSL::Digest::SHA1.hexdigest(@key.public_key.to_der).scan(/../).join(':')
end
openssh_rsa_public_key() click to toggle source

Encode an OpenSSH RSA public key. Key format is PEM-encoded - size (big-endian), then data:

* Type (ie: len: 7 (size of string), data: ssh-rsa)
* Exponent (len/data)
* Modulus (len+1/NUL+data)
# File lib/ssh_keygen/provider.rb, line 61
def openssh_rsa_public_key
  enc_type = "#{[7].pack('N')}ssh-rsa"
  enc_exponent = "#{[@key.public_key.e.num_bytes].pack('N')}#{@key.public_key.e.to_s(2)}"
  enc_modulus = "#{[@key.public_key.n.num_bytes + 1].pack('N')}\0#{@key.public_key.n.to_s(2)}"
  Base64.strict_encode64("#{enc_type}#{enc_exponent}#{enc_modulus}")
end
private_key() click to toggle source

return the public key (encrypted if passphrase is given), in PEM form

# File lib/ssh_keygen/provider.rb, line 36
def private_key
  if @passphrase.to_s.empty?
    @key.to_pem
  else
    cipher = ::OpenSSL::Cipher.new('AES-128-CBC')
    @key.export(cipher, @passphrase)
  end
end
ssh_public_key() click to toggle source

OpenSSH public key

# File lib/ssh_keygen/provider.rb, line 46
def ssh_public_key
  case @type
  when 'rsa'
    enc_pubkey = openssh_rsa_public_key
  else
    fail "Invalid key type #{new_resource.type} found in ssh_public_key method - serious error!"
  end
  "ssh-#{@type} #{enc_pubkey} #{@comment}\n"
end