Module: Travis::Tools::SSLKey

Extended by:
SSLKey
Included in:
SSLKey
Defined in:
lib/travis/tools/ssl_key.rb

Instance Method Summary collapse

Instance Method Details

#generate_rsa(size = 2048) ⇒ Object



9
10
11
# File 'lib/travis/tools/ssl_key.rb', line 9

def generate_rsa(size = 2048)
  OpenSSL::PKey::RSA.generate(size)
end

#has_passphrase?(key) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
# File 'lib/travis/tools/ssl_key.rb', line 20

def has_passphrase?(key)
  OpenSSL::PKey::RSA.new(key, key)
  false
rescue OpenSSL::PKey::RSAError
  true
end

#public_rsa_key(string) ⇒ Object



13
14
15
16
17
18
# File 'lib/travis/tools/ssl_key.rb', line 13

def public_rsa_key(string)
  @to_rsa ||= OpenSSL::PKey::RSA.new(string)
rescue OpenSSL::PKey::RSAError
  public_key = string.gsub('RSA PUBLIC KEY', 'PUBLIC KEY')
  @to_rsa = OpenSSL::PKey::RSA.new(public_key)
end

#remove_passphrase(key, passphrase) ⇒ Object



27
28
29
30
31
# File 'lib/travis/tools/ssl_key.rb', line 27

def remove_passphrase(key, passphrase)
  OpenSSL::PKey::RSA.new(key, passphrase).to_s
rescue OpenSSL::PKey::RSAError
  false
end

#rsa_ssh(key) ⇒ Object



33
34
35
# File 'lib/travis/tools/ssl_key.rb', line 33

def rsa_ssh(key)
  ['ssh-rsa ', "\0\0\0\assh-rsa#{sized_bytes(key.e)}#{sized_bytes(key.n)}"].pack('a*m').gsub("\n", '')
end

#sized_bytes(value) ⇒ Object



37
38
39
40
# File 'lib/travis/tools/ssl_key.rb', line 37

def sized_bytes(value)
  bytes = to_byte_array(value.to_i)
  [bytes.size, *bytes].pack('NC*')
end

#to_byte_array(num, *significant) ⇒ Object



42
43
44
45
# File 'lib/travis/tools/ssl_key.rb', line 42

def to_byte_array(num, *significant)
  return significant if num.between?(-1, 0) and significant[0][7] == num[7]
  to_byte_array(*num.divmod(256)) + significant
end