Class: SSHKeygen::Generator
- Inherits:
-
Object
- Object
- SSHKeygen::Generator
- Defined in:
- lib/ssh_keygen/provider.rb
Overview
Lightweight SSH key generator
Instance Method Summary collapse
-
#initialize(bits, type, passphrase, comment) ⇒ Generator
constructor
A new instance of Generator.
-
#key_fingerprint ⇒ Object
Fingerprint (SHA1 digest, colon delimited).
-
#openssh_rsa_public_key ⇒ Object
Encode an OpenSSH RSA public key.
-
#private_key ⇒ Object
return the public key (encrypted if passphrase is given), in PEM form.
-
#ssh_public_key ⇒ Object
OpenSSH public key.
Constructor Details
#initialize(bits, type, passphrase, comment) ⇒ Generator
Returns a new instance of Generator.
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# 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 |
Instance Method Details
#key_fingerprint ⇒ Object
Fingerprint (SHA1 digest, colon delimited)
69 70 71 |
# 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 ⇒ Object
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)
61 62 63 64 65 66 |
# 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 ⇒ Object
return the public key (encrypted if passphrase is given), in PEM form
36 37 38 39 40 41 42 43 |
# 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 ⇒ Object
OpenSSH public key
46 47 48 49 50 51 52 53 54 |
# 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 |