Class: SecureConf::Encrypter

Inherits:
Object
  • Object
show all
Defined in:
lib/secure_conf/encrypter.rb

Instance Method Summary collapse

Constructor Details

#initialize(pkey = nil, pass = nil) ⇒ Encrypter

Returns a new instance of Encrypter.



6
7
8
9
# File 'lib/secure_conf/encrypter.rb', line 6

def initialize(pkey=nil, pass=nil)
  pkey ||= "~/.ssh/id_rsa"
  self.pkey = [pkey, pass]
end

Instance Method Details

#decrypt(str) ⇒ Object



33
34
35
# File 'lib/secure_conf/encrypter.rb', line 33

def decrypt(str)
  @pkey.private_decrypt(Base64.strict_decode64(str))
end

#encrypt(str) ⇒ Object



29
30
31
# File 'lib/secure_conf/encrypter.rb', line 29

def encrypt(str)
  Base64.strict_encode64(@pkey.public_encrypt(str))
end

#pkey=(pk) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/secure_conf/encrypter.rb', line 11

def pkey=(pk)
  pk, pass = [pk].flatten

  case pk
  when OpenSSL::PKey::RSA
    @pkey = pk
  when String
    pk2 = File.expand_path(pk)
    if File.file?(pk2) && File.readable?(pk2)
      pk = File.read(pk2)
    end

    @pkey = OpenSSL::PKey::RSA.new(pk, pass)
  when Integer
    @pkey = OpenSSL::PKey::RSA.new(pk)
  end
end