carrierwave_encrypter_decrypter 0.0.4 → 0.0.5
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 +8 -8
- data/Readme.md +34 -3
- data/lib/carrierwave/encrypter_decrypter/decryption.rb +3 -0
- data/lib/carrierwave/encrypter_decrypter/encryption.rb +3 -0
- data/lib/carrierwave/encrypter_decrypter/openssl/pkcs5.rb +89 -0
- data/lib/carrierwave/encrypter_decrypter/version.rb +1 -1
- data/lib/generators/ced/install/install_generator.rb +9 -0
- data/lib/generators/ced/install/templates/carrierwave_encrypter_decrypter.yml +9 -0
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZDlkZGEyY2E0NTRhMjViMTIzMzRmODMzOGY3OWYyNDZjZmYzYmViNA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MDUyYTY1MGY3NzQ5ZjIyNzk3OGVjODViNDg0YjdiZDM4Yjg4NmE3Ng==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ODM0MmE3ODA3MWM2YjQyOGYyM2NjNGNhODc5NzkyMmJhZWI3YWIzMGU4ZDNj
|
10
|
+
NTZhYTE5YWQ1ZWI5MDdlMjQyNjc1NzU4NTE5ZGZjOWMzZDlkMzFlYTliNzZk
|
11
|
+
NzQxZTI3ZTQyZWVlOTJiZmIzYmQ1OTUzMGNiODk1MmMzYjcwNTI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NzAwYjg2ZmRjNzk4Njg0M2EyNzcyMDA2ZjkyYjI1MzQ4OGJkZGVjMGI4YjMy
|
14
|
+
ZGJiZGQzNDhkZjkyMGU4OWQ2NDhkY2YzZDE3NDBhZTczMWQ0NjAwMzExN2M5
|
15
|
+
YzE5OTFmOTJlOTI3NTA5YTgyNWEyMjViNjAyNzU3YzFhNzYxNTU=
|
data/Readme.md
CHANGED
@@ -1,10 +1,15 @@
|
|
1
|
-
**Note:** Gem Work in progress :pray: will be completed by 31st Dec 2013
|
2
|
-
|
3
1
|
# Carrierwave Encrypter Decrypter
|
4
2
|
|
5
|
-
A Rubygem to secure the file uploaded by encrypting the
|
3
|
+
A Rubygem to secure the file uploaded by encrypting the file later on decrypting when needed. Completely secure and depends on Ruby 2.0.0 OpenSSL::Cipher and OpenSSL::PKCS5
|
4
|
+
|
5
|
+
|
6
|
+
OpenSSL::Cipher
|
7
|
+
|
8
|
+
Provides symmetric algorithms for encryption and decryption.
|
6
9
|
|
10
|
+
OpenSSL::PKCS5
|
7
11
|
|
12
|
+
Provides password-based encryption functionality based on PKCS#5.
|
8
13
|
|
9
14
|
## Installation
|
10
15
|
|
@@ -25,6 +30,32 @@ This will create a initializer `carrierwave_encrypter_decrypter`
|
|
25
30
|
|
26
31
|
create config/initializers/carrierwave_encrypter_decrypter.rb
|
27
32
|
|
33
|
+
and a `carrierwave_encrypter_decrypter.yml`
|
34
|
+
|
35
|
+
create config/carrierwave_encrypter_decrypter.yml
|
36
|
+
|
37
|
+
the above will be used when you have the `encryption_type` as `pkcs5`.
|
38
|
+
|
39
|
+
## Choosing encryption type?
|
40
|
+
|
41
|
+
The Gem support 2 ways **[OpenSSL::Cipher](http://ruby-doc.org/stdlib-2.0/libdoc/openssl/rdoc/OpenSSL/Cipher.html)** and **[OpenSSL::PKCS5](http://ruby-doc.org/stdlib-2.0/libdoc/openssl/rdoc/OpenSSL/PKCS5.html)**
|
42
|
+
|
43
|
+
if you want to go with standard encryption in your `config/initializers/carrierwave_encrypter_decrypter.rb` select
|
44
|
+
|
45
|
+
Carrierwave::EncrypterDecrypter.configure do |config|
|
46
|
+
config.encryption_type = :aes
|
47
|
+
config.key_size = 256
|
48
|
+
end
|
49
|
+
|
50
|
+
if you want to go with password based encrption (pkcs5) in your `config/initializers/carrierwave_encrypter_decrypter.rb` select
|
51
|
+
|
52
|
+
Carrierwave::EncrypterDecrypter.configure do |config|
|
53
|
+
config.encryption_type = :pkcs5
|
54
|
+
config.key_size = 256
|
55
|
+
end
|
56
|
+
|
57
|
+
**Note:** Make sure you have the password set in `config/carrierwave_encrypter_decrypter.yml`
|
58
|
+
|
28
59
|
|
29
60
|
Now in your Uploader for eg `app/uploaders/avatar_uploader.rb` add the after store callback
|
30
61
|
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'carrierwave/encrypter_decrypter/openssl/aes'
|
2
|
+
require 'carrierwave/encrypter_decrypter/openssl/pkcs5'
|
2
3
|
|
3
4
|
class Decryption
|
4
5
|
def self.start!(obj,opts)
|
@@ -7,6 +8,8 @@ class Decryption
|
|
7
8
|
case encryption_type
|
8
9
|
when :aes
|
9
10
|
Openssl::Aes.decrypt_for(obj,opts)
|
11
|
+
when :pkcs5
|
12
|
+
Openssl::Pkcs5.decrypt_for(obj,opts)
|
10
13
|
end
|
11
14
|
end
|
12
15
|
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'carrierwave/encrypter_decrypter/openssl/aes'
|
2
|
+
require 'carrierwave/encrypter_decrypter/openssl/pkcs5'
|
2
3
|
|
3
4
|
class Encryption
|
4
5
|
def self.start!(obj)
|
@@ -7,6 +8,8 @@ class Encryption
|
|
7
8
|
case encryption_type
|
8
9
|
when :aes
|
9
10
|
Openssl::Aes.encrypt_for(obj)
|
11
|
+
when :pkcs5
|
12
|
+
Openssl::Pkcs5.encrypt_for(obj)
|
10
13
|
end
|
11
14
|
end
|
12
15
|
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
module Openssl
|
3
|
+
module Pkcs5
|
4
|
+
def self.encrypt_for(obj)
|
5
|
+
begin
|
6
|
+
config = YAML.load_file("#{Rails.root}/config/carrierwave_encrypter_decrypter.yml")[Rails.env]
|
7
|
+
model = obj.model
|
8
|
+
mounted_as = obj.mounted_as
|
9
|
+
|
10
|
+
cipher = OpenSSL::Cipher.new("AES-#{Carrierwave::EncrypterDecrypter.configuration.key_size}-CBC")
|
11
|
+
cipher.encrypt
|
12
|
+
iv = cipher.random_iv
|
13
|
+
model.iv = iv
|
14
|
+
|
15
|
+
pwd = config['pkcs5_password']
|
16
|
+
|
17
|
+
salt = OpenSSL::Random.random_bytes 16
|
18
|
+
|
19
|
+
model.key = salt
|
20
|
+
|
21
|
+
iter = 20000
|
22
|
+
|
23
|
+
key_len = cipher.key_len
|
24
|
+
digest = OpenSSL::Digest::SHA256.new
|
25
|
+
|
26
|
+
key = OpenSSL::PKCS5.pbkdf2_hmac(pwd, salt, iter, key_len, digest)
|
27
|
+
cipher.key = key
|
28
|
+
|
29
|
+
original_file_path = File.expand_path(obj.store_path, obj.root)
|
30
|
+
encrypted_file_path = File.expand_path(obj.store_path, obj.root) + ".enc"
|
31
|
+
model.save!
|
32
|
+
|
33
|
+
|
34
|
+
buf = ""
|
35
|
+
File.open(encrypted_file_path, "wb") do |outf|
|
36
|
+
File.open(model.send(mounted_as).path, "rb") do |inf|
|
37
|
+
while inf.read(4096, buf)
|
38
|
+
outf << cipher.update(buf)
|
39
|
+
end
|
40
|
+
outf << cipher.final
|
41
|
+
end
|
42
|
+
end
|
43
|
+
File.unlink(model.send(mounted_as).path)
|
44
|
+
rescue Exception => e
|
45
|
+
puts "****************************#{e.message}"
|
46
|
+
puts "****************************#{e.backtrace.inspect}"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.decrypt_for(obj,opts)
|
51
|
+
begin
|
52
|
+
config = YAML.load_file("#{Rails.root}/config/carrierwave_encrypter_decrypter.yml")[Rails.env]
|
53
|
+
model = obj
|
54
|
+
mounted_as = opts[:mounted_as]
|
55
|
+
|
56
|
+
cipher = OpenSSL::Cipher.new("AES-#{Carrierwave::EncrypterDecrypter.configuration.key_size}-CBC")
|
57
|
+
cipher.decrypt
|
58
|
+
cipher.iv = model.iv
|
59
|
+
|
60
|
+
pwd = config['pkcs5_password']
|
61
|
+
|
62
|
+
salt = model.key
|
63
|
+
iter = 20000
|
64
|
+
key_len = cipher.key_len
|
65
|
+
digest = OpenSSL::Digest::SHA256.new
|
66
|
+
|
67
|
+
key = OpenSSL::PKCS5.pbkdf2_hmac(pwd, salt, iter, key_len, digest)
|
68
|
+
cipher.key = key
|
69
|
+
|
70
|
+
original_file_path = obj.send(mounted_as).root + obj.send(mounted_as).url
|
71
|
+
encrypted_file_path = obj.send(mounted_as).root + obj.send(mounted_as).url + ".enc"
|
72
|
+
|
73
|
+
buf = ""
|
74
|
+
|
75
|
+
File.open(original_file_path, "wb") do |outf|
|
76
|
+
File.open(encrypted_file_path, "rb") do |inf|
|
77
|
+
while inf.read(4096, buf)
|
78
|
+
outf << cipher.update(buf)
|
79
|
+
end
|
80
|
+
outf << cipher.final
|
81
|
+
end
|
82
|
+
end
|
83
|
+
rescue Exception => e
|
84
|
+
puts "****************************#{e.message}"
|
85
|
+
puts "****************************#{e.backtrace.inspect}"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
module Ced
|
2
2
|
module Generators
|
3
3
|
class InstallGenerator < ::Rails::Generators::Base
|
4
|
+
source_root File.expand_path("../templates", __FILE__)
|
4
5
|
desc "This generator creates an initializer file at config/initializers"
|
5
6
|
def create_initializer_file
|
6
7
|
create_file "config/initializers/carrierwave_encrypter_decrypter.rb" do
|
@@ -10,10 +11,18 @@ module Ced
|
|
10
11
|
#Read more about it here http://ruby-doc.org/stdlib-2.0/libdoc/openssl/rdoc/OpenSSL.html#module-OpenSSL-label-Encryption
|
11
12
|
config.encryption_type = :aes
|
12
13
|
config.key_size = 256
|
14
|
+
|
15
|
+
#This strategy is applicable when you want to have the pkcs5 (Password based encryption)
|
16
|
+
config.encryption_type = :pkcs5
|
17
|
+
config.key_size = 256
|
13
18
|
end
|
14
19
|
"
|
15
20
|
end
|
16
21
|
end
|
22
|
+
|
23
|
+
def copy_pkcs5_yml
|
24
|
+
copy_file "carrierwave_encrypter_decrypter.yml","config/carrierwave_encrypter_decrypter.yml"
|
25
|
+
end
|
17
26
|
end
|
18
27
|
end
|
19
28
|
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
#This file is required by carrierwave_encrypter_decrypter gem.
|
2
|
+
#The password that is set will be used if you have the encryption_type as pkcs5.
|
3
|
+
#This will enable the password based encryption.
|
4
|
+
|
5
|
+
development:
|
6
|
+
pkcs5_password: "Secret"
|
7
|
+
|
8
|
+
production:
|
9
|
+
pkcs5_password: "Secret"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: carrierwave_encrypter_decrypter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ankit gupta
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-12-
|
11
|
+
date: 2013-12-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: logger
|
@@ -52,8 +52,8 @@ dependencies:
|
|
52
52
|
- - ! '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
description: A library
|
56
|
-
|
55
|
+
description: A library for encrypting and decrypting uploaded files. Supports Ruby
|
56
|
+
OpenSSL::Cipher and OpenSSL::PKCS5 Strategies.
|
57
57
|
email: ankit.gupta8898@gmail.com
|
58
58
|
executables: []
|
59
59
|
extensions: []
|
@@ -64,11 +64,13 @@ files:
|
|
64
64
|
- lib/carrierwave/encrypter_decrypter/downloader.rb
|
65
65
|
- lib/carrierwave/encrypter_decrypter/encryption.rb
|
66
66
|
- lib/carrierwave/encrypter_decrypter/openssl/aes.rb
|
67
|
+
- lib/carrierwave/encrypter_decrypter/openssl/pkcs5.rb
|
67
68
|
- lib/carrierwave/encrypter_decrypter/uploader.rb
|
68
69
|
- lib/carrierwave/encrypter_decrypter/version.rb
|
69
70
|
- lib/carrierwave_encrypter_decrypter.rb
|
70
71
|
- lib/generators/ced.rb
|
71
72
|
- lib/generators/ced/install/install_generator.rb
|
73
|
+
- lib/generators/ced/install/templates/carrierwave_encrypter_decrypter.yml
|
72
74
|
- Readme.md
|
73
75
|
homepage: https://github.com/ankit8898/carrierwave_encrypter_decrypter
|
74
76
|
licenses:
|