Module: BeakerHcloud::SSHDataPatches::EncodingPatch

Defined in:
lib/beaker-hcloud/ssh_data_patches.rb

Overview

Add encoding methods for OpenSSH’s PEM-like format to store private keys.

Instance Method Summary collapse

Instance Method Details

#encode_openssh_private_key(private_key, comment = '') ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/beaker-hcloud/ssh_data_patches.rb', line 25

def encode_openssh_private_key(private_key, comment = '')
  public_key = private_key.public_key
  private_key_data = [
    (SecureRandom.random_bytes(4) * 2),
    public_key.rfc4253,
    encode_string(private_key.ed25519_key.seed + public_key.ed25519_key.to_str),
    encode_string(comment),
  ].join
  unpadded = private_key_data.bytesize % 8
  private_key_data << Array(1..(8 - unpadded)).pack('c*') unless unpadded.zero?
  [
    ::SSHData::Encoding::OPENSSH_PRIVATE_KEY_MAGIC,
    encode_string('none'),
    encode_string('none'),
    encode_string(''),
    encode_uint32(1),
    encode_string(public_key.rfc4253),
    encode_string(private_key_data),
  ].join
end

#encode_pem(data, type) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/beaker-hcloud/ssh_data_patches.rb', line 13

def encode_pem(data, type)
  encoded_data = Base64.strict_encode64(data)
                       .scan(/.{1,70}/m)
                       .join("\n")
                       .chomp
  "    -----BEGIN \#{type}-----\n    \#{encoded_data}\n    -----END \#{type}-----\n  PEM\nend\n"