Class: PGP::Encryptor

Inherits:
Object
  • Object
show all
Defined in:
lib/pgp/encryptor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_string = nil, gpg_engine = nil) ⇒ Encryptor

Returns a new instance of Encryptor.



5
6
7
8
9
# File 'lib/pgp/encryptor.rb', line 5

def initialize(key_string=nil, gpg_engine=nil)
  @gpg_engine = gpg_engine || GPG::Engine.new
  self.recipients = []
  add_keys(key_string) if key_string
end

Instance Attribute Details

#recipientsObject

Returns the value of attribute recipients.



3
4
5
# File 'lib/pgp/encryptor.rb', line 3

def recipients
  @recipients
end

Instance Method Details

#add_keys(key_string) ⇒ Object



11
12
13
# File 'lib/pgp/encryptor.rb', line 11

def add_keys(key_string)
  self.recipients += @gpg_engine.import_key(key_string)
end

#add_keys_from_file(filename) ⇒ Object



15
16
17
# File 'lib/pgp/encryptor.rb', line 15

def add_keys_from_file(filename)
  add_keys(File.read(filename))
end

#encrypt(cleartext, filename = nil, mtime = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/pgp/encryptor.rb', line 19

def encrypt(cleartext, filename=nil, mtime=nil)
  result = @gpg_engine.encrypt(cleartext, recipients)

  unless filename.nil?
    File.write(filename, result[1])
  end

  result[1]
end

#encrypt_file(file_path) ⇒ Object



29
30
31
# File 'lib/pgp/encryptor.rb', line 29

def encrypt_file(file_path)
  encrypt(File.read(file_path))
end