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.



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

def initialize(pkey=nil, pass=nil)
  pkey ||= File.open(File.expand_path("~/.ssh/id_rsa"), "r") {|f| f.read }
  self.pkey = [pkey, pass]
end

Instance Method Details

#decrypt(str) ⇒ Object



41
42
43
# File 'lib/secure_conf/encrypter.rb', line 41

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

#encrypt(str) ⇒ Object



37
38
39
# File 'lib/secure_conf/encrypter.rb', line 37

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

#pkey=(pk) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/secure_conf/encrypter.rb', line 12

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

  case pk
  when OpenSSL::PKey::RSA
    # OpenSSL private key object
    @pkey = pk
  when OpenSSH::PKey
    # OpenSSH private key object
    @pkey = pk.to_openssl
  when String
    pk = pk.strip
    if pk.start_with?("-----BEGIN OPENSSH PRIVATE KEY-----") && pk.end_with?("-----END OPENSSH PRIVATE KEY-----")
      # OpenSSH private pem string
      @pkey = OpenSSH::PKey.new(pk).to_openssl
    else
      # OpenSSL private pem string
      @pkey = OpenSSL::PKey::RSA.new(pk, pass)
    end
  when Integer
    # generate
    @pkey = OpenSSL::PKey::RSA.new(pk)
  end
end