cryptology 1.2.0 → 2.0.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 22580eefd3a2222b7f68ac139bbc4e4e6ef2b1f8
4
- data.tar.gz: 514c5cae153148249950f52a5b8cad69cb9f7d8b
3
+ metadata.gz: 3b487542337e1013f886ac420c0e2a53b0ec22e3
4
+ data.tar.gz: 9c1e6d5ace17fa5fbedcfd6c6d870e27e7f076f0
5
5
  SHA512:
6
- metadata.gz: c565bdf3263f22f22223bacec3390c459d426302b632c8f42765e33c0afcdfdd45899a8cdcd40fbd2ca60b984343414c6e989352c67e4a6f74c9f74377106f9a
7
- data.tar.gz: 2d7cb662f8a8718080e3d2f1bc0c7b5c2174ab9e73e3c82789079d8ed406946c943e8203fb7ae73d6534042f66617fb310dadf6841b43c63b2555d1ea014405d
6
+ metadata.gz: eac2db8af311b962af96c6e83f02591e181b65dd147268cf7272b4040557c8e8b9a0cc85b3851fe0c923f8bda14e7f4e140554f620a7f52fda140bf402355611
7
+ data.tar.gz: 0c39c3408209210ff0f05337ae94196a638a3fdd831d83e2ada51262a6d2d5887016ac6beff5c22a62c27938b61f0397b6b73c70c80a389f97e07b6e925dd2d6
data/.rubocop.yml ADDED
@@ -0,0 +1,17 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+
4
+ Metrics/BlockLength:
5
+ ExcludedMethods: RSpec.describe, context
6
+
7
+ Metrics/LineLength:
8
+ Max: 100
9
+
10
+ Metrics/ParameterLists:
11
+ Max: 6
12
+
13
+ Style/Documentation:
14
+ Enabled: false
15
+
16
+ Style/FrozenStringLiteralComment:
17
+ Enabled: false
data/.travis.yml CHANGED
@@ -1,7 +1,7 @@
1
+ sudo: false
1
2
  language: ruby
2
-
3
3
  rvm:
4
- - 2.1.7
5
- - 2.2.3
6
-
7
- sudo: false
4
+ - 2.1.10
5
+ - 2.2.7
6
+ - 2.3.4
7
+ - 2.4.1
data/CHANGELOG.md CHANGED
@@ -1,16 +1,24 @@
1
+ ## 2.0.0 (2017-06-27)
2
+
3
+ - New: `encrypt` method returns a hash
4
+ - Improvement: `key` hashing method updated
5
+ - Improvement: `iv` is always set if algorithm supports it
6
+ - Improvement: Optional `salt` and `iter` arguments
7
+ - Ruby versioning: Support for Ruby 2.4
8
+
1
9
  ## 1.2.0 (2015-12-10)
2
10
 
3
- - Improvement: Add decryptable? helper method
11
+ - Improvement: Add `decryptable?` helper method
4
12
 
5
13
  ## 1.1.0 (2015-12-08)
6
14
 
7
15
  - Improvement: Switch to keyword arguments
8
- - Ruby Versioning: Drop support for Ruby 2.0
16
+ - Ruby versioning: Drop support for Ruby 2.0
9
17
 
10
18
  ## 1.0.1 (2015-12-08)
11
19
 
12
- - Bug Fix: Allow iv to be nil
20
+ - Bug fix: Allow iv to be nil
13
21
 
14
22
  ## 1.0.0 (2015-12-07)
15
23
 
16
- - Initial release
24
+ - Initial release
data/Rakefile CHANGED
@@ -1,4 +1,6 @@
1
1
  require 'bundler/gem_tasks'
2
2
  require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
3
6
  task default: :spec
4
- RSpec::Core::RakeTask.new
data/cryptology.gemspec CHANGED
@@ -1,4 +1,5 @@
1
1
  # coding: utf-8
2
+
2
3
  lib = File.expand_path('../lib', __FILE__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'cryptology/version'
@@ -9,18 +10,18 @@ Gem::Specification.new do |spec|
9
10
  spec.authors = ['Dmitriy Tarasov']
10
11
  spec.email = ['info@rubysamurai.com']
11
12
 
12
- spec.summary = 'Wrapper for symmetric encryption and decryption using any algorithm supported by OpenSSL'
13
- spec.description = 'Wrapper for symmetric encryption and decryption using any algorithm supported by OpenSSL'
13
+ spec.summary = 'Symmetric encryption and decryption with OpenSSL'
14
+ spec.description = 'Symmetric encryption and decryption with OpenSSL'
14
15
  spec.homepage = 'https://github.com/rubysamurai/cryptology'
15
16
  spec.license = 'MIT'
16
17
 
17
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec|features)/}) }
18
19
  spec.bindir = 'exe'
19
20
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
21
  spec.require_paths = ['lib']
21
22
 
22
23
  spec.required_ruby_version = '>= 2.1.0'
23
24
 
24
- spec.add_development_dependency 'rake', '~> 10.0'
25
- spec.add_development_dependency 'rspec', '~> 3.3'
25
+ spec.add_development_dependency 'rake', '>= 12.0'
26
+ spec.add_development_dependency 'rspec', '>= 3.6'
26
27
  end
data/lib/cryptology.rb CHANGED
@@ -3,42 +3,50 @@ require 'openssl'
3
3
  require 'base64'
