password_protected_file 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/password_protected_file.rb +28 -23
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 22f79bd8144d4177e3864230ff65e59f256b6bd3
4
- data.tar.gz: 91f718ca2909d063607e7a2ad90685df20cc146d
3
+ metadata.gz: a3615fea187f41000e5b509bb36ec14dff958482
4
+ data.tar.gz: 96e84f0a578e8a370e998d376c587fb983da2403
5
5
  SHA512:
6
- metadata.gz: 666356eac3094b83f5bcb9f20b95458de9f61fa70688d8e641ba731672c09d8e2acf51c2c9e9c7bc76e03925c7512be2b92fc4229f3f5cbd006f1f06098ef22d
7
- data.tar.gz: 58c46cf815894cccec60a1df5598e2b28e18dc431b606de73de5396179c7d5c83e7043ffb72c39cd66da9b56c49c1ee3fba2d3d2a7cd8f945822e53133cf7152
6
+ metadata.gz: b5a0f91ee9df43e871950c8b1f882701227915f6585e9541b2d008765df45a541af076686216d5dd45433b60d72221fd338971c6a4c2748ce73772400ffaa55b
7
+ data.tar.gz: 829cb8864b9aa76efb95789392a78130676ef14d8c34c9b60cfc6a0065b036319d5b63f9c67911bba436e021e813b00d449614217c6a7a1a86426db460636ff1
@@ -1,24 +1,26 @@
1
+ require 'openssl'
2
+ require 'securerandom'
1
3
  class PasswordProtectedFile
2
4
  DEFAULT_ITERATIONS = 20_000
3
5
  ITERATION_BYTES = 8
4
6
  KEY_BYTES = 32
5
7
  SALT_BYTES = 16
6
8
  IV_BYTES = 16
7
- HASH_FUNCTION = OpenSSL::Digest::SHA256
9
+ HASH_FUNCTION = ::OpenSSL::Digest::SHA256
8
10
 
9
11
  private_class_method :new
10
12
 
11
13
  def self.open(file_name, password)
12
- assert_valid_filename(file_name)
13
- assert_file_exists(file_name)
14
- assert_valid_password(password)
14
+ __send__(:assert_valid_filename, file_name)
15
+ __send__(:assert_file_exists, file_name)
16
+ __send__(:assert_valid_password, password)
15
17
  new(file_name, password).tap { |o| o.__send__(:open_existing) }
16
18
  end
17
19
 
18
20
  def self.create(file_name, password, data = '')
19
- assert_valid_filename(file_name)
20
- assert_file_does_not_exist(file_name)
21
- assert_valid_password(password)
21
+ __send__(:assert_valid_filename, file_name)
22
+ __send__(:assert_file_does_not_exist, file_name)
23
+ __send__(:assert_valid_password, password)
22
24
  new(file_name, password).tap { |o| o.__send__(:create_new, data) }
23
25
  end
24
26
 
@@ -103,27 +105,30 @@ class PasswordProtectedFile
103
105
  SecureRandom.random_bytes(SALT_BYTES)
104
106
  end
105
107
 
106
- def self.assert_valid_filename(file_name)
107
- unless file_name.is_a?(String) && !file_name.empty? && Dir.exist?(File.dirname(file_name))
108
- fail InvalidFilenameError.new("Invalid filename given: #{file_name.inspect}")
109
- end
108
+ def assert_valid_data(d)
109
+ fail InvalidDataError.new("Expected String, got #{d.class}") unless d.instance_of?(String)
110
+ fail InvalidStringEncodingError.new("Expected utf-8, got #{d.encoding}") unless d.encoding == Encoding::UTF_8
110
111
  end
111
112
 
112
- def self.assert_file_exists(file_name)
113
- fail FileNotFoundError.new("File #{file_name} not found") unless File.exist?(file_name)
114
- end
113
+ class << self
114
+ private
115
+ def assert_valid_filename(file_name)
116
+ unless file_name.is_a?(String) && !file_name.empty? && Dir.exist?(File.dirname(file_name))
117
+ fail InvalidFilenameError.new("Invalid filename given: #{file_name.inspect}")
118
+ end
119
+ end
115
120
 
116
- def self.assert_file_does_not_exist(file_name)
117
- fail FilenameNotAvailableError.new("File #{file_name} already exists") if File.exist?(file_name)
118
- end
121
+ def assert_file_exists(file_name)
122
+ fail FileNotFoundError.new("File #{file_name} not found") unless File.exist?(file_name)
123
+ end
119
124
 
120
- def self.assert_valid_password(password)
121
- fail InvalidPasswordError.new("Invalid password given: #{password.inspect}") unless password.is_a?(String) && password.length > 0
122
- end
125
+ def assert_file_does_not_exist(file_name)
126
+ fail FilenameNotAvailableError.new("File #{file_name} already exists") if File.exist?(file_name)
127
+ end
123
128
 
124
- def assert_valid_data(d)
125
- fail InvalidDataError.new("Expected String, got #{d.class}") unless d.instance_of?(String)
126
- fail InvalidStringEncodingError.new("Expected utf-8, got #{d.encoding}") unless d.encoding == Encoding::UTF_8
129
+ def assert_valid_password(password)
130
+ fail InvalidPasswordError.new("Invalid password given: #{password.inspect}") unless password.is_a?(String) && password.length > 0
131
+ end
127
132
  end
128
133
 
129
134
  class PasswordProtectedFileError < StandardError; end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: password_protected_file
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Isaac Post
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-21 00:00:00.000000000 Z
11
+ date: 2017-03-15 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: An easy way to create files protected by a password
14
14
  email: post.isaac@gmail.com