module Secret::Encryption

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!

Public Instance Methods

change_encryption_passphrase!(old_passphrase, new_passphrase) click to toggle source

Change the passphrase.

# 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) click to toggle source

Immediately decrypt the contents of this file. @raise [OpenSSL::Cipher::CipherError] if the password was incorrect

# 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) click to toggle source

Gets the decrypted version of this. @raise [OpenSSL::Cipher::CipherError] if the password was incorrect

# File lib/secret/encryption.rb, line 24
def decrypted(passphrase)
  return contents unless encrypted?
  AES.decrypt contents, passphrase
end
encrypt!(passphrase) click to toggle source

Encrypt the contents of this file immediately

# File lib/secret/encryption.rb, line 39
def encrypt!(passphrase)
  stash_encrypted passphrase, contents
end
encrypted(passphrase) click to toggle source

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.

# File lib/secret/encryption.rb, line 16
def encrypted(passphrase)
  return contents if encrypted?
  encrypt_string passphrase, contents
end
encrypted?() click to toggle source

Checks to see if the file is encrypted

# File lib/secret/encryption.rb, line 62
def encrypted?
  ::File.exist?(encrypted_meta_filename)
end
ensure_unencrypted!() click to toggle source

Ensure that the contents of this file are unencrypted

# File lib/secret/encryption.rb, line 67
def ensure_unencrypted!
  raise FileEncryptedError, "Contents of the file are encrypted" if encrypted?
end
remove_encrypted_indicator() click to toggle source
# File lib/secret/encryption.rb, line 72
def remove_encrypted_indicator
  ::File.delete encrypted_meta_filename if encrypted?
end
stash_encrypted(data, passphrase) click to toggle source

Stash the contents of this file with an encrypted password

# 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

Protected Instance Methods

encrypt_string(passphrase, string) click to toggle source
# File lib/secret/encryption.rb, line 78
def encrypt_string(passphrase, string)
  iv = AES.iv :base_64
  return AES.encrypt string, passphrase, :iv => iv
end
encrypted_meta_filename() click to toggle source
# File lib/secret/encryption.rb, line 83
def encrypted_meta_filename
  file_path + '.enc'
end