Module: Safe::Keys

Defined in:
lib/safe/keys.rb

Constant Summary collapse

SAFE_KEYS_DIR =
File.join(RAILS_ROOT,'config', 'safe_keys')

Instance Method Summary collapse

Instance Method Details

#gen_keypair(path, pass) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/safe/keys.rb', line 26

def gen_keypair(path, pass)
  gen_private_key(path, pass)
  gen_public_key(path, pass)
  if File.exists?("#{path}keypair.pem")
    FileUtils.rm("#{path}keypair.pem")
  end
  cmd = "cat #{path}private.pem  #{path}public.pem >> #{path}keypair.pem"
  Open4::popen4("sh") do |pid, stdin, stdout, stderr|
    stdin.puts cmd
    stdin.close
  end
end

#gen_private_key(path, pass) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/safe/keys.rb', line 10

def gen_private_key(path, pass)
  cmd = "openssl genrsa -des3 -passout pass:#{pass} -out #{path}private.pem 2048"
  Open4::popen4("sh") do |pid, stdin, stdout, stderr|
    stdin.puts cmd
    stdin.close
  end
end

#gen_public_key(path, pass) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/safe/keys.rb', line 18

def gen_public_key(path, pass)
  cmd = "openssl rsa -in #{path}private.pem -out #{path}public.pem -outform PEM -pubout -passin pass:#{pass}"
  Open4::popen4("sh") do |pid, stdin, stdout, stderr|
    stdin.puts cmd
    stdin.close
  end
end

#make_keys!Object

destructible, only call once

Raises:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/safe/keys.rb', line 40

def make_keys!
  root_dir = SAFE_KEYS_DIR
  object_id = self.id
  pass = self.password 
  raise Safe::KeygenError if object_id.nil? || pass.nil?
  dir_class = root_dir + "/#{self.class.to_s.tableize}/"
  dir = dir_class + "#{object_id}/"
  if File.exists?(root_dir) && File.directory?(root_dir)
    Dir.mkdir(dir_class) unless File.exists?(dir_class)
    Dir.mkdir(dir) unless File.exists?(dir)
    gen_keypair(dir, pass)
  else
    Dir.mkdir(root_dir)
    Dir.mkdir(dir_class)
    Dir.mkdir(dir)
    gen_keypair(dir, pass)
  end  
end