underlock 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: efeef63eb2420dcfc642b0ee17b5cf03820ad6dd
4
+ data.tar.gz: 8b8817fe84302875634819cf9a5709bbe494e169
5
+ SHA512:
6
+ metadata.gz: 521f822d0cfa8b6a3a5d7152e1edb280cece2af86cefcd7ca696f2085c290d3815da0987a74d8a99c7eead0c2e92e2525d3e9cdc4697a9931db962fa6be4ab68
7
+ data.tar.gz: 0538a17b60e37d57b0f2d87e009d280888159f404a80d2b975e1855e4efca2cd8b14b15df9b7340d213e769611a1a876734dcaf867327060b9f2fa1b5447a74e
data/.gitignore ADDED
@@ -0,0 +1,59 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
11
+ *.rbc
12
+ /.config
13
+ /coverage/
14
+ /InstalledFiles
15
+ /pkg/
16
+ /spec/reports/
17
+ /spec/examples.txt
18
+ /test/tmp/
19
+ /test/version_tmp/
20
+ /tmp/
21
+
22
+ # Used by dotenv library to load environment variables.
23
+ # .env
24
+
25
+ ## Specific to RubyMotion:
26
+ .dat*
27
+ .repl_history
28
+ build/
29
+ *.bridgesupport
30
+ build-iPhoneOS/
31
+ build-iPhoneSimulator/
32
+
33
+ ## Specific to RubyMotion (use of CocoaPods):
34
+ #
35
+ # We recommend against adding the Pods directory to your .gitignore. However
36
+ # you should judge for yourself, the pros and cons are mentioned at:
37
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
38
+ #
39
+ # vendor/Pods/
40
+
41
+ ## Documentation cache and generated files:
42
+ /.yardoc/
43
+ /_yardoc/
44
+ /doc/
45
+ /rdoc/
46
+
47
+ ## Environment normalization:
48
+ /.bundle/
49
+ /vendor/bundle
50
+ /lib/bundler/man/
51
+
52
+ # for a library or gem, you might want to ignore these files since the code is
53
+ # intended to run in multiple environments; otherwise, check them in:
54
+ # Gemfile.lock
55
+ # .ruby-version
56
+ # .ruby-gemset
57
+
58
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
59
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in underlock.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Jasdeep Singh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,135 @@
1
+ # Underlock
2
+
3
+ Underlock makes it dead simple to encrypt and decrypt your data and files. It comes with little to no dependencies and has a very small API surface.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'underlock'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install underlock
20
+
21
+ ## Initialization
22
+
23
+ ```ruby
24
+ Underlock::Base.configure do |config|
25
+ config.public_key = File.read('./key.pub')
26
+ config.private_key = File.read('./key.priv')
27
+ config.cipher = OpenSSL::Cipher.new('aes-256-gcm')
28
+ end
29
+ ```
30
+
31
+ For the `config.cipher` value, all algorithms available in `OpenSSL::Cipher.ciphers` are supported.
32
+
33
+ ## Generating Public/Private keypair
34
+
35
+ ```ruby
36
+ key = OpenSSL::PKey::RSA.new 4096
37
+ puts key.to_pem
38
+ puts key.public_key.to_pem
39
+ ```
40
+
41
+ ## Usage
42
+
43
+ ### Encrypting Strings/Text
44
+
45
+ ```ruby
46
+ irb> Underlock::Base.encrypt("super secret message")
47
+ => #<Underlock::EncryptedEntity:0x007fef2e4b8320>
48
+ ```
49
+
50
+ `Underlock::EncryptedEntity` has the following 3 methods
51
+
52
+ ```ruby
53
+ encrypted_entity.value
54
+ encrypted_entity.key
55
+ encrypted_entity.iv # iv stands for initialization vector
56
+ ```
57
+
58
+ You should persist or store the `key` and `iv` in order to be able to decrypt the encrypted `value`.
59
+
60
+ ### Decrypting Strings/Text
61
+
62
+ - Create an instance of `Underlock::EncryptedEntity`, use the `key` and `iv` collected in the previous steps.
63
+
64
+ ```ruby
65
+ irb> encrypted_entity = Underlock::EncryptedEntity.new(value: value, key: key, iv: iv)
66
+ ```
67
+
68
+ - Decrypt using one of the following methods:
69
+
70
+ ```ruby
71
+ irb> encrypted_entity.decrypt
72
+ ```
73
+
74
+ ```ruby
75
+ irb> Underlock::Base.decrypt(encrypted_entity)
76
+ ```
77
+
78
+ ### Encrypting Files
79
+
80
+ To encrypt files, instead of passing a `String` object, pass a `File` object to `Underlock::Base.encrypt`
81
+
82
+ ```ruby
83
+ irb> file = File.open('/path/to/your/secret/file.txt')
84
+ irb> Underlock::Base.encrypt(file)
85
+ => #<Underlock::EncryptedEntity:0x007fef2e4b8320>
86
+ ```
87
+
88
+ The return value is an instance of `Underlock::EncryptedEntity` and has the following methods:
89
+
90
+ ```ruby
91
+ encrypted_entity.encrypted_file
92
+ encrypted_entity.key
93
+ encrypted_entity.iv # iv stands for initialization vector here
94
+ ```
95
+
96
+ `#encrypted_file` returns a `File` object. This file is saved in the same directory as your original file.
97
+
98
+ ### Decrypting Files
99
+
100
+ - Create an instance of `Underlock::EncryptedEntity`, use the `key` and `iv` collected in the previous steps.
101
+
102
+ ```ruby
103
+ irb> file = File.open('/path/to/your/secret/file.txt.enc')
104
+ irb> encrypted_entity = Underlock::EncryptedEntity.new(encrypted_file: file, key: key, iv: iv)
105
+ ```
106
+
107
+ - Decrypt using one of the following methods:
108
+
109
+ ```ruby
110
+ irb> encrypted_entity.decrypt
111
+ ```
112
+
113
+ ```ruby
114
+ irb> Underlock::Base.decrypt(encrypted_entity)
115
+ ```
116
+
117
+ Following naming scheme is followed when encrypting/decrypting files:
118
+
119
+ | original file name | encrypted file name | decrypted file name |
120
+ |--------------------|---------------------|---------------------|
121
+ | file.pdf | file.pdf.enc | file.decrypted.pdf |
122
+
123
+ ## Development
124
+
125
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
126
+
127
+ ## Contributing
128
+
129
+ Bug reports and pull requests are welcome on GitHub at https://github.com/metaware/underlock.
130
+
131
+
132
+ ## License
133
+
134
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
135
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "underlock"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/underlock.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'underlock/version'
2
+ require 'dry-configurable'
3
+ require 'openssl'
4
+ require 'underlock/encryptor'
5
+ require 'underlock/file_encryptor'
6
+ require 'underlock/encrypted_entity'
7
+ require 'underlock/base'
8
+
9
+ module Underlock
10
+ end
@@ -0,0 +1,28 @@
1
+ module Underlock
2
+ class Base
3
+ extend Dry::Configurable
4
+
5
+ setting :private_key
6
+ setting :public_key
7
+ setting :passphrase
8
+ setting :cipher
9
+
10
+ class << self
11
+
12
+ def encrypt(unencrypted_value)
13
+ if Pathname.new(unencrypted_value).exist?
14
+ unencrypted_value = File.new(unencrypted_value)
15
+ end
16
+ case unencrypted_value
17
+ when File then FileEncryptor.new.encrypt(unencrypted_value)
18
+ when String then Encryptor.new.encrypt(unencrypted_value)
19
+ end
20
+ end
21
+
22
+ def decrypt(encrypted_entity)
23
+ encrypted_entity.decrypt
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ module Underlock
2
+ class EncryptedEntity
3
+
4
+ attr_accessor :value, :encrypted_file, :key, :iv
5
+ def initialize(value: nil, encrypted_file: nil, key:, iv:)
6
+ @encrypted_file = encrypted_file
7
+ @value = value
8
+ @key = key
9
+ @iv = iv
10
+ end
11
+
12
+ def decrypt
13
+ return Encryptor.new.decrypt(self) if value
14
+ return FileEncryptor.new.decrypt(self) if encrypted_file
15
+ end
16
+
17
+ def inspect
18
+ self.to_s
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,46 @@
1
+ module Underlock
2
+ class Encryptor
3
+
4
+ def encrypt(value)
5
+ cipher = Underlock::Base.config.cipher.dup
6
+ cipher.encrypt
7
+ key = cipher.random_key
8
+ iv = cipher.random_iv
9
+
10
+ encrypted_value = base64_encode(cipher.update(value))
11
+ encrypted_key = public_encrypt(key)
12
+ encrypted_iv = public_encrypt(iv)
13
+
14
+ EncryptedEntity.new(value: encrypted_value, key: encrypted_key, iv: encrypted_iv)
15
+ end
16
+
17
+ def decrypt(encrypted_entity)
18
+ decode_cipher = Underlock::Base.config.cipher.dup
19
+ decode_cipher.decrypt
20
+ decode_cipher.key = private_decrypt(encrypted_entity.key)
21
+ decode_cipher.iv = private_decrypt(encrypted_entity.iv)
22
+ decode_cipher.update(base64_decode(encrypted_entity.value)[0])
23
+ end
24
+
25
+ private
26
+
27
+ def public_encrypt(value)
28
+ key = OpenSSL::PKey::RSA.new(Underlock::Base.config.public_key)
29
+ base64_encode(key.public_encrypt(base64_encode(value)))
30
+ end
31
+
32
+ def private_decrypt(value)
33
+ key = OpenSSL::PKey::RSA.new(Underlock::Base.config.private_key)
34
+ base64_decode(key.private_decrypt(base64_decode(value)[0]))[0]
35
+ end
36
+
37
+ def base64_encode(value)
38
+ [value].pack('m')
39
+ end
40
+
41
+ def base64_decode(value)
42
+ value.unpack('m')
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,82 @@
1
+ module Underlock
2
+ class FileEncryptor
3
+
4
+ def encrypt(file)
5
+ file = File.realpath(file)
6
+ @base_dir, @filename = File.split(file)
7
+ cipher = Underlock::Base.config.cipher.dup
8
+ cipher.encrypt
9
+ key = cipher.random_key
10
+ iv = cipher.random_iv
11
+
12
+ File.open(encrypted_filepath, "w") do |encrypted_file|
13
+ File.open(file) do |inf|
14
+ loop do
15
+ r = inf.read(4096)
16
+ break unless r
17
+ encrypted_file << cipher.update(r)
18
+ end
19
+ end
20
+ encrypted_file << cipher.final
21
+ end
22
+
23
+ encrypted_file = File.new(encrypted_filepath)
24
+ encrypted_key = public_encrypt(key)
25
+ encrypted_iv = public_encrypt(iv)
26
+
27
+ EncryptedEntity.new(encrypted_file: encrypted_file, key: encrypted_key, iv: encrypted_iv)
28
+ end
29
+
30
+ def decrypt(encrypted_entity)
31
+ decode_cipher = Underlock::Base.config.cipher.dup
32
+ decode_cipher.decrypt
33
+ decode_cipher.key = private_decrypt(encrypted_entity.key)
34
+ decode_cipher.iv = private_decrypt(encrypted_entity.iv)
35
+
36
+ @base_dir, @filename = File.split(encrypted_entity.encrypted_file)
37
+
38
+ File.open(decrypted_filepath, 'wb') do |decrypted_file|
39
+ File.open(encrypted_entity.encrypted_file) do |inf|
40
+ loop do
41
+ r = inf.read(4096)
42
+ break unless r
43
+ decrypted_file << decode_cipher.update(r)
44
+ end
45
+ end
46
+ end
47
+ File.new(decrypted_filepath)
48
+ end
49
+
50
+ private
51
+
52
+ def encrypted_filepath
53
+ "#{@base_dir}/#{@filename}.enc"
54
+ end
55
+
56
+ def decrypted_filepath
57
+ original_filename = @filename.gsub('.enc', '')
58
+ extension = File.extname(original_filename)
59
+ decrypted_filename = original_filename.gsub(extension, ".decrypted#{extension}")
60
+ "#{@base_dir}/#{decrypted_filename}"
61
+ end
62
+
63
+ def public_encrypt(value)
64
+ key = OpenSSL::PKey::RSA.new(Underlock::Base.config.public_key)
65
+ base64_encode(key.public_encrypt(base64_encode(value)))
66
+ end
67
+
68
+ def private_decrypt(value)
69
+ key = OpenSSL::PKey::RSA.new(Underlock::Base.config.private_key)
70
+ base64_decode(key.private_decrypt(base64_decode(value)[0]))[0]
71
+ end
72
+
73
+ def base64_encode(value)
74
+ [value].pack('m')
75
+ end
76
+
77
+ def base64_decode(value)
78
+ value.unpack('m')
79
+ end
80
+
81
+ end
82
+ end
@@ -0,0 +1,3 @@
1
+ module Underlock
2
+ VERSION = "0.0.3"
3
+ end
data/underlock.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'underlock/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "underlock"
8
+ spec.version = Underlock::VERSION
9
+ spec.authors = ["Jasdeep Singh"]
10
+ spec.email = ["narang.jasdeep@gmail.com"]
11
+
12
+ spec.summary = %q{Underlock makes it dead simple to encrypt and decrypt files using public/private keys.}
13
+ # spec.description = %q{}
14
+ spec.homepage = "https://github.com/metaware/underlock"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "dry-configurable", "~> 0.5.0"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.13"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ spec.add_development_dependency "yomu", "~> 0.2.4"
30
+ spec.add_development_dependency "pry", "~> 0.10.4"
31
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: underlock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Jasdeep Singh
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-configurable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.5.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.5.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: yomu
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.2.4
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.2.4
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.10.4
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.10.4
97
+ description:
98
+ email:
99
+ - narang.jasdeep@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - lib/underlock.rb
114
+ - lib/underlock/base.rb
115
+ - lib/underlock/encrypted_entity.rb
116
+ - lib/underlock/encryptor.rb
117
+ - lib/underlock/file_encryptor.rb
118
+ - lib/underlock/version.rb
119
+ - underlock.gemspec
120
+ homepage: https://github.com/metaware/underlock
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.6.8
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Underlock makes it dead simple to encrypt and decrypt files using public/private
144
+ keys.
145
+ test_files: []