Module: Secret::Encryption

Included in:
File
Defined in:
lib/secret/encryption.rb

Overview

Enables basic encryption support with AES.

Note that encryption and decription are rather slow processes and are intended to be used with small strings / file sizes. Please use sparingly!

Instance Method Summary collapse

Instance Method Details

#change_encryption_passphrase!(old_passphrase, new_passphrase) ⇒ Object

Change the passphrase.

Raises:

  • (ArgumentError)


44
45
46
47
48
49
# File 'lib/secret/encryption.rb', line 44

def change_encryption_passphrase!(old_passphrase, new_passphrase)
  raise ArgumentError, "The contents of this file are not encrypted" unless encrypted?
  original = decrypted old_passphrase
  remove_encrypted_indicator
  stash_encrypted original, new_passphrase
end

#decrypt!(passphrase) ⇒ Object

Immediately decrypt the contents of this file.

Raises:

  • (OpenSSL::Cipher::CipherError)

    if the password was incorrect



31
32
33
34
35
36
# File 'lib/secret/encryption.rb', line 31

def decrypt!(passphrase)
  raise ArgumentError, "The contents of this file are not encrypted" unless encrypted?
  str = decrypted(passphrase)
  remove_encrypted_indicator
  stash str
end

#decrypted(passphrase) ⇒ Object

Gets the decrypted version of this.

Raises:

  • (OpenSSL::Cipher::CipherError)

    if the password was incorrect



24
25
26
27
# File 'lib/secret/encryption.rb', line 24

def decrypted(passphrase)
  return contents unless encrypted?
  AES.decrypt contents, passphrase
end

#encrypt!(passphrase) ⇒ Object

Encrypt the contents of this file immediately



39
40
41
# File 'lib/secret/encryption.rb', line 39

def encrypt!(passphrase)
  stash_encrypted passphrase, contents
end

#encrypted(passphrase) ⇒ Object

Gets the contents of the file in an encrypted format. This may quite possibly result in doubly-encrypted text if you’re not careful. This process will take a few moments.



16
17
18
19
# File 'lib/secret/encryption.rb', line 16

def encrypted(passphrase)
  return contents if encrypted?
  encrypt_string passphrase, contents
end

#encrypted?Boolean

Checks to see if the file is encrypted

Returns:

  • (Boolean)


62
63
64
# File 'lib/secret/encryption.rb', line 62

def encrypted?
  ::File.exist?(encrypted_meta_filename)
end

#ensure_unencrypted!Object

Ensure that the contents of this file are unencrypted

Raises:



67
68
69
# File 'lib/secret/encryption.rb', line 67

def ensure_unencrypted!
  raise FileEncryptedError, "Contents of the file are encrypted" if encrypted?
end

#remove_encrypted_indicatorObject



72
73
74
# File 'lib/secret/encryption.rb', line 72

def remove_encrypted_indicator
  ::File.delete encrypted_meta_filename if encrypted?
end

#stash_encrypted(data, passphrase) ⇒ Object

Stash the contents of this file with an encrypted password

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
# File 'lib/secret/encryption.rb', line 52

def stash_encrypted(data, passphrase)
  raise ArgumentError, "The contents of this file is already encrypted" if encrypted?
  ::File.open(encrypted_meta_filename, 'w', container.chmod_mode) {|f| f.write "aes-default" }
  str = encrypt_string passphrase, data
  stash str
  ::File.open(encrypted_meta_filename, 'w', container.chmod_mode) {|f| f.write "aes-default" }
end