4
4
 
5
5
  module Cryptology
6
- def self.encrypt(data:, key:, cipher: 'AES-256-CBC', iv: nil)
7
- encrypted = encrypt_data(data.to_s, digest(key), cipher, iv)
8
- ::Base64.encode64(encrypted)
6
+ def self.encrypt(data:, key:, salt: nil, iter: 10_000, cipher: 'AES-256-CBC', iv: nil)
7
+ salt ||= OpenSSL::Random.random_bytes(16)
8
+ iv ||= OpenSSL::Cipher.new(cipher).random_iv
9
+ encrypted = encrypt_data(data.to_s, digest_key(key, salt, iter), cipher, iv)
10
+ { 'cipher' => cipher,
11
+ 'salt' => salt,
12
+ 'iter' => iter,
13
+ 'iv' => iv,
14
+ 'data' => ::Base64.encode64(encrypted) }
9
15
  end
10
16
 
11
- def self.decrypt(data:, key:, cipher: 'AES-256-CBC', iv: nil)
17
+ def self.decrypt(data:, key:, salt:, iter: 10_000, cipher: 'AES-256-CBC', iv:)
12
18
  base64_decoded = ::Base64.decode64(data.to_s)
13
- decrypt_data(base64_decoded, digest(key), cipher, iv)
19
+ decrypt_data(base64_decoded, digest_key(key, salt, iter), cipher, iv)
14
20
  .force_encoding('UTF-8').encode
15
21
  end
16
22
 
17
- def self.decryptable?(data:, key:, cipher: 'AES-256-CBC', iv: nil)
18
- return true if decrypt(data: data, key: key, cipher: cipher, iv: iv)
23
+ def self.decryptable?(data:, key:, salt:, iter: 10_000, cipher: 'AES-256-CBC', iv:)
24
+ return true if decrypt(data: data, key: key, salt: salt, iter: iter, cipher: cipher, iv: iv)
19
25
  rescue OpenSSL::Cipher::CipherError
20
26
  return false
21
27
  end
22
28
 
23
- private
29
+ def self.encrypt_data(data, key, cipher, iv)
30
+ c = OpenSSL::Cipher.new(cipher)
31
+ c.encrypt
32
+ c.key = key
33
+ c.iv = iv unless iv.length != c.random_iv.length
34
+ c.update(data) + c.final
35
+ end
24
36
 
25
- def self.encrypt_data(data, key, cipher, iv)
26
- cipher = ::OpenSSL::Cipher::Cipher.new(cipher)
27
- cipher.encrypt
28
- cipher.key = key
29
- cipher.iv = iv unless iv.nil?
30
- cipher.update(data) + cipher.final
31
- end
37
+ def self.decrypt_data(data, key, cipher, iv)
38
+ decipher = OpenSSL::Cipher.new(cipher)
39
+ decipher.decrypt
40
+ decipher.key = key
41
+ decipher.iv = iv unless iv.length != decipher.random_iv.length
42
+ decipher.update(data) + decipher.final
43
+ end
32
44
 
33
- def self.decrypt_data(data, key, cipher, iv)
34
- decipher = ::OpenSSL::Cipher::Cipher.new(cipher)
35
- decipher.decrypt
36
- decipher.key = key
37
- decipher.iv = iv unless iv.nil?
38
- decipher.update(data) + decipher.final
39
- end
45
+ def self.digest_key(key, salt, iter)
46
+ digest = OpenSSL::Digest::SHA256.new
47
+ len = digest.digest_length
48
+ OpenSSL::PKCS5.pbkdf2_hmac(key, salt, iter, len, digest)
49
+ end
40
50
 
41
- def self.digest(key)
42
- ::OpenSSL::Digest::SHA256.digest(key)
43
- end
51
+ private_class_method :encrypt_data, :decrypt_data, :digest_key
44
52
  end
@@ -1,3 +1,3 @@
1
1
  module Cryptology
2
- VERSION = '1.2.0'
2
+ VERSION = '2.0.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,45 +1,44 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cryptology
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitriy Tarasov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-12-09 00:00:00.000000000 Z
11
+ date: 2017-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '10.0'
19
+ version: '12.0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '10.0'
26
+ version: '12.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '3.3'
33
+ version: '3.6'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '3.3'
41
- description: Wrapper for symmetric encryption and decryption using any algorithm supported
42
- by OpenSSL
40
+ version: '3.6'
41
+ description: Symmetric encryption and decryption with OpenSSL
43
42
  email:
44
43
  - info@rubysamurai.com
45
44
  executables: []
@@ -48,6 +47,7 @@ extra_rdoc_files: []
48
47
  files:
49
48
  - ".gitignore"
50
49
  - ".rspec"
50
+ - ".rubocop.yml"
51
51
  - ".travis.yml"
52
52
  - CHANGELOG.md
53
53
  - Gemfile
@@ -79,9 +79,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
79
  version: '0'
80
80
  requirements: []
81
81
  rubyforge_project:
82
- rubygems_version: 2.4.8
82
+ rubygems_version: 2.6.12
83
83
  signing_key:
84
84
  specification_version: 4
85
- summary: Wrapper for symmetric encryption and decryption using any algorithm supported
86
- by OpenSSL
85
+ summary: Symmetric encryption and decryption with OpenSSL
87
86
  test_files: []