ossl_cryptor 0.1.0.1 → 0.2.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: 369196e6ffdf50c0bb4b869458fb62bda29afa6c
4
- data.tar.gz: a41973bc6fbef1e834543bc189b96f4366c6dba8
3
+ metadata.gz: 063bae8ea6b7d56fe6e153bc3abc19fbe1a13811
4
+ data.tar.gz: af280d9d8f661ed7ac879fa0d66eda52e10e72cb
5
5
  SHA512:
6
- metadata.gz: 1d68ad535bde9c473a56910172dbe9c35f051a9f79a2aeef55377c189bab25c0c3dc5f5a56667f7c5ef8d612e6ffe29dbd5c8cf1f1e851fd54d4fb8f7dfe34cc
7
- data.tar.gz: 7287695ecee81db4cd46631a5aab79d701e24e53a00b399bcd159e29a1c1c26cb9d33e2e0ab55f9461b0546b435f85bc2718cce820ac9f7ef791ce2229ba6a2f
6
+ metadata.gz: 3d74640cd7433fbda3a7749af35c41c5a35a065892c1fa4b0368770d5160bd02f93fa1ba2e401dc0d139e8fc5ac4c84c5a23c225f718b61ffc1a02677202f30f
7
+ data.tar.gz: fa9f2880869bc268e355efafe532c9d531e08b81f3a5dd502462b3f9076bbde26192dc1ef650d6ccdb459d4daa4c66392a823d69378bddbebe55926e7b828f2b
data/README.md CHANGED
@@ -2,7 +2,8 @@
2
2
 
