nexaas-cipher 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 80abb5b25e4c0a338fb72a7966797517f41b28e991908515486642cd468cdce5
4
+ data.tar.gz: ec5f8180e713ff98e09da3e5561590f28a04b17a1942959d7708487d4d570304
5
+ SHA512:
6
+ metadata.gz: fdbe833f8488f886749104c36bd5d9a14cd4e02b40590d02d2cc6d27e592b3e5f3b1aac73fb1c6b63f0ccad6d2fd268ee6affaf54c21239ea71448a82bca93b7
7
+ data.tar.gz: 28e4c2203283229add96ec4eb575581cb11ea64ca61a6161e8516ab5a58accf08226bc8b0e804f8a6af9cf8111e358cb82c5921b8b8e93e3af5fd4e0044af08d
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nexaas-cipher.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,49 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ nexaas-cipher (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ byebug (10.0.2)
10
+ coderay (1.1.1)
11
+ diff-lcs (1.3)
12
+ method_source (0.8.2)
13
+ pry (0.10.4)
14
+ coderay (~> 1.1.0)
15
+ method_source (~> 0.8.1)
16
+ slop (~> 3.4)
17
+ pry-byebug (3.6.0)
18
+ byebug (~> 10.0)
19
+ pry (~> 0.10)
20
+ rake (12.3.1)
21
+ rdoc (6.0.4)
22
+ rspec (3.8.0)
23
+ rspec-core (~> 3.8.0)
24
+ rspec-expectations (~> 3.8.0)
25
+ rspec-mocks (~> 3.8.0)
26
+ rspec-core (3.8.0)
27
+ rspec-support (~> 3.8.0)
28
+ rspec-expectations (3.8.1)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.8.0)
31
+ rspec-mocks (3.8.0)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.8.0)
34
+ rspec-support (3.8.0)
35
+ slop (3.6.0)
36
+
37
+ PLATFORMS
38
+ ruby
39
+
40
+ DEPENDENCIES
41
+ bundler (~> 1.9)
42
+ nexaas-cipher!
43
+ pry-byebug (~> 3.6)
44
+ rake (~> 12.0)
45
+ rdoc (~> 6.0)
46
+ rspec (~> 3.6)
47
+
48
+ BUNDLED WITH
49
+ 1.17.1
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://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
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2013 Myfreecomm - Free Communities Consultoria Em Informatica Ltda
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,182 @@
1
+ # Nexaas Cipher (Ruby)
2
+
3
+ This is the official Ruby support for [Nexaas ID](https://id.nexaas.com) simple
4
+ ciphering.
5
+
6
+ `nexaas-cipher-ruby` RDoc documentation:
7
+ <http://rubydoc.info/github/myfreecomm/nexaas-cipher-ruby/frames/>
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application’s Gemfile:
12
+
13
+ gem 'nexaas-cipher', require: 'nexaas'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install nexaas-cipher
22
+
23
+ ## API
24
+
25
+ ### `Nexaas::Cipher::Salt`
26
+
27
+ This resource manages the cipher salt. The minimal instantiation is:
28
+
29
+ ```ruby
30
+ salt = Nexaas::Cipher::Salt.new(raw_salt_string)
31
+ ```
32
+
33
+ If the salt string is coded in [Base64][base64], you still can use it:
34
+
35
+ ```ruby
36
+ salt = Nexaas::Cipher::Salt.new(base64_salt_string, base64: true)
37
+ ```
38
+
39
+ #### Methods
40
+
41
+ `#salt?`
42
+ : returns false if the salt is empty, true otherwise.
43
+
44
+ `#salt`
45
+ : returns the raw salt.
46
+
47
+ `#salt(base64: true)`
48
+ : returns the salt encoded in Base64. This parâmeter is valid for all variations
49
+ of this method.
50
+
51
+ `#salt(code)`
52
+ : returns the salt sized to fit the supplied code.
53
+
54
+ `#salt(code, truncate: true)`
55
+ : same before, but truncates the salt when the code is smaller than it.
56
+
57
+ Example:
58
+
59
+ ```ruby
60
+ irb> salt = Nexaas::Cipher::Salt.new('YWJjZGVm', base64: true)
61
+ => #<Nexaas::Cipher::Salt:0x0000563848195c40 @salt="abcdef">
62
+ irb> salt.salt?
63
+ => true
64
+ irb> salt.salt
65
+ => "abcdef"
66
+ irb> salt.salt('test')
67
+ => "abcdef"
68
+ irb> salt.salt('test', truncate: true)
69
+ => "abcd"
70
+ irb> salt.salt('test', base64: true, truncate: true)
71
+ => "YWJjZA=="
72
+ ```
73
+
74
+ ### `Nexaas::Cipher::XorCrypter`
75
+
76
+ This class allows to perform [XOR cipher][xor-cipher] encryption.
77
+ Its constructor has the same signature as `Nexaas::Cipher::Salt`.
78
+
79
+ #### Methods
80
+
81
+ `#salt?`
82
+ : returns false if its salt is empty, true otherwise.
83
+
84
+ `#salt`
85
+ : returns its internal `Nexaas::Cipher::Salt` object.
86
+
87
+ `#encrypt(code, **options)`
88
+ : encrypts the code using the salt. The `options` are passed to the `salt#salt`
89
+ method to get the effective salt string. It accepts an extra option,
90
+ `obfuscate`, that generates a random obfuscative string to be concatenated to
91
+ the code in order to fill the salt length when the code is smaller and is not
92
+ truncated, avoiding to expose it.
93
+
94
+ `#decrypt(code, **options)`
95
+ : does the same of `#encrypt`, but truncates the result code in the firt null
96
+ character. Useful when the cipher was generate with `obfuscate: true`.
97
+
98
+ ### `Nexaas::Cipher::IDCrypter`
99
+
100
+ This is the same crypter used by Nexaas ID to deal with application signatures.
101
+
102
+ It expects the code/cipher to be the
103
+ [application secret][nexaas-id-application-data].
104
+
105
+ Features:
106
+
107
+ - The salt is always truncated and never obfuscated
108
+ (makes no sense on truncation).
109
+ - On encryption, the code is expected to be Base64 by default,
110
+ and the generated cipher is **always** Base64.
111
+ - On decryption, the cipher **must** be Base64,
112
+ and the determinated code is Base64 by default.
113
+
114
+ #### Methods
115
+
116
+ `#encrypt(code, base64: true)`
117
+ : encrypt `code`. The `base64` controls if the `code` is Base64 or not (raw).
118
+
119
+ `#decrypt(code, base64: true)`
120
+ : decrypt `code`. The `base64` controls if the output is Base64 or not (raw).
121
+
122
+ Using the default options, `#encrypt` and `decrypt` act the same:
123
+ expecting a Base64 code and _xoring_ it with the salt.
124
+ Since the XOR cipher is self-reversing,
125
+ “encrypting” the generated cipher results in the original code.
126
+
127
+ #### Example
128
+
129
+ ```ruby
130
+ crypter = Nexaas::Cipher::IDCrypter.new(application.secret)
131
+
132
+ signature = Base64.encode64(Random.urandom(12)).rstrip
133
+ cipher = crypter.encrypt(signature)
134
+
135
+ # Send cipher to Nexaas ID using the access token, and getting res(ponse) back
136
+ # ...
137
+
138
+ if res.params[:signature] == signature
139
+ puts 'OK, it is right!'
140
+ # ...
141
+ end
142
+ ```
143
+
144
+ ### `Nexaas::Cipher::CrypterBase`
145
+
146
+ Pretending to be an abstract class, it is used to inherit new crypters.
147
+
148
+ The constructor passes its parameters to the `Nexaas::Cipher::Salt` one,
149
+ and stores its instance for use.
150
+
151
+ The subclasses supposed to implement the following protected methods:
152
+
153
+ `#do_encrypt(code, **options)`
154
+ : The routine to be performed for encryption.
155
+
156
+ `#do_decrypt(code, **options)`
157
+ : The routine to be performed for decryption.
158
+
159
+ Notes:
160
+
161
+ - The `code` is passed raw.
162
+ - The `options` are the same passed to `#encrypt` and `#decrypt` methods.
163
+ - The `base64:` options is **not** passed.
164
+ If it’s false (default), the code or cipher is passed raw through and back;
165
+ if it’s true, the supplied code is decoded from Base64 to raw before being
166
+ passed to the `#do_*` method, and de returned valueis encoded in Base64.
167
+
168
+ #### Available methods
169
+
170
+ `#salt?`
171
+ : returns false if the salt is empty, true otherwise.
172
+
173
+ `#salt`
174
+ : returns the internal `Nexaas::Cipher::Salt` object.
175
+
176
+ ## Support
177
+
178
+ This gem supports Ruby 2.5 or superior.
179
+
180
+ [base64]: https://en.wikipedia.org/wiki/Base64
181
+ [nexaas-id-application-data]: https://docs.id.nexaas.com/add-app#application-data
182
+ [xor-cipher]: https://www.geeksforgeeks.org/xor-cipher/
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rdoc/task'
4
+ Rake::RDocTask.new do |rdoc|
5
+ rdoc.title = 'nexaas-cipher'
6
+ rdoc.main = 'README.rdoc'
7
+ rdoc.rdoc_dir = 'doc'
8
+ rdoc.rdoc_files.include('README.rdoc', 'lib/**/*.rb')
9
+ # rdoc.generator = 'darkfish'
10
+ end
11
+
12
+ require 'rspec/core/rake_task'
13
+ RSpec::Core::RakeTask.new(:spec)
14
+
15
+ task test: :spec
16
+ task default: :spec
data/config.reek ADDED
@@ -0,0 +1,12 @@
1
+ DataClump:
2
+ enabled: false
3
+
4
+ NilCheck:
5
+ enabled: false
6
+
7
+ UncommunicativeParameterName:
8
+ enabled: false
9
+
10
+ exclude_paths:
11
+ - spec
12
+ - vendor
data/lib/nexaas.rb ADDED
@@ -0,0 +1,3 @@
1
+ # Nexaas: needed by Cipher submodule
2
+ module Nexaas
3
+ end
@@ -0,0 +1,7 @@
1
+ module Nexaas
2
+ # Cipher: containing some ciphering and crypting facilities
3
+ module Cipher
4
+ end
5
+ end
6
+
7
+ require 'nexaas/cipher/salt'
@@ -0,0 +1,62 @@
1
+ require 'nexaas/cipher/salt'
2
+
3
+ module Nexaas
4
+ module Cipher
5
+ # CrypterBase: abstract class to support crypter classes
6
+ class CrypterBase
7
+ attr_reader :salt
8
+
9
+ def initialize(*args)
10
+ @salt = Salt.new(*args)
11
+ end
12
+
13
+ def encrypt(code, **options)
14
+ guarded_crypt(code, :do_encrypt, **options)
15
+ end
16
+
17
+ def decrypt(code, **options)
18
+ guarded_crypt(code, :do_decrypt, **options)
19
+ end
20
+
21
+ def salt?
22
+ salt.salt?
23
+ end
24
+
25
+ protected
26
+
27
+ def guarded_crypt(code, method, **options)
28
+ return if code.nil? || code.empty?
29
+ return code unless salt.salt?
30
+ start_encrypting(code, method, **options)
31
+ end
32
+
33
+ # base64 :reek:ControlParameter
34
+ # base64 :reek:BooleanParameter
35
+ # :reek:LongParameterList
36
+ def start_encrypting(code, method, base64: false, **options)
37
+ if base64
38
+ b64_encrypt(code, method, **options)
39
+ else
40
+ do_crypt(code, method, **options)
41
+ end
42
+ end
43
+
44
+ def b64_encrypt(code, method, **options)
45
+ Base64.
46
+ encode64(do_crypt(Base64.decode64(code), method, **options)).
47
+ rstrip
48
+ end
49
+
50
+ def do_crypt(code, method, **options)
51
+ case method
52
+ when :do_encrypt
53
+ do_encrypt(code, **options)
54
+ when :do_decrypt
55
+ do_decrypt(code, **options)
56
+ else
57
+ raise ArgumentError(method)
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,27 @@
1
+ require 'nexaas/cipher/xor_crypter'
2
+
3
+ module Nexaas
4
+ module Cipher
5
+ # IDCrypter: XOR cipher as used by Nexaas ID
6
+ class IDCrypter < XorCrypter
7
+ # base64 :reek:BooleanParameter
8
+ # base64 :reek:ControlParameter
9
+ def encrypt(code, base64: true)
10
+ return if code.nil? || code.empty?
11
+ code = Base64.encode64(code).rstrip unless base64
12
+ if salt?
13
+ super(code, base64: true, obfuscate: false, truncate: true)
14
+ else
15
+ code
16
+ end
17
+ end
18
+
19
+ # base64 :reek:BooleanParameter
20
+ # base64 :reek:ControlParameter
21
+ def decrypt(code, base64: true)
22
+ res = encrypt(code)
23
+ base64 || res.nil? ? res : Base64.decode64(res)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,36 @@
1
+ require 'base64'
2
+
3
+ module Nexaas
4
+ module Cipher
5
+ # Salt: represents the disturber salt for ciphering
6
+ # :reek:BooleanParameter
7
+ # :reek:ControlParameter
8
+ class Salt
9
+ def initialize(salt, base64: false)
10
+ @salt = base64 ? Base64.decode64(salt) : salt
11
+ end
12
+
13
+ def salt(code = nil, base64: false, truncate: false)
14
+ return Base64.encode64(salt(code, truncate: truncate)).rstrip if base64
15
+ return @salt unless code
16
+ return "\0" * code.length if @salt.empty?
17
+ dup_salt(code, @salt, truncate)
18
+ end
19
+
20
+ def salt?
21
+ !@salt.empty?
22
+ end
23
+
24
+ private
25
+
26
+ # current :reek:FeatureEnvy
27
+ # current.length :reek:DuplicateMethodCall
28
+ def dup_salt(code, current, truncate)
29
+ expected_length = code.length
30
+ expected_length = [current.length, expected_length].max unless truncate
31
+ current <<= @salt while current.length < expected_length
32
+ current[0, expected_length]
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ module Nexaas
2
+ module Cipher
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
@@ -0,0 +1,37 @@
1
+ require 'nexaas/cipher/crypter_base'
2
+
3
+ module Nexaas
4
+ module Cipher
5
+ # XorCrypter: allows perform XOR cipher
6
+ class XorCrypter < CrypterBase
7
+ protected
8
+
9
+ # obfuscate :reek:BooleanParameter
10
+ def do_encrypt(code, obfuscate: false, **options)
11
+ self.class.xor_encrypt(salt.salt(code, **options), code, obfuscate)
12
+ end
13
+
14
+ def do_decrypt(code, **options)
15
+ do_encrypt(code, options).split("\0").first
16
+ end
17
+
18
+ class << self
19
+ # obfuscate :reek:ControlParameter
20
+ def xor_encrypt(key, code, obfuscate)
21
+ xor(obfuscate ? obfuscate_key(key, code) : code, key)
22
+ end
23
+
24
+ def xor(code, key)
25
+ key.unpack('C*').zip(code.unpack('C*')).
26
+ map { |fst, lst| fst ^ (lst || 0) }.
27
+ pack('C*')
28
+ end
29
+
30
+ def obfuscate_key(key, code)
31
+ len = key.length
32
+ (code + "\0" + Array(0..255).sample(len).pack('C*'))[0, len]
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,32 @@
1
+ require 'English'
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ # Ensure we require the local version and not one we might have installed already
7
+ require File.join([File.dirname(__FILE__), 'lib', 'nexaas', 'cipher', 'version.rb'])
8
+
9
+ Gem::Specification.new do |spec|
10
+ spec.name = 'nexaas-cipher'
11
+ spec.version = Nexaas::Cipher::VERSION
12
+ spec.authors = ['Rodrigo Cacilhas']
13
+ spec.email = ['rodrigo.cacilhas@nexaas.com']
14
+ spec.description = 'A Ruby client for the Nexaas ID REST API'
15
+ spec.summary = 'A Ruby client for the Nexaas ID REST API: https://id.nexaas.com/static/docs/'
16
+ spec.homepage = 'https://github.com/myfreecomm/nexaas-cipher-ruby'
17
+ spec.license = 'Apache-2.0'
18
+ spec.has_rdoc = true
19
+
20
+ spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
23
+ spec.require_paths = %w[lib]
24
+
25
+ # spec.add_dependency 'multi_json', '~> 1.11'
26
+
27
+ spec.add_development_dependency 'bundler', '~> 1.9'
28
+ spec.add_development_dependency "pry-byebug", "~> 3.6"
29
+ spec.add_development_dependency 'rake', '~> 12.0'
30
+ spec.add_development_dependency 'rdoc', '~> 6.0'
31
+ spec.add_development_dependency 'rspec', '~> 3.6'
32
+ end
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+ require 'nexaas/cipher/id_crypter'
3
+
4
+ describe Nexaas::Cipher::IDCrypter do
5
+ context 'normal behavior without salt' do
6
+ let(:crypter) { described_class.new('') }
7
+
8
+ describe '#salt' do
9
+ it { expect(crypter.salt?).to be(false) }
10
+ it { expect(crypter.salt.salt).to be_empty }
11
+ end
12
+
13
+ describe '#encrypt' do
14
+ it { expect(crypter.encrypt(nil)).to be(nil) }
15
+ it { expect(crypter.encrypt('dGVzdA==')).to eq('dGVzdA==') }
16
+ end
17
+
18
+ describe '#decrypt' do
19
+ it { expect(crypter.decrypt(nil)).to be(nil) }
20
+ it { expect(crypter.decrypt('dGVzdA==')).to eq('dGVzdA==') }
21
+ end
22
+ end
23
+
24
+ context 'normal behavior with salt' do
25
+ let(:crypter) { described_class.new(salt) }
26
+ let(:salt) { 'iIl7wbZsP7Gd2WphNoo/64sdhwwrzEBh' }
27
+
28
+ describe '#salt' do
29
+ it { expect(crypter.salt?).to be(true) }
30
+ it { expect(crypter.salt.salt).to eq(salt) }
31
+ end
32
+
33
+ describe '#encrypt' do
34
+ it { expect(crypter.encrypt(nil)).to be(nil) }
35
+ it { expect(crypter.encrypt('dGVzdA==')).to eq('HSwfQw==') }
36
+
37
+ it 'should not obfuscate the salt' do
38
+ expect(crypter.encrypt('dGVzdDE=')).to eq(crypter.encrypt('dGVzdDE='))
39
+ end
40
+ end
41
+
42
+ describe '#decrypt' do
43
+ it { expect(crypter.decrypt(nil)).to be(nil) }
44
+ it { expect(crypter.decrypt('dGVzdA==')).to eq('HSwfQw==') }
45
+
46
+ it 'should not obfuscate the salt' do
47
+ expect(crypter.decrypt('dGVzdDE=')).to eq(crypter.decrypt('dGVzdDE='))
48
+ end
49
+ end
50
+ end
51
+
52
+ context 'altered behavior without salt' do
53
+ let(:crypter) { described_class.new('') }
54
+
55
+ describe '#encrypt' do
56
+ it { expect(crypter.encrypt(nil, base64: false)).to be(nil) }
57
+ it { expect(crypter.encrypt('test', base64: false)).to eq('dGVzdA==') }
58
+ end
59
+
60
+ describe '#decrypt' do
61
+ it { expect(crypter.decrypt(nil, base64: false)).to be(nil) }
62
+ it { expect(crypter.decrypt('dGVzdA==', base64: false)).to eq('test') }
63
+ end
64
+ end
65
+
66
+ context 'altered behavior with salt' do
67
+ let(:crypter) { described_class.new(salt) }
68
+ let(:salt) { 'iIl7wbZsP7Gd2WphNoo/64sdhwwrzEBh' }
69
+
70
+ describe '#encrypt' do
71
+ it { expect(crypter.encrypt(nil, base64: false)).to be(nil) }
72
+ it { expect(crypter.encrypt('test', base64: false)).to eq('HSwfQw==') }
73
+
74
+ it 'should not obfuscate the salt' do
75
+ expect(crypter.encrypt('test1', base64: false)).
76
+ to eq(crypter.encrypt('test1', base64: false))
77
+ end
78
+ end
79
+
80
+ describe '#decrypt' do
81
+ it { expect(crypter.decrypt(nil, base64: false)).to be(nil) }
82
+ it { expect(crypter.decrypt('HSwfQw==', base64: false)).to eq('test') }
83
+
84
+ it 'should not obfuscate the salt' do
85
+ expect(crypter.decrypt('dGVzdDE=', base64: false)).
86
+ to eq(crypter.decrypt('dGVzdDE=', base64: false))
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe Nexaas::Cipher::Salt do
4
+ context 'empty salt' do
5
+ subject { described_class.new('') }
6
+
7
+ it { expect(subject.salt?).to be(false) }
8
+ it { expect(subject.salt).to be_empty }
9
+ it { expect(subject.salt('test')).to eq("\0\0\0\0") }
10
+ it { expect(subject.salt('test', base64: true)).to eq('AAAAAA==') }
11
+ end
12
+
13
+ context 'non-coded' do
14
+ subject { described_class.new('abc') }
15
+
16
+ it { expect(subject.salt?).to be(true) }
17
+ it { expect(subject.salt).to eq('abc') }
18
+
19
+ context 'full length' do
20
+ it { expect(subject.salt('te')).to eq('abc') }
21
+ it { expect(subject.salt('tes')).to eq('abc') }
22
+ it { expect(subject.salt('testtest')).to eq('abcabcab') }
23
+ it { expect(subject.salt('testtest', base64: true)).to eq('YWJjYWJjYWI=') }
24
+ end
25
+
26
+ context 'truncated' do
27
+ it { expect(subject.salt('te', truncate: true)).to eq('ab') }
28
+ it { expect(subject.salt('tes', truncate: true)).to eq('abc') }
29
+ it { expect(subject.salt('testtest', truncate: true)).to eq('abcabcab') }
30
+ it { expect(subject.salt('te', base64: true, truncate: true)).to eq('YWI=') }
31
+ end
32
+
33
+ context 'coded' do
34
+ subject { described_class.new('ZGVm', base64: true) }
35
+
36
+ it { expect(subject.salt?).to be(true) }
37
+ it { expect(subject.salt).to eq('def') }
38
+
39
+ context 'full length' do
40
+ it { expect(subject.salt('te')).to eq('def') }
41
+ it { expect(subject.salt('tes')).to eq('def') }
42
+ it { expect(subject.salt('testtest')).to eq('defdefde') }
43
+ it { expect(subject.salt('testtest', base64: true)).to eq('ZGVmZGVmZGU=') }
44
+ end
45
+
46
+ context 'truncated' do
47
+ it { expect(subject.salt('te', truncate: true)).to eq('de') }
48
+ it { expect(subject.salt('tes', truncate: true)).to eq('def') }
49
+ it { expect(subject.salt('testtest', truncate: true)).to eq('defdefde') }
50
+ it { expect(subject.salt('te', base64: true, truncate: true)).to eq('ZGU=') }
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+ require 'nexaas/cipher/xor_crypter'
3
+
4
+ describe Nexaas::Cipher::XorCrypter do
5
+ let(:crypter) { described_class.new(salt, base64: true) }
6
+
7
+ context 'without salt' do
8
+ let(:salt) { '' }
9
+
10
+ it { expect(crypter.salt?).to be(false) }
11
+ it { expect(crypter.encrypt(nil)).to be(nil) }
12
+ it { expect(crypter.encrypt('test')).to eq('test') }
13
+ it { expect(crypter.encrypt('test', trucate: true)).to eq('test') }
14
+ it { expect(crypter.encrypt('test', ofuscate: true)).to eq('test') }
15
+ it { expect(crypter.encrypt('AA==', base64: true)).to eq('AA==') }
16
+ end
17
+
18
+ context 'with salt' do
19
+ let(:salt) { '0LhN0KgMsNksgZQmfYCjweg68cRxCr9086IzcqM1f6w=' }
20
+
21
+ describe '#salt' do
22
+ it { expect(crypter.salt?).to be(true) }
23
+ it { expect(crypter.salt.salt(base64: true)).to eq(salt) }
24
+ end
25
+
26
+ describe '#encrypt' do
27
+ it { expect(crypter.encrypt(nil)).to be(nil) }
28
+
29
+ it 'should truncate' do
30
+ expect(crypter.encrypt('test1', truncate: true)).
31
+ to eq("\xa4\xdd>\xa4\x99".force_encoding('ASCII-8BIT'))
32
+ end
33
+
34
+ it 'should not obfuscate the salt' do
35
+ expect(crypter.encrypt('test1')).to eq(crypter.encrypt('test1'))
36
+ end
37
+
38
+ it 'should obfuscate the salt' do
39
+ expect(crypter.encrypt('test1', obfuscate: true)).
40
+ not_to eq(crypter.encrypt('test1', obfuscate: true))
41
+ end
42
+ end
43
+
44
+ describe '#decrypt' do
45
+ it { expect(crypter.decrypt(crypter.encrypt('test1'))).to eq('test1') }
46
+ it { expect(crypter.decrypt(crypter.encrypt('test1', truncate: true))).to match(/\Atest1/) }
47
+ it { expect(crypter.decrypt(crypter.encrypt('test1', obfuscate: true))).to eq('test1') }
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,7 @@
1
+ require 'nexaas/cipher'
2
+ require 'pry'
3
+
4
+ RSpec.configure do |c|
5
+ c.mock_with :rspec
6
+ c.example_status_persistence_file_path = '.rspec_persistence'
7
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nexaas-cipher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rodrigo Cacilhas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry-byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rdoc
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '6.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '6.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.6'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.6'
83
+ description: A Ruby client for the Nexaas ID REST API
84
+ email:
85
+ - rodrigo.cacilhas@nexaas.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rubocop.yml"
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - LICENSE
95
+ - README.md
96
+ - Rakefile
97
+ - config.reek
98
+ - lib/nexaas.rb
99
+ - lib/nexaas/cipher.rb
100
+ - lib/nexaas/cipher/crypter_base.rb
101
+ - lib/nexaas/cipher/id_crypter.rb
102
+ - lib/nexaas/cipher/salt.rb
103
+ - lib/nexaas/cipher/version.rb
104
+ - lib/nexaas/cipher/xor_crypter.rb
105
+ - nexaas-cipher.gemspec
106
+ - spec/nexaas/cipher/id_crypter_spec.rb
107
+ - spec/nexaas/cipher/salt_spec.rb
108
+ - spec/nexaas/cipher/xor_crypter_spec.rb
109
+ - spec/spec_helper.rb
110
+ homepage: https://github.com/myfreecomm/nexaas-cipher-ruby
111
+ licenses:
112
+ - Apache-2.0
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.7.6
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: 'A Ruby client for the Nexaas ID REST API: https://id.nexaas.com/static/docs/'
134
+ test_files:
135
+ - spec/nexaas/cipher/id_crypter_spec.rb
136
+ - spec/nexaas/cipher/salt_spec.rb
137
+ - spec/nexaas/cipher/xor_crypter_spec.rb
138
+ - spec/spec_helper.rb