evp_bytes_to_key 0.1.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 +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.rubocop.yml +53 -0
- data/.travis.yml +6 -0
- data/CHANGELOG.md +8 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +63 -0
- data/LICENSE.txt +177 -0
- data/README.md +252 -0
- data/Rakefile +8 -0
- data/bin/console +8 -0
- data/bin/setup +6 -0
- data/evp_bytes_to_key.gemspec +29 -0
- data/lib/evp_bytes_to_key.rb +9 -0
- data/lib/evp_bytes_to_key/error.rb +6 -0
- data/lib/evp_bytes_to_key/key.rb +116 -0
- data/lib/evp_bytes_to_key/version.rb +6 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bb6c3d29fea1e070a1fa7e82bdfc22d307d222e804e1139a6b58b77a86af5104
|
4
|
+
data.tar.gz: cac2ccad33d79e0d2eee561a574db37002466f003e110cee37de15c432a5cab4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ae248998f96ac0a2313eca5d77420b6675415acc685e5da17cac69582485f43a7fa020685d5d42160f9387d8267c19ef2641eb74ae2062f5a03a90d7b93ec3e8
|
7
|
+
data.tar.gz: 94c6277abed9b3be639e6cc948aa00f76188a402d2cb5a4fe65eb373b7003c7db3fdae6c3df2cb116012633754252608e94442bad4125809619e47d2c0a3a761
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.7
|
3
|
+
DisplayCopNames: true
|
4
|
+
Style/HashSyntax:
|
5
|
+
Enabled: false
|
6
|
+
Layout/LineLength:
|
7
|
+
Max: 140
|
8
|
+
Layout/EmptyLinesAroundAttributeAccessor:
|
9
|
+
Enabled: true
|
10
|
+
Layout/SpaceAroundMethodCallOperator:
|
11
|
+
Enabled: true
|
12
|
+
Lint/DeprecatedOpenSSLConstant:
|
13
|
+
Enabled: true
|
14
|
+
Lint/DuplicateElsifCondition:
|
15
|
+
Enabled: true
|
16
|
+
Lint/MixedRegexpCaptureTypes:
|
17
|
+
Enabled: true
|
18
|
+
Lint/RaiseException:
|
19
|
+
Enabled: true
|
20
|
+
Lint/StructNewOverride:
|
21
|
+
Enabled: true
|
22
|
+
Style/AccessorGrouping:
|
23
|
+
Enabled: true
|
24
|
+
Style/ArrayCoercion:
|
25
|
+
Enabled: true
|
26
|
+
Style/BisectedAttrAccessor:
|
27
|
+
Enabled: true
|
28
|
+
Style/CaseLikeIf:
|
29
|
+
Enabled: true
|
30
|
+
Style/ExponentialNotation:
|
31
|
+
Enabled: true
|
32
|
+
Style/HashAsLastArrayItem:
|
33
|
+
Enabled: true
|
34
|
+
Style/HashEachMethods:
|
35
|
+
Enabled: true
|
36
|
+
Style/HashLikeCase:
|
37
|
+
Enabled: true
|
38
|
+
Style/HashTransformKeys:
|
39
|
+
Enabled: true
|
40
|
+
Style/HashTransformValues:
|
41
|
+
Enabled: true
|
42
|
+
Style/RedundantAssignment:
|
43
|
+
Enabled: true
|
44
|
+
Style/RedundantFetchBlock:
|
45
|
+
Enabled: true
|
46
|
+
Style/RedundantFileExtensionInRequire:
|
47
|
+
Enabled: true
|
48
|
+
Style/RedundantRegexpCharacterClass:
|
49
|
+
Enabled: true
|
50
|
+
Style/RedundantRegexpEscape:
|
51
|
+
Enabled: true
|
52
|
+
Style/SlicingWithRange:
|
53
|
+
Enabled: true
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
evp_bytes_to_key (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.1)
|
10
|
+
coderay (1.1.3)
|
11
|
+
diff-lcs (1.4.4)
|
12
|
+
method_source (1.0.0)
|
13
|
+
parallel (1.19.2)
|
14
|
+
parser (2.7.1.4)
|
15
|
+
ast (~> 2.4.1)
|
16
|
+
pry (0.13.1)
|
17
|
+
coderay (~> 1.1)
|
18
|
+
method_source (~> 1.0)
|
19
|
+
rainbow (3.0.0)
|
20
|
+
rake (12.3.3)
|
21
|
+
regexp_parser (1.7.1)
|
22
|
+
rexml (3.2.4)
|
23
|
+
rspec (3.9.0)
|
24
|
+
rspec-core (~> 3.9.0)
|
25
|
+
rspec-expectations (~> 3.9.0)
|
26
|
+
rspec-mocks (~> 3.9.0)
|
27
|
+
rspec-core (3.9.2)
|
28
|
+
rspec-support (~> 3.9.3)
|
29
|
+
rspec-expectations (3.9.2)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.9.0)
|
32
|
+
rspec-mocks (3.9.1)
|
33
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
34
|
+
rspec-support (~> 3.9.0)
|
35
|
+
rspec-support (3.9.3)
|
36
|
+
rubocop (0.88.0)
|
37
|
+
parallel (~> 1.10)
|
38
|
+
parser (>= 2.7.1.1)
|
39
|
+
rainbow (>= 2.2.2, < 4.0)
|
40
|
+
regexp_parser (>= 1.7)
|
41
|
+
rexml
|
42
|
+
rubocop-ast (>= 0.1.0, < 1.0)
|
43
|
+
ruby-progressbar (~> 1.7)
|
44
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
45
|
+
rubocop-ast (0.1.0)
|
46
|
+
parser (>= 2.7.0.1)
|
47
|
+
ruby-progressbar (1.10.1)
|
48
|
+
unicode-display_width (1.7.0)
|
49
|
+
yard (0.9.25)
|
50
|
+
|
51
|
+
PLATFORMS
|
52
|
+
ruby
|
53
|
+
|
54
|
+
DEPENDENCIES
|
55
|
+
evp_bytes_to_key!
|
56
|
+
pry (~> 0.13.1)
|
57
|
+
rake (~> 12.0)
|
58
|
+
rspec (~> 3.9.0)
|
59
|
+
rubocop (~> 0.88.0)
|
60
|
+
yard (~> 0.9.25)
|
61
|
+
|
62
|
+
BUNDLED WITH
|
63
|
+
2.1.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,177 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
https://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
data/README.md
ADDED
@@ -0,0 +1,252 @@
|
|
1
|
+
# EvpBytesToKey
|
2
|
+
|
3
|
+
This gem is a pure Ruby implementation of OpenSSL's
|
4
|
+
[`EVP_BytesToKey()`](https://www.openssl.org/docs/man1.0.2/man3/EVP_BytesToKey.html) function as it is used by
|
5
|
+
the `openssl` command line utility. This function is used to generate a key and IV from a given password. (and
|
6
|
+
optional salt)
|
7
|
+
|
8
|
+
The purpose of this gem is to make it easier to encrypt or decrypt data that has been encrypted by `openssl`
|
9
|
+
with a password on the command line by replicating the logic used to derive a key and IV from a given password.
|
10
|
+
|
11
|
+
The [OpenSSL documentation](https://www.openssl.org/docs/man1.0.2/man3/EVP_BytesToKey.html) states:
|
12
|
+
|
13
|
+
> Newer applications should use a more modern algorithm such as PBKDF2
|
14
|
+
|
15
|
+
Therefore use of this key derivation function is at your own risk. It is provided here to make interoperation
|
16
|
+
with Ruby more convenient.
|
17
|
+
|
18
|
+
The is **not** a drop-in replacement for `EVP_BytesToKey()`. The [original
|
19
|
+
function](https://github.com/openssl/openssl/blob/2e9d61ecd81a6a512a0700486ccc1b3784b4c969/crypto/evp/evp_key.c#L78-L154)
|
20
|
+
supports specifying an algorithm (to determine key length and IV and IV length automatically), a message digest
|
21
|
+
(so a digest other than MD5 can be used), and the number of rounds.
|
22
|
+
|
23
|
+
This **is** a drop-in replacement for `EVP_BytesToKey()` as it is used by the `openssl` command line utility,
|
24
|
+
that is, using MD5 with 1 round. However, the options passed to this method require some knowledge of the algorithm
|
25
|
+
in use, e.g., `aes256` will use a 256-bit key and a 16-byte IV while `rc4-40` will use a 40-bit key
|
26
|
+
and no IV. These are provided as arguments to `EvpBytesToKey::Key.new`.
|
27
|
+
|
28
|
+
## Installation
|
29
|
+
|
30
|
+
Add this line to your application's Gemfile:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
gem 'evp_bytes_to_key'
|
34
|
+
```
|
35
|
+
|
36
|
+
And then execute:
|
37
|
+
|
38
|
+
$ bundle install
|
39
|
+
|
40
|
+
Or install it yourself as:
|
41
|
+
|
42
|
+
$ gem install evp_bytes_to_key
|
43
|
+
|
44
|
+
## Requirements
|
45
|
+
|
46
|
+
This gem requires Ruby 2.0.0 due to the format of its `gemspec` file but the `EvpBytesToKey` module has been tested to work in Ruby as low as 1.9.3.
|
47
|
+
|
48
|
+
## Usage
|
49
|
+
|
50
|
+
Require the gem first:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
require 'evp_bytes_to_key'
|
54
|
+
```
|
55
|
+
|
56
|
+
Set the arguments for key generation. All arguments are required.
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
# must be a string and may be empty
|
60
|
+
password = 'password'
|
61
|
+
|
62
|
+
# must be nil or an eight-byte string
|
63
|
+
salt = 'saltsalt'
|
64
|
+
|
65
|
+
# must be a positive integer divisible by 8
|
66
|
+
bits = 256
|
67
|
+
|
68
|
+
# must be an integer >= 0
|
69
|
+
iv_length = 16
|
70
|
+
```
|
71
|
+
|
72
|
+
Then use the `Key` class to get access to the key:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
key = EvpBytesToKey::Key.new(password, salt, bits, iv_length)
|
76
|
+
=> #<EvpBytesToKey::Key:0x00007fe388095330
|
77
|
+
@bits=256,
|
78
|
+
@iv="\v\\\xA7\xB1\b\x1F\x94\xB1\xAC\x12\xE3\xC8\xBA\x87\xD0Z",
|
79
|
+
@iv_hex="0b5ca7b1081f94b1ac12e3c8ba87d05a",
|
80
|
+
@iv_length=16,
|
81
|
+
@key="\xFD\xBD\xF3A\x9F\xFF\x98\xBD\xB0$\x13\x90\xF6*\x9D\xB3_J\xBA)\xD7uf7y\x971N\xBF\xC7\t\xF2",
|
82
|
+
@key_hex="fdbdf3419fff98bdb0241390f62a9db35f4aba29d77566377997314ebfc709f2",
|
83
|
+
@password="password",
|
84
|
+
@salt="saltsalt">
|
85
|
+
|
86
|
+
key.key_hex
|
87
|
+
=> "fdbdf3419fff98bdb0241390f62a9db35f4aba29d77566377997314ebfc709f2"
|
88
|
+
|
89
|
+
key.iv_hex
|
90
|
+
=> "0b5ca7b1081f94b1ac12e3c8ba87d05a"
|
91
|
+
|
92
|
+
key.key
|
93
|
+
=> "\xFD\xBD\xF3A\x9F\xFF\x98\xBD\xB0$\x13\x90\xF6*\x9D\xB3_J\xBA)\xD7uf7y\x971N\xBF\xC7\t\xF2"
|
94
|
+
|
95
|
+
key.iv
|
96
|
+
=> "\v\\\xA7\xB1\b\x1F\x94\xB1\xAC\x12\xE3\xC8\xBA\x87\xD0Z"
|
97
|
+
```
|
98
|
+
|
99
|
+
These values can be compared to the values returned by the command line utility `openssl`:
|
100
|
+
|
101
|
+
```bash
|
102
|
+
echo -n "foo" | openssl enc -e -base64 -aes256 -S 73616c7473616c74 -pass pass:password -p
|
103
|
+
|
104
|
+
salt=73616C7473616C74
|
105
|
+
key=FDBDF3419FFF98BDB0241390F62A9DB35F4ABA29D77566377997314EBFC709F2
|
106
|
+
iv =0B5CA7B1081F94B1AC12E3C8BA87D05A
|
107
|
+
U2FsdGVkX19zYWx0c2FsdOnid6UWvFAXeeXIe+sL0l8=
|
108
|
+
```
|
109
|
+
|
110
|
+
Note that `openssl` requires the salt in hexadecimal format:
|
111
|
+
|
112
|
+
```bash
|
113
|
+
printf saltsalt | xxd
|
114
|
+
00000000: 7361 6c74 7361 6c74 saltsalt
|
115
|
+
```
|
116
|
+
|
117
|
+
### Examples
|
118
|
+
|
119
|
+
#### `aes256` with a salt and IV
|
120
|
+
|
121
|
+
Encrypt with `openssl`:
|
122
|
+
|
123
|
+
```bash
|
124
|
+
echo -n "foo" | openssl enc -e -base64 -aes256 -S 73616c7473616c74 -pass pass:password
|
125
|
+
```
|
126
|
+
|
127
|
+
This returns:
|
128
|
+
|
129
|
+
U2FsdGVkX19zYWx0c2FsdOnid6UWvFAXeeXIe+sL0l8=
|
130
|
+
|
131
|
+
Decrypt with Ruby:
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
require 'openssl'
|
135
|
+
require 'base64'
|
136
|
+
require 'evp_bytes_to_key'
|
137
|
+
|
138
|
+
key = EvpBytesToKey::Key.new('password', 'saltsalt', 256, 16)
|
139
|
+
decipher = OpenSSL::Cipher.new('aes256')
|
140
|
+
decipher.decrypt
|
141
|
+
decipher.key = key.key
|
142
|
+
decipher.iv = key.iv
|
143
|
+
ciphertext = Base64.strict_decode64('U2FsdGVkX19zYWx0c2FsdOnid6UWvFAXeeXIe+sL0l8=')
|
144
|
+
ciphertext = ciphertext.byteslice(16..-1) if ciphertext.byteslice(0, 8) == 'Salted__'
|
145
|
+
plaintext = decipher.update(ciphertext) + decipher.final
|
146
|
+
```
|
147
|
+
|
148
|
+
This returns:
|
149
|
+
|
150
|
+
"foo"
|
151
|
+
|
152
|
+
Encrypt with Ruby:
|
153
|
+
|
154
|
+
```
|
155
|
+
require 'openssl'
|
156
|
+
require 'base64'
|
157
|
+
require 'evp_bytes_to_key'
|
158
|
+
|
159
|
+
salt = 'saltsalt'
|
160
|
+
key = EvpBytesToKey::Key.new('password', salt, 256, 16)
|
161
|
+
cipher = OpenSSL::Cipher.new('aes256')
|
162
|
+
cipher.encrypt
|
163
|
+
cipher.key = key.key
|
164
|
+
cipher.iv = key.iv
|
165
|
+
ciphertext = cipher.update('foo') + cipher.final
|
166
|
+
ciphertext = "Salted__#{salt}#{ciphertext}" if salt
|
167
|
+
ciphertext = Base64.strict_encode64(ciphertext)
|
168
|
+
```
|
169
|
+
|
170
|
+
This returns:
|
171
|
+
|
172
|
+
"U2FsdGVkX19zYWx0c2FsdOnid6UWvFAXeeXIe+sL0l8="
|
173
|
+
|
174
|
+
Decrypt with `openssl`:
|
175
|
+
|
176
|
+
```bash
|
177
|
+
echo -n "U2FsdGVkX19zYWx0c2FsdOnid6UWvFAXeeXIe+sL0l8=" | base64 --decode | openssl enc -d -aes256 -S 73616c7473616c74 -pass pass:password
|
178
|
+
```
|
179
|
+
|
180
|
+
This returns:
|
181
|
+
|
182
|
+
foo
|
183
|
+
|
184
|
+
#### `rc4` with no salt and no IV
|
185
|
+
|
186
|
+
Encrypt with `openssl`:
|
187
|
+
|
188
|
+
```bash
|
189
|
+
echo -n "foo" | openssl enc -e -base64 -rc4 -nosalt -pass pass:password
|
190
|
+
```
|
191
|
+
|
192
|
+
This returns:
|
193
|
+
|
194
|
+
jpdE
|
195
|
+
|
196
|
+
Decrypt with Ruby:
|
197
|
+
|
198
|
+
```ruby
|
199
|
+
require 'openssl'
|
200
|
+
require 'base64'
|
201
|
+
require 'evp_bytes_to_key'
|
202
|
+
|
203
|
+
key = EvpBytesToKey::Key.new('password', nil, 128, 0)
|
204
|
+
decipher = OpenSSL::Cipher.new('rc4')
|
205
|
+
decipher.decrypt
|
206
|
+
decipher.key = key.key
|
207
|
+
ciphertext = Base64.strict_decode64('jpdE')
|
208
|
+
plaintext = decipher.update(ciphertext) + decipher.final
|
209
|
+
```
|
210
|
+
|
211
|
+
This returns:
|
212
|
+
|
213
|
+
"foo"
|
214
|
+
|
215
|
+
Encrypt with Ruby:
|
216
|
+
|
217
|
+
```
|
218
|
+
require 'openssl'
|
219
|
+
require 'base64'
|
220
|
+
require 'evp_bytes_to_key'
|
221
|
+
|
222
|
+
key = EvpBytesToKey::Key.new('password', nil, 128, 0)
|
223
|
+
cipher = OpenSSL::Cipher.new('rc4')
|
224
|
+
cipher.encrypt
|
225
|
+
cipher.key = key.key
|
226
|
+
ciphertext = cipher.update('foo') + cipher.final
|
227
|
+
ciphertext = Base64.strict_encode64(ciphertext)
|
228
|
+
```
|
229
|
+
|
230
|
+
This returns:
|
231
|
+
|
232
|
+
"jpdE"
|
233
|
+
|
234
|
+
Decrypt with `openssl`:
|
235
|
+
|
236
|
+
```bash
|
237
|
+
echo -n "jpdE" | base64 --decode | openssl enc -d -rc4 -nosalt -pass pass:password
|
238
|
+
```
|
239
|
+
|
240
|
+
This returns:
|
241
|
+
|
242
|
+
foo
|
243
|
+
|
244
|
+
## Development
|
245
|
+
|
246
|
+
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.
|
247
|
+
|
248
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
249
|
+
|
250
|
+
## Contributing
|
251
|
+
|
252
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/anothermh/evp_bytes_to_key.
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/evp_bytes_to_key/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'evp_bytes_to_key'
|
7
|
+
spec.version = EvpBytesToKey::VERSION
|
8
|
+
spec.authors = ['anothermh']
|
9
|
+
spec.summary = "A pure Ruby implementation of OpenSSL's EVP_BytesToKey() function"
|
10
|
+
spec.description = 'The purpose of this gem is to make it easier to encrypt or decrypt data that has been encrypted by openssl'
|
11
|
+
spec.homepage = 'https://github.com/anothermh/evp_bytes_to_key'
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.0.0')
|
13
|
+
|
14
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
15
|
+
spec.metadata['source_code_uri'] = spec.homepage
|
16
|
+
spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/master/CHANGELOG.md"
|
17
|
+
|
18
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
spec.bindir = 'exe'
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ['lib']
|
24
|
+
|
25
|
+
spec.add_development_dependency 'pry', '~> 0.13.1'
|
26
|
+
spec.add_development_dependency 'rspec', '~> 3.9.0'
|
27
|
+
spec.add_development_dependency 'rubocop', '~> 0.88.0'
|
28
|
+
spec.add_development_dependency 'yard', '~> 0.9.25'
|
29
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'digest/md5'
|
4
|
+
|
5
|
+
module EvpBytesToKey
|
6
|
+
# This class is used to generate a new encryption key (and iv, if applicable) from a password in a way that
|
7
|
+
# is compatible with how the openssl command line utility generates keys and ivs. It emulates the logic of
|
8
|
+
# EVP_KeyToBytes from OpenSSL but is not a direct drop-in replacement because it does not support options
|
9
|
+
# like the number of rounds to use or the message digest algorithm to use.
|
10
|
+
class Key
|
11
|
+
# @return [String] the key or iv value in the named format
|
12
|
+
attr_accessor :key, :key_hex, :iv, :iv_hex
|
13
|
+
private :key=, :key_hex=, :iv=, :iv_hex=
|
14
|
+
|
15
|
+
# Generate a key from a given password. This key is identical to the key generated
|
16
|
+
# by EVP_KeyToBytes() in the openssl command-line utility.
|
17
|
+
#
|
18
|
+
# @param password [String] the password used for key generation
|
19
|
+
# @param salt [String, nil] the salt used for key generation
|
20
|
+
# @param bits [Integer] the bit length of the key, must be divisible by 8
|
21
|
+
# @param iv_length [Integer] the byte length of the IV
|
22
|
+
#
|
23
|
+
# @return [EvpBytesToKey::Key]
|
24
|
+
def initialize(password = nil, salt = nil, bits = nil, iv_length = nil)
|
25
|
+
@password = validate_password(password)
|
26
|
+
@salt = validate_salt(salt)
|
27
|
+
@bits = validate_bits(bits)
|
28
|
+
@iv_length = validate_iv_length(iv_length)
|
29
|
+
|
30
|
+
generate_key!
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
# @param password [String] the password that should be used for key derivation
|
36
|
+
# @raise [EvpBytesToKey::ArgumentError]
|
37
|
+
# @return [String]
|
38
|
+
def validate_password(password)
|
39
|
+
raise EvpBytesToKey::ArgumentError, 'password must be a String' unless password.is_a?(String)
|
40
|
+
|
41
|
+
password
|
42
|
+
end
|
43
|
+
|
44
|
+
# @param salt [String] the salt to append to the password that should be used for key derivation
|
45
|
+
# @raise [EvpBytesToKey::ArgumentError]
|
46
|
+
# @return [String, nil]
|
47
|
+
def validate_salt(salt)
|
48
|
+
if salt
|
49
|
+
raise EvpBytesToKey::ArgumentError, 'salt must be an 8 byte String' unless salt.is_a?(String) && salt.bytesize == 8
|
50
|
+
end
|
51
|
+
|
52
|
+
salt
|
53
|
+
end
|
54
|
+
|
55
|
+
# @param bits [Integer] the size of the key that should be returned in bits
|
56
|
+
# @raise [EvpBytesToKey::ArgumentError]
|
57
|
+
# @return [Integer]
|
58
|
+
def validate_bits(bits)
|
59
|
+
unless bits.is_a?(Integer) && bits >= 0 && (bits % 8).zero?
|
60
|
+
raise EvpBytesToKey::ArgumentError, 'bits must be a non-negative Integer evenly divisible by 8'
|
61
|
+
end
|
62
|
+
|
63
|
+
bits
|
64
|
+
end
|
65
|
+
|
66
|
+
# @param iv_length [Integer] the size of the iv that should be returned in bytes
|
67
|
+
# @raise [EvpBytesToKey::ArgumentError]
|
68
|
+
# @return [Integer]
|
69
|
+
def validate_iv_length(iv_length)
|
70
|
+
unless iv_length.is_a?(Integer) && iv_length >= 0 && (iv_length % 2).zero?
|
71
|
+
raise EvpBytesToKey::ArgumentError, 'iv_length must be an even Integer >= 0'
|
72
|
+
end
|
73
|
+
|
74
|
+
iv_length
|
75
|
+
end
|
76
|
+
|
77
|
+
# Generate a key from the supplied arguments by iteratively hashing the values and adding them
|
78
|
+
# to bytes until bytes has been fully populated
|
79
|
+
#
|
80
|
+
# @return [void]
|
81
|
+
def generate_key!
|
82
|
+
key_length = @bits / 8
|
83
|
+
last_hash = ''
|
84
|
+
bytes = ''
|
85
|
+
|
86
|
+
# Each iteration hashes the previous hash + password + salt to get the new hash, which is
|
87
|
+
# appended to the list of all hashes until it has enough bytes to constitute both the key
|
88
|
+
# and the iv
|
89
|
+
loop do
|
90
|
+
last_hash = Digest::MD5.digest(last_hash + @password + @salt.to_s)
|
91
|
+
bytes += last_hash
|
92
|
+
|
93
|
+
break if bytes.bytesize >= key_length + @iv_length.to_i
|
94
|
+
end
|
95
|
+
|
96
|
+
set_key_and_iv(bytes, key_length)
|
97
|
+
end
|
98
|
+
|
99
|
+
# @param bytes [String] the byte string from which the key and iv are extracted
|
100
|
+
# @param key_length [Integer] the length of the key in bytes
|
101
|
+
# @return [void]
|
102
|
+
def set_key_and_iv(bytes, key_length)
|
103
|
+
self.key = bytes.byteslice(0..key_length - 1)
|
104
|
+
# rubocop:disable Style/UnpackFirst
|
105
|
+
self.key_hex = key.unpack('H*').first
|
106
|
+
# rubocop:enable Style/UnpackFirst
|
107
|
+
|
108
|
+
return unless bytes.bytesize > key_length
|
109
|
+
|
110
|
+
self.iv = bytes.byteslice(key_length..key_length + @iv_length - 1)
|
111
|
+
# rubocop:disable Style/UnpackFirst
|
112
|
+
self.iv_hex = iv.unpack('H*').first
|
113
|
+
# rubocop:enable Style/UnpackFirst
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: evp_bytes_to_key
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- anothermh
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-07-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: pry
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.13.1
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.13.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.9.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.9.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rubocop
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.88.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.88.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: yard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.9.25
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.9.25
|
69
|
+
description: The purpose of this gem is to make it easier to encrypt or decrypt data
|
70
|
+
that has been encrypted by openssl
|
71
|
+
email:
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".rubocop.yml"
|
79
|
+
- ".travis.yml"
|
80
|
+
- CHANGELOG.md
|
81
|
+
- Gemfile
|
82
|
+
- Gemfile.lock
|
83
|
+
- LICENSE.txt
|
84
|
+
- README.md
|
85
|
+
- Rakefile
|
86
|
+
- bin/console
|
87
|
+
- bin/setup
|
88
|
+
- evp_bytes_to_key.gemspec
|
89
|
+
- lib/evp_bytes_to_key.rb
|
90
|
+
- lib/evp_bytes_to_key/error.rb
|
91
|
+
- lib/evp_bytes_to_key/key.rb
|
92
|
+
- lib/evp_bytes_to_key/version.rb
|
93
|
+
homepage: https://github.com/anothermh/evp_bytes_to_key
|
94
|
+
licenses: []
|
95
|
+
metadata:
|
96
|
+
homepage_uri: https://github.com/anothermh/evp_bytes_to_key
|
97
|
+
source_code_uri: https://github.com/anothermh/evp_bytes_to_key
|
98
|
+
changelog_uri: https://github.com/anothermh/evp_bytes_to_key/blob/master/CHANGELOG.md
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: 2.0.0
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubygems_version: 3.1.2
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: A pure Ruby implementation of OpenSSL's EVP_BytesToKey() function
|
118
|
+
test_files: []
|