classifieds 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cabedbd14c81b9a85f94de1dfd63dee58310e433
4
- data.tar.gz: 440f0c7aa0f547f02ca14b5b78ed95636ef67c89
3
+ metadata.gz: d6d409658285a51984fa62c9c70833593f6ec516
4
+ data.tar.gz: 3245625b00c570c2e0f5532b49d453bcc44cdad6
5
5
  SHA512:
6
- metadata.gz: 20f7942d408a41df7407938fa1b881c12f061f4bc886519ea2d58c9aa87a95dc3e5a56c6dff094c54e80f4c3eb108ed8084cc57db37c6985f5235e5bcdc8c4ed
7
- data.tar.gz: c48e8c98405cf4ebda709f2b95dbcfb6e3bc927885f2d23a98602500d9db66bcd5d70e956fcb27025cda86f192046f1305ca4ab2d3498e43ee3a7f2bc460cc51
6
+ metadata.gz: 47fb6152fb475e202712ed4dda885c0c334134680ea3d9e6ced9f481453d858e43622b58ed967772c4e2bf87bb48644ee9dc6369a93b32880316cd20954e3847
7
+ data.tar.gz: 5df1af17642507a30ecad363ae0fe4d39e4a8a7b88ea9e5fabdfff7f5ebcc279f17b1878af3afb1f296fa5221a54eca65469a1a4a8e8f2080bb00de7cbb401fd
@@ -1,5 +1,6 @@
1
1
  require 'classifieds/exception'
2
2
  require 'classifieds/parser'
3
+ require 'classifieds/encryptor'
3
4
  require 'classifieds/main'
4
5
 
5
6
  module Classifieds
@@ -0,0 +1,35 @@
1
+ module Classifieds
2
+ class Encryptor
3
+ def initialize(password, salt)
4
+ @cipher = OpenSSL::Cipher.new('AES-256-CBC')
5
+ @password = password
6
+ @salt = salt
7
+ end
8
+
9
+ def encrypt(data)
10
+ @cipher.encrypt
11
+ key_iv = OpenSSL::PKCS5.pbkdf2_hmac_sha1(
12
+ @password,
13
+ @salt,
14
+ 1000,
15
+ @cipher.key_len + @cipher.iv_len
16
+ )
17
+ @cipher.key = key_iv[0, @cipher.key_len]
18
+ @cipher.iv = key_iv[@cipher.key_len, @cipher.iv_len]
19
+ Base64.encode64(@cipher.update(data) + @cipher.final)
20
+ end
21
+
22
+ def decrypt(data)
23
+ @cipher.decrypt
24
+ key_iv = OpenSSL::PKCS5.pbkdf2_hmac_sha1(
25
+ @password,
26
+ @salt,
27
+ 1000,
28
+ @cipher.key_len + @cipher.iv_len
29
+ )
30
+ @cipher.key = key_iv[0, @cipher.key_len]
31
+ @cipher.iv = key_iv[@cipher.key_len, @cipher.iv_len]
32
+ @cipher.update(Base64.decode64(data)) + @cipher.final
33
+ end
34
+ end
35
+ end
@@ -2,6 +2,7 @@ require 'digest/sha1'
2
2
  require 'openssl'
3
3
  require 'base64'
4
4
  require 'fileutils'
5
+ require 'find'
5
6
 
6
7
  require 'safe_colorize'
7
8
 
@@ -12,12 +13,13 @@ module Classifieds
12
13
  using SafeColorize
13
14
 
14
15
  def initialize(*args)
15
- unless File.exists?(SOURCE_FILE)
16
+ unless classifieds?
16
17
  STDERR.puts "#{SOURCE_FILE} is not found in this repository".color(:red)
17
18
  exit 1
18
19
  end
19
20
 
20
- FileUtils.mkdir_p(SOURCE_DIRECTORY) unless Dir.exists?(SOURCE_DIRECTORY)
21
+ source_directory = File.join(root_directory, SOURCE_DIRECTORY)
22
+ FileUtils.mkdir_p(source_directory) unless Dir.exists?(source_directory)
21
23
  @prefix = Digest::SHA1.hexdigest('classifieds')
22
24
  super
23
25
  end
@@ -25,17 +27,17 @@ module Classifieds
25
27
  desc 'keygen', 'Generate identity files using by public key encryption'
26
28
  option :force, type: :boolean, aliases: '-f'
27
29
  def keygen
28
- if !options[:force] && (File.exists?(PUBLIC_KEY_PATH) && File.exists?(COMMON_KEY_PATH))
30
+ if !options[:force] && keygenerated?
29
31
  STDERR.puts 'Already generated in this repository'.color(:red)
30
32
  exit 1
31
33
  else
32
34
  OpenSSL::Random.seed(File.read('/dev/random', 16))
33
35
  rsa = OpenSSL::PKey::RSA.new(2048)
34
36
  pub = rsa.public_key
35
- File.open(PUBLIC_KEY_PATH, 'w') do |f|
37
+ File.open(File.join(root_directory, PUBLIC_KEY_PATH), 'w') do |f|
36
38
  f.puts pub.to_pem
37
39
  end
