Class: PasswordProtectedFile

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

Defined Under Namespace

Classes: FileNotFoundError, FilenameNotAvailableError, IncorrectPasswordError, InvalidDataError, InvalidFilenameError, InvalidPasswordError, InvalidStringEncodingError, PasswordProtectedFileError

Constant Summary collapse

DEFAULT_ITERATIONS =
20_000
ITERATION_BYTES =
8
KEY_BYTES =
32
SALT_BYTES =
16
IV_BYTES =
16
HASH_FUNCTION =
OpenSSL::Digest::SHA256

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(file_name, password, data = '') ⇒ Object



18
19
20
21
22
23
# File 'lib/password_protected_file.rb', line 18

def self.create(file_name, password, data = '')
  assert_valid_filename(file_name)
  assert_file_does_not_exist(file_name)
  assert_valid_password(password)
  new(file_name, password).tap { |o| o.__send__(:create_new, data) }
end

.open(file_name, password) ⇒ Object



11
12
13
14
15
16
# File 'lib/password_protected_file.rb', line 11

def self.open(file_name, password)
  assert_valid_filename(file_name)
  assert_file_exists(file_name)
  assert_valid_password(password)
  new(file_name, password).tap { |o| o.__send__(:open_existing) }
end

Instance Method Details

#dataObject



25
26
27
# File 'lib/password_protected_file.rb', line 25

def data
  @data.clone
end

#data=(new_data) ⇒ Object



29
30
31
32
33
# File 'lib/password_protected_file.rb', line 29

def data=(new_data)
  assert_valid_data(new_data)
  @data = new_data.clone
  write_file
end