cryptenv 0.0.2

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 +15 -0
  2. data/lib/cryptenv.rb +245 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MjQyOTM2NmM5YjFiYzMyYzRhOGNhNzYxNDJmMGUzMzZiOWQ5MmRiZQ==
5
+ data.tar.gz: !binary |-
6
+ ODNhOGIxN2MwNjk3NDRjOWI4NTA4ZGRhNDhhZDQzNWIwYjhhYzAyMA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZTdlNzJlOTlkNjRkN2EzNGU2NTk5ZGNlZWY5Njc1NDczMDA0MTQ4YjQ1MzQz
10
+ NTIyMmMwZWIzOGRkODY2ZTlmNzkzOWUzMDQ1YzEwMWNmMjEwZjI1ZDM0ZDZm
11
+ MTFlNjQwODMxNGJiZGFhOGQ4MWU3MmVkZGExY2NmNTI0MTMxYTE=
12
+ data.tar.gz: !binary |-
13
+ MWFhMTJlODE4YzU2MDY3OTI4YmQxNWZlMjA4YmE0Y2QxZThkNDk0OTI5N2Vj
14
+ NWQ5ZWJjMzEwNmY0YzMwYTVhN2ZhNzA5ZWQyNDQ1ZjAzNzkyZTYzMWE5MzY5
15
+ ZjY0NmM5NGIzNmZlYjBkNjBkNjk4ZTNmYzUxNjdhY2Y3MGQ5YzU=
data/lib/cryptenv.rb ADDED
@@ -0,0 +1,245 @@
1
+ ########################################################################################
2
+ # BUILD & INSTALLATION INSTRUCTION
3
+ #
4
+ # - Build :
5
+ # $ gem build cryptenv.gemspec
6
+ #
7
+ # - Install :
8
+ # $ gem install ./cryptenv-0.0.2.gem
9
+ #
10
+ # - Publish :
11
+ # $ (one time) $ curl -u msylvestre https://rubygems.org/api/v1/api_key.yaml > ~/.gem/credentials; chmod 0600 ~/.gem/credentials
12
+ # $ gem push cryptenv-0.0.2.gem
13
+ #
14
+ # - Unpublish :
15
+ # $ gem yank cryptenv -v 0.0.1
16
+ #
17
+ # - Reference :
18
+ # http://guides.rubygems.org/make-your-own-gem/
19
+ #
20
+ #
21
+ # CHANGE LOG
22
+ #
23
+ # - 0.0.1 Initial Release
24
+ #
25
+ #
26
+ ########################################################################################
27
+
28
+ # TODO: Make those require as dependencies of the gem (see reference above)
29
+ require 'json'
30
+ require 'openssl'
31
+ require 'securerandom'
32
+
33
+ class Cryptenv
34
+
35
+ attr_accessor :env
36
+
37
+ @@OPENSSL_MAGIC = "Salted__"
38
+ @@DEFAULT_CIPHER = "aes-256-cbc"
39
+ @@DEFAULT_MD = OpenSSL::Digest::SHA256
40
+ @@file_loaded = false
41
+
42
+
43
+ def load_crypted_file(path)
44
+ begin
45
+ data = decrypt_in_memory("This is the passphrase", path)
46
+ @env = JSON.parse(data)
47
+ @@file_loaded = true
48
+
49
+ rescue Exception => e
50
+ puts "\n\n-------------------------------------------------------------------------------------"
51
+ puts "FUNCTION: load_crypted_file(path)"
52
+ puts "ERROR MESSAGE: " + e.message
53
+ puts "HINT: You should create a local file ~/ci_env.json or in /<user>/ci_env.json (windows)"
54
+ puts "-------------------------------------------------------------------------------------\n\n"
55
+ exit
56
+ end
57
+ end
58
+
59
+
60
+ def load_file(path)
61
+ begin
62
+ data = IO.read(path)
63
+ @env = JSON.parse(data)
64
+ @@file_loaded = true
65
+
66
+ rescue Exception => e
67
+ puts "\n\n-------------------------------------------------------------------------------------"
68
+ puts "FUNCTION: load_file(path)"
69
+ puts "ERROR MESSAGE: " + e.message
70
+ puts "HINT: You should create a local file ~/ci_env.json or in /<user>/ci_env.json (windows)"
71
+ puts "-------------------------------------------------------------------------------------\n\n"
72
+ exit
73
+ end
74
+ end
75
+
76
+
77
+ def get(*criteria)
78
+
79
+ begin
80
+ raise "env_ci.json not loaded. Please load the file first via 'load_credential_file()'" if !@@file_loaded
81
+
82
+ case criteria.length
83
+
84
+ when 1 then
85
+ return @env[criteria[0]]
86
+
87
+ when 2 then
88
+ return @env[criteria[0]][criteria[1]]
89
+
90
+ when 3 then
91
+ return @env[criteria[0]][criteria[1]][criteria[2]]
92
+
93
+ when 4 then
94
+ return @env[criteria[0]][criteria[1]][criteria[2]][criteria[3]]
95
+
96
+ when 5 then
97
+ return @env[criteria[0]][criteria[1]][criteria[2]][criteria[3]][criteria[4]]
98
+
99
+ when 6 then
100
+ return @env[criteria[0]][criteria[1]][criteria[2]][criteria[3]][criteria[4]][criteria[5]]
101
+
102
+ when 7 then
103
+ return @env[criteria[0]][criteria[1]][criteria[2]][criteria[3]][criteria[4]][criteria[5]][criteria[6]]
104
+
105
+ when 8 then
106
+ return @env[criteria[0]][criteria[1]][criteria[2]][criteria[3]][criteria[4]][criteria[5]][criteria[6]][criteria[7]]
107
+
108
+ else
109
+ raise "Number of parameter is out-of-bound (can't be > 8)"
110
+
111
+ end
112
+
113
+ rescue Exception => e
114
+ puts "\n\n-------------------------------------------------------------------------------------"
115
+ puts "FUNCTION: get_credential(*criteria)"
116
+ puts "PARAM: criteria = " + criteria.to_s
117
+ puts "ERROR MESSAGE: " + e.message
118
+ puts "-------------------------------------------------------------------------------------\n\n"
119
+ exit
120
+ end
121
+ end
122
+
123
+ def encrypt_in_memory(password, source_file, cipher = @@DEFAULT_CIPHER, md = @@DEFAULT_MD.new)
124
+ raise "encrypt_in_memory(...) is not implemented yet"
125
+ end
126
+
127
+
128
+ def encrypt_in_file(password, source_file, cipher = @@DEFAULT_CIPHER, md = @@DEFAULT_MD.new)
129
+ buf = ""
130
+ set_magic_salt = false
131
+
132
+ salt = SecureRandom.random_bytes(8)
133
+ cipher = OpenSSL::Cipher::Cipher.new(cipher)
134
+ cipher.encrypt
135
+ cipher.pkcs5_keyivgen(password, salt, 1, md)
136
+ #encrypted_data = cipher.update(data) + cipher.final
137
+
138
+ File.open(source_file + ".enc", "wb") do |outf|
139
+ File.open(source_file, "rb") do |inf|
140
+ while inf.read(4096, buf)
141
+
142
+ if !set_magic_salt
143
+ outf << @@OPENSSL_MAGIC
144
+ outf << salt
145
+ set_magic_salt = true
146
+ end
147
+
148
+ outf << cipher.update(buf)
149
+ end
150
+ outf << cipher.final
151
+ end
152
+ end
153
+ end
154
+
155
+ def decrypt_in_memory(password, source_file, cipher = @@DEFAULT_CIPHER, md = @@DEFAULT_MD.new)
156
+
157
+ buf = ""
158
+ data = ""
159
+ got_magic_salt = false
160
+
161
+ File.open(source_file, "rb") do |inf|
162
+
163
+ while inf.read(4096, buf)
164
+
165
+ if !got_magic_salt
166
+ input_magic = buf.slice!(0, 8)
167
+ input_salt = buf.slice!(0, 8)
168
+ cipher = OpenSSL::Cipher::Cipher.new(cipher)
169
+ cipher.decrypt
170
+ cipher.pkcs5_keyivgen(password, input_salt, 1, md)
171
+ got_magic_salt = true
172
+ end
173
+
174
+ data += cipher.update(buf)
175
+
176
+ end
177
+
178
+ data += cipher.final
179
+ end
180
+
181
+ return data
182
+ end
183
+
184
+ # Data may be written from the command line with
185
+ # `openssl enc -#{cipher} -md #{md} -in #{INFILE} -out #{OUTFILE}`
186
+ # and the resulting bytes may be read by this function.
187
+ #
188
+ # Example:
189
+ # openssl enc -aes-256-cbc -md sha256 -in file.txt -out file.txt.encrypted
190
+ def decrypt_in_file(password, source_file, cipher = @@DEFAULT_CIPHER, md = @@DEFAULT_MD.new)
191
+
192
+ buf = ""
193
+ got_magic_salt = false
194
+
195
+ File.open(source_file + ".dec", "wb") do |outf|
196
+ File.open(source_file, "rb") do |inf|
197
+
198
+ while inf.read(4096, buf)
199
+
200
+ if !got_magic_salt
201
+ input_magic = buf.slice!(0, 8)
202
+ input_salt = buf.slice!(0, 8)
203
+ cipher = OpenSSL::Cipher::Cipher.new(cipher)
204
+ cipher.decrypt
205
+ cipher.pkcs5_keyivgen(password, input_salt, 1, md)
206
+ got_magic_salt = true
207
+ end
208
+
209
+ outf << cipher.update(buf)
210
+
211
+ end
212
+
213
+ outf << cipher.final
214
+ end
215
+ end
216
+ end
217
+
218
+ end
219
+
220
+ ########################################################################################
221
+ # DEBUG CODE
222
+ =begin
223
+
224
+ def main()
225
+ ce = Cryptenv.new()
226
+
227
+ #ce.load_credential_file("ci_env.enc")
228
+ #puts "env: " + cm.env.to_s
229
+
230
+ ce.decrypt_in_file("This is the passphrase", "../../../ci_env.json.enc")
231
+ puts "encrypted !"
232
+
233
+ #puts ce.decrypt_in_memory("This is the passphrase", "ci_env.enc")
234
+ #puts "decrypted in memory !"
235
+
236
+ #puts JSON.pretty_generate(ce.get("adhoc_report","real_user","credentials","qa"))
237
+
238
+ end
239
+
240
+
241
+
242
+ #######################################################################################
243
+ # Old School launch !
244
+ main()
245
+ =end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cryptenv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Marco Sylvestre
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-31 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Manage environement information (like credentials) in an encrypted .json
14
+ file with ease.
15
+ email: marco.sylvestre@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/cryptenv.rb
21
+ homepage: http://rubygems.org/gems/cryptenv
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.4.8
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Secure environment information lib.
45
+ test_files: []