3
3
  Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ossl_cryptor`. To experiment with that code, run `bin/console` for an interactive prompt.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ This gem provide crypt process by DES and AES-256-CBC.
6
+ Use openssl lib.
6
7
 
7
8
  ## Installation
8
9
 
@@ -22,7 +23,22 @@ Or install it yourself as:
22
23
 
23
24
  ## Usage
24
25
 
25
- TODO: Write usage instructions here
26
+ You implement the method of the AES-256-CBC as a sample.
27
+ DES can also be run in the same way (only the specified mode is different).
28
+
29
+ ```ruby
30
+ gem 'ossl_cryptor'
31
+
32
+ cryptor = OsslCryptor::Cryptor.new(OsslCryptor::AES)
33
+
34
+ # encrypt
35
+ enc_value = cryptor.encrypt(aes_encrypt_value)
36
+ p enc_value
37
+
38
+ # decrypt
39
+ dec_value = cryptor.decrypt(enc_value)
40
+ p dec_value
41
+ ```
26
42
 
27
43
  ## Development
28
44
 
@@ -32,7 +48,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
48
 
33
49
  ## Contributing
34
50
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ossl_cryptor. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/h-shigemoto/ossl_cryptor. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
36
52
 
37
53
 
38
54
  ## License
@@ -1,3 +1,3 @@
1
1
  module OsslCryptor
2
- VERSION = "0.1.0.1"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/ossl_cryptor.rb CHANGED
@@ -1,7 +1,9 @@
1
1
  require "ossl_cryptor/version"
2
2
  require "ossl_cryptor/cipher_generator"
3
3
  require "base64"
4
+ require "openssl"
4
5
 
6
+ # openssl crypt module
5
7
  module OsslCryptor
6
8
 
7
9
  # DES Mode.
@@ -26,13 +28,20 @@ module OsslCryptor
26
28
  end
27
29
 
28
30
  # generate cipher instance.
29
- @cipher = setup(mode, key_iv, pass, salt, key_iv_hash)
31
+ @cipher = CipherGenerator.generate_cipher(mode)
32
+ # set initialize parameter and generate key, iv
30
33
  @mode = mode
34
+ @default_key_iv = key_iv
35
+ @pass = pass.nil? ? CipherGenerator::DEFAULT_PASS : pass
36
+ @salt = salt
37
+ @key_iv_hash = key_iv_hash.nil? ? CipherGenerator::DEFAULT_KEY_IV_HASH : key_iv_hash
38
+ @key_iv = generate_key_iv(@mode, @default_key_iv, @pass, @salt, @key_iv_hash)
31
39
  end
32
40
 
33
41
  # reset cipher instance.
34
42
  def reset
35
- @cipher = setup(@mode, @key)
43
+ @cipher = CipherGenerator.generate_cipher(@mode)
44
+ @key_iv = generate_key_iv(@mode, @default_key_iv, @pass, @salt, @key_iv_hash)
36
45
  end
37
46
 
38
47
  # encrypt value.
@@ -42,9 +51,13 @@ module OsslCryptor
42
51
 
43
52
  # prepare encrypt.
44
53
  @cipher.encrypt
54
+ set_key_iv
45
55
 
46
56
  # encrypt.
47
- encrypt_value = @cipher.update(value) + @cipher.final
57
+ encrypt_value = ""
58
+ encrypt_value << @cipher.update(value)
59
+ encrypt_value << @cipher.final
60
+
48
61
  # encode base64.
49
62
  if encode_base64
50
63
  encrypt_value = Base64.encode64(encrypt_value)
@@ -60,18 +73,17 @@ module OsslCryptor
60
73
 
61
74
  # prepare decrypt.
62
75
  @cipher.decrypt
76
+ set_key_iv
63
77
 
64
78
  # decode base64.
65
79
  if decode_base64
66
80
  value = Base64.decode64(value)
67
81
  end
68
82
 
69
- if @mode == AES
70
- @cipher.padding = 0
71
- end
72
-
73
83
  # decrypt.
74
- decrypt_value = @cipher.update(value) + @cipher.final
84
+ decrypt_value = ""
85
+ decrypt_value << @cipher.update(value)
86
+ decrypt_value << @cipher.final
75
87
  decrypt_value
76
88
  end
77
89
 
@@ -90,39 +102,31 @@ module OsslCryptor
90
102
  private
91
103
 
92
104
  # setup cipher instance.
93
- # @param [String] mode cipher mode.
105
+ # @param [String] mode crypt mode.
94
106
  # @param [Hash] key_iv key and iv. key_iv[:key] = key, key_iv[:iv] = iv
95
107
  # @param [String] pass password, if pass = nil, use CipherGenerator::DEFAULT_PASS
96
108
  # @param [String] salt salt data. if salt = nil, use random salt.
97
- # @param [String] key_iv_hash use hash algorithm when key and iv generate. if key_iv_hash = nil, use CipherGenerator::DEFAULT_KEY_IV_HASH
98
- # @return [Cipher] cipher instance.
99
- def setup (mode, key_iv=nil, pass=nil, salt=nil, key_iv_hash=nil)
109
+ # @param [String] hash use hash algorithm when key and iv generate. if key_iv_hash = nil, use CipherGenerator::DEFAULT_KEY_IV_HASH
110
+ # @return [Hash] key and iv hash.
111
+ def generate_key_iv(mode, key_iv=nil, pass=nil, salt=nil, hash=nil)
100
112
 
101
- # generate OpenSSL::Cipher instance.
102
- cipher = CipherGenerator.generate_cipher(mode)
113
+ cipher_key_iv = nil
103
114
 
104
115
  # if key_iv = nil, generate key and iv.
105
116
  if key_iv.nil?
106
- pass = pass.nil? ? CipherGenerator::DEFAULT_PASS : pass
107
117
  salt = salt.nil? ? get_default_salt(mode) : salt
108
- hash = key_iv_hash.nil? ? CipherGenerator::DEFAULT_KEY_IV_HASH : key_iv_hash
109
- key_len = cipher.key_len + cipher.iv_len
110
- key_iv_str = OpenSSL::PKCS5.pbkdf2_hmac(pass, salt, 2000, key_len, hash)
111
- key = key_iv_str[0, cipher.key_len]
112
- iv = key_iv_str[cipher.key_len, key_len]
113
- cipher.key = key
114
- cipher.iv = iv
115
- @key_iv = { key: key, iv: iv }
118
+ key_iv_str = OpenSSL::PKCS5.pbkdf2_hmac(pass, salt, 2000, (@cipher.key_len + @cipher.iv_len), hash)
119
+ key = key_iv_str[0, @cipher.key_len]
120
+ iv = key_iv_str[@cipher.key_len, @cipher.iv_len]
121
+ cipher_key_iv = { key: key, iv: iv }
116
122
  else
117
- cipher.key = key_iv[:key]
118
- cipher.iv = key_iv[:iv]
119
- @key_iv = key_iv
123
+ cipher_key_iv = key_iv
120
124
  end
121
125
 
122
- cipher
126
+ cipher_key_iv
123
127
  end
124
128
 
125
- # get default sold depend on mode.
129
+ # get default salt depend on mode.
126
130
  # @param [String] mode cipher mode.
127
131
  # @return [String] default salt.
128
132
  def get_default_salt(mode)
@@ -137,6 +141,12 @@ module OsslCryptor
137
141
 
138
142
  default_salt
139
143
  end
144
+
145
+ # set key and iv to cipher instance.
146
+ def set_key_iv
147
+ @cipher.key = @key_iv[:key]
148
+ @cipher.iv = @key_iv[:iv]
149
+ end
140
150
  end
141
151
 
142
152
  # get available crypt mode.
data/ossl_cryptor.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["corporation.ore@gmail.com"]
11
11
 
12
12
  spec.summary = %q{This gem provide crypt by AES-256-CBC and DES.}
13
- spec.description = %q{This gem provide crypt by AES-256-CBC and DES. Use openssl lib. But now, only DES of properly available.}
13
+ spec.description = %q{This gem provide crypt by AES-256-CBC and DES. Use openssl lib.}
14
14
  spec.homepage = "https://github.com/h-shigemoto/ossl_cryptor"
15
15
  spec.license = "MIT"
16
16
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ossl_cryptor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - h.shigemoto
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-06-26 00:00:00.000000000 Z
11
+ date: 2016-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,8 +52,7 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
- description: This gem provide crypt by AES-256-CBC and DES. Use openssl lib. But now,
56
- only DES of properly available.
55
+ description: This gem provide crypt by AES-256-CBC and DES. Use openssl lib.
57
56
  email:
58
57
  - corporation.ore@gmail.com
59
58
  executables: []