Class: Lockness::Content

Inherits:
Object
  • Object
show all
Defined in:
lib/lockness/content.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(encrypted_file:) ⇒ Content

Returns a new instance of Content.



8
9
10
11
12
# File 'lib/lockness/content.rb', line 8

def initialize(encrypted_file:)
  @encrypted_file    = encrypted_file
  @message_encryptor = ActiveSupport::MessageEncryptor.new(MasterKey.new.read)
  @encrypted         = encrypted_file.read
end

Instance Attribute Details

#encryptedObject (readonly)

Returns the value of attribute encrypted.



4
5
6
# File 'lib/lockness/content.rb', line 4

def encrypted
  @encrypted
end

#encrypted_fileObject (readonly)

Returns the value of attribute encrypted_file.



4
5
6
# File 'lib/lockness/content.rb', line 4

def encrypted_file
  @encrypted_file
end

#message_encryptorObject (readonly)

Returns the value of attribute message_encryptor.



4
5
6
# File 'lib/lockness/content.rb', line 4

def message_encryptor
  @message_encryptor
end

Instance Method Details

#plainObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/lockness/content.rb', line 14

def plain
  message_encryptor.decrypt_and_verify(encrypted)

rescue ActiveSupport::MessageVerifier::InvalidSignature => exception
  puts "Unable to decrypt using the master.key."
  puts
  puts "Please double check that the master.key is correct."

  exit 1
end

#update(new_plain_content) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/lockness/content.rb', line 25

def update(new_plain_content)
  ensure_content(new_plain_content)

  encrypted_content = message_encryptor.encrypt_and_sign(new_plain_content)

  encrypted_file.save(encrypted_content)
end