Class: Votifer::Key

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/votifier/key.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pem_key_content) ⇒ Key

Returns a new instance of Key.



6
7
8
# File 'lib/votifier/key.rb', line 6

def initialize(pem_key_content)
  @key = OpenSSL::PKey::RSA.new(pem_key_content)
end

Class Method Details

.convert_to_pem_format(key_content, type = :private) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/votifier/key.rb', line 34

def self.convert_to_pem_format(key_content, type = :private)
  key_headers = {
    :private => "RSA PRIVATE",
    :public => "PUBLIC"
  }
  header = key_headers[type]
  wrapped_key_content = key_content.scan(/.{1,65}/).join("\n")
  pem_key_content = "-----BEGIN #{header} KEY-----\n" +
    wrapped_key_content + "\n" +
    "-----END #{header} KEY-----"
  pem_key_content
end

.from_key_content(key_content, type = :private) ⇒ Object



24
25
26
27
# File 'lib/votifier/key.rb', line 24

def self.from_key_content(key_content, type = :private)
  pem_key_content = convert_to_pem_format(key_content, type)
  from_pem_key_content(pem_key_content)
end

.from_key_file(key_file, type = :private) ⇒ Object



19
20
21
22
# File 'lib/votifier/key.rb', line 19

def self.from_key_file(key_file, type = :private)
  key_content = File.read(key_file)
  self.from_key_content(key_content, type)
end

.from_pem_key_content(pem_key_content) ⇒ Object



29
30
31
32
# File 'lib/votifier/key.rb', line 29

def self.from_pem_key_content(pem_key_content)
  key = self.new(pem_key_content)
  key
end

.from_pem_key_file(key_file) ⇒ Object



10
11
12
13
# File 'lib/votifier/key.rb', line 10

def self.from_pem_key_file(key_file)
  key_content = File.read(key_file)
  self.from_perm_key_content(key_content)
end

.from_public_key_file(key_file) ⇒ Object



15
16
17
# File 'lib/votifier/key.rb', line 15

def self.from_public_key_file(key_file)
  self.from_key_file(key_file, :public)
end