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.
- checksums.yaml +4 -4
- data/lib/password_protected_file.rb +28 -23
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a3615fea187f41000e5b509bb36ec14dff958482
|
4
|
+
data.tar.gz: 96e84f0a578e8a370e998d376c587fb983da2403
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
13
|
-
assert_file_exists
|
14
|
-
assert_valid_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
|
20
|
-
assert_file_does_not_exist
|
21
|
-
assert_valid_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
|
107
|
-
|
108
|
-
|
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
|
-
|
113
|
-
|
114
|
-
|
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
|
-
|
117
|
-
|
118
|
-
|
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
|
-
|
121
|
-
|
122
|
-
|
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
|
-
|
125
|
-
|
126
|
-
|
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.
|
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:
|
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
|