38
- File.open(COMMON_KEY_PATH, 'w') do |f|
40
+ File.open(File.join(root_directory, COMMON_KEY_PATH), 'w') do |f|
39
41
  f.puts pub.public_encrypt(OpenSSL::Random.random_bytes(16))
40
42
  end
41
43
  puts rsa
@@ -47,7 +49,7 @@ module Classifieds
47
49
  def encrypt
48
50
  if identity_file = options[:identity_file]
49
51
  rsa = OpenSSL::PKey::RSA.new(File.read(identity_file).chomp)
50
- @password = rsa.private_decrypt(File.read(COMMON_KEY_PATH).chomp)
52
+ @password = rsa.private_decrypt(File.read(File.join(root_directory, COMMON_KEY_PATH)).chomp)
51
53
  else
52
54
  @password = ask_password
53
55
  retype_password
@@ -58,7 +60,6 @@ module Classifieds
58
60
 
59
61
  file = File.open(file_path, 'r+')
60
62
  file.flock(File::LOCK_EX)
61
-
62
63
  data = file.read.chomp
63
64
 
64
65
  begin
@@ -83,7 +84,7 @@ module Classifieds
83
84
  def decrypt
84
85
  if identity_file = options[:identity_file]
85
86
  rsa = OpenSSL::PKey::RSA.new(File.read(identity_file).chomp)
86
- @password = rsa.private_decrypt(File.read(COMMON_KEY_PATH).chomp)
87
+ @password = rsa.private_decrypt(File.read(File.join(root_directory, COMMON_KEY_PATH)).chomp)
87
88
  else
88
89
  @password = ask_password
89
90
  end
@@ -94,7 +95,6 @@ module Classifieds
94
95
 
95
96
  file = File.open(file_path, 'r+')
96
97
  file.flock(File::LOCK_EX)
97
-
98
98
  file.read(@prefix.size)
99
99
  data = file.read.chomp
100
100
 
@@ -159,35 +159,41 @@ module Classifieds
159
159
  end
160
160
 
161
161
  def classifieds
162
- Parser.parse(File.read(SOURCE_FILE).chomp)
162
+ Parser.parse(File.read(File.join(root_directory, SOURCE_FILE)).chomp)
163
163
  end
164
164
 
165
165
  def encrypt_data(data)
166
- cipher = OpenSSL::Cipher.new('AES-256-CBC')
167
- cipher.encrypt
168
- key_iv = OpenSSL::PKCS5.pbkdf2_hmac_sha1(
169
- @password,
170
- File.expand_path(File.dirname(__FILE__)).split('/').pop,
171
- 1000,
172
- cipher.key_len + cipher.iv_len
173
- )
174
- cipher.key = key_iv[0, cipher.key_len]
175
- cipher.iv = key_iv[cipher.key_len, cipher.iv_len]
176
- Base64.encode64(cipher.update(data) + cipher.final)
166
+ encryptor = Encryptor.new(@password, root_directory.split('/').pop)
167
+ encryptor.encrypt(data)
177
168
  end
178
169
 
179
170
  def decrypt_data(data)
180
- cipher = OpenSSL::Cipher.new('AES-256-CBC')
181
- cipher.decrypt
182
- key_iv = OpenSSL::PKCS5.pbkdf2_hmac_sha1(
183
- @password,
184
- File.expand_path(File.dirname(__FILE__)).split('/').pop,
185
- 1000,
186
- cipher.key_len + cipher.iv_len
187
- )
188
- cipher.key = key_iv[0, cipher.key_len]
189
- cipher.iv = key_iv[cipher.key_len, cipher.iv_len]
190
- cipher.update(Base64.decode64(data)) + cipher.final
171
+ encryptor = Encryptor.new(@password, root_directory.split('/').pop)
172
+ encryptor.decrypt(data)
173
+ end
174
+
175
+ def root_directory
176
+ @root_directory ||=
177
+ begin
178
+ target_directory = File.expand_path(Dir.pwd)
179
+ until target_directory == '/' do
180
+ Find.find(target_directory) do |path|
181
+ Find.prune if path =~ %r|^#{target_directory}/|
182
+ return target_directory if path =~ /#{SOURCE_FILE}$/
183
+ end
184
+ target_directory = File.expand_path('..', target_directory)
185
+ end
186
+
187
+ nil
188
+ end
189
+ end
190
+
191
+ def classifieds?
192
+ !!root_directory
193
+ end
194
+
195
+ def keygenerated?
196
+ File.exists?(File.join(root_directory, PUBLIC_KEY_PATH)) && File.exists?(File.join(root_directory, COMMON_KEY_PATH))
191
197
  end
192
198
 
193
199
  def encrypted?(file)
@@ -1,3 +1,3 @@
1
1
  module Classifieds
2
- VERSION = '0.2.1'
2
+ VERSION = '0.2.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: classifieds
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaihar4
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-25 00:00:00.000000000 Z
11
+ date: 2015-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -84,6 +84,7 @@ files:
84
84
  - classifieds.gemspec
85
85
  - exe/classifieds
86
86
  - lib/classifieds.rb
87
+ - lib/classifieds/encryptor.rb
87
88
  - lib/classifieds/exception.rb
88
89
  - lib/classifieds/main.rb
89
90
  - lib/classifieds/parser.rb