parsel 0.3.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +10 -0
- data/Gemfile +1 -1
- data/README.md +16 -7
- data/Rakefile +9 -4
- data/lib/parsel.rb +23 -10
- data/lib/parsel/json.rb +2 -0
- data/lib/parsel/marshal.rb +2 -0
- data/lib/parsel/version.rb +2 -2
- data/parsel.gemspec +11 -10
- data/test/parsel/json_test.rb +18 -0
- data/test/parsel/marshal_test.rb +18 -0
- data/test/parsel/parsel_test.rb +40 -0
- data/test/test_helper.rb +12 -0
- metadata +28 -15
- data/.rspec +0 -1
- data/spec/parsel/json_spec.rb +0 -18
- data/spec/parsel/marshal_spec.rb +0 -18
- data/spec/parsel_spec.rb +0 -40
- data/spec/spec_helper.rb +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ab6c3def75bb0d360e2e50ba25ad94625801dcb7fdbb9ad5040e8030d0a58ee5
|
4
|
+
data.tar.gz: aa17d8b8c3ea92c7357c23a6637cacaaa977c5295c9f36a79fbf1deb7742098d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 50fa30534889c969e533319572a1dcdc3cacbe9e7acf9fba92965e65b4be09350963e135ff29e3a4ff16be4b581db38b88de05fbe71162f205bec796229a76eb
|
7
|
+
data.tar.gz: 47cd6019ff5155c646ac1e669d145815c7e997cd48bccf8d718516190f1a8ab611b1a5542ba680d6145830866f590903bd23dea00e538019120bb325a5a2b026
|
data/.travis.yml
ADDED
data/Gemfile
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
source
|
1
|
+
source "https://rubygems.org"
|
2
2
|
gemspec
|
data/README.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
# parsel
|
2
2
|
|
3
|
+
[![Code Climate](https://codeclimate.com/github/fnando/parsel-rb.png)](https://codeclimate.com/github/fnando/parsel-rb)
|
4
|
+
[![Build Status](https://travis-ci.org/fnando/parsel-rb.svg)](https://travis-ci.org/fnando/parsel-rb)
|
5
|
+
[![Gem](https://img.shields.io/gem/v/parsel.svg)](https://rubygems.org/gems/parsel)
|
6
|
+
[![Gem](https://img.shields.io/gem/dt/parsel.svg)](https://rubygems.org/gems/parsel)
|
7
|
+
|
3
8
|
Encrypt and decrypt data with a given key.
|
4
9
|
|
5
10
|
This library was created to allow an easy data
|
@@ -14,21 +19,21 @@ Check it out the Node.js counter-part: <http://github.com/fnando/parsel-js>.
|
|
14
19
|
## Usage
|
15
20
|
|
16
21
|
```ruby
|
17
|
-
require
|
22
|
+
require "parsel"
|
18
23
|
|
19
|
-
secret_key =
|
20
|
-
encrypted = Parsel.encrypt(secret_key,
|
24
|
+
secret_key = "mysupersecretkeythatnobodyknowsabout"
|
25
|
+
encrypted = Parsel.encrypt(secret_key, "hello from ruby!")
|
21
26
|
decrypted = Parsel.decrypt(secret_key, encrypted)
|
22
27
|
```
|
23
28
|
|
24
29
|
You can also use Marshal for encrypting/decrypting objects. Notice that this isn't supported by Node.js.
|
25
30
|
|
26
31
|
```ruby
|
27
|
-
require
|
32
|
+
require "parsel"
|
28
33
|
|
29
34
|
data = {user_id: 1234}
|
30
35
|
|
31
|
-
secret_key =
|
36
|
+
secret_key = "mysupersecretkeythatnobodyknowsabout"
|
32
37
|
encrypted = Parsel::Marshal.encrypt(secret_key, data)
|
33
38
|
|
34
39
|
decrypted = Parsel::Marshal.decrypt(secret_key, encrypted)
|
@@ -38,11 +43,11 @@ decrypted = Parsel::Marshal.decrypt(secret_key, encrypted)
|
|
38
43
|
Alternatively you can use `JSON` as your serializer.
|
39
44
|
|
40
45
|
```ruby
|
41
|
-
require
|
46
|
+
require "parsel"
|
42
47
|
|
43
48
|
data = {user_id: 1234}
|
44
49
|
|
45
|
-
secret_key =
|
50
|
+
secret_key = "mysupersecretkeythatnobodyknowsabout"
|
46
51
|
encrypted = Parsel::JSON.encrypt(secret_key, data)
|
47
52
|
|
48
53
|
decrypted = Parsel::JSON.decrypt(secret_key, encrypted)
|
@@ -51,6 +56,8 @@ decrypted = Parsel::JSON.decrypt(secret_key, encrypted)
|
|
51
56
|
|
52
57
|
### Custom Cipher IV
|
53
58
|
|
59
|
+
![Make sure you use a 16-chars long if you're going to exchange data with parsel-js.](http://messages.hellobits.com/warning.svg?message=Make%20sure%20you%20use%20a%2016-chars%20long%20if%20you're%20going%20to%20exchange%20data%20with%20parsel-js.)
|
60
|
+
|
54
61
|
By default, this library uses a built-in IV. You may want to change that.
|
55
62
|
|
56
63
|
You can globally change the IV like the following:
|
@@ -66,6 +73,8 @@ Parsel.encrypt(secret_key, iv, data)
|
|
66
73
|
Parsel.decrypt(secret_key, iv, data)
|
67
74
|
```
|
68
75
|
|
76
|
+
**NOTE:** If you're going to use <http://github.com/fnando/parsel-js> to exchange data, you have to use a 16-chars long IV. Node.js will fail with different lengths.
|
77
|
+
|
69
78
|
## Maintainer
|
70
79
|
|
71
80
|
- Nando Vieira (<http://nandovieira.com.br>)
|
data/Rakefile
CHANGED
@@ -1,5 +1,10 @@
|
|
1
|
-
require "bundler"
|
2
|
-
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rake/testtask"
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
Rake::TestTask.new(:test) do |t|
|
5
|
+
t.libs << "test"
|
6
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
7
|
+
t.warning = false
|
8
|
+
end
|
9
|
+
|
10
|
+
task default: :test
|
data/lib/parsel.rb
CHANGED
@@ -1,13 +1,22 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
1
|
+
require "securerandom"
|
2
|
+
require "openssl"
|
3
|
+
require "base64"
|
4
|
+
require "json"
|
5
|
+
require "parsel/marshal"
|
6
|
+
require "parsel/json"
|
7
|
+
require "parsel/version"
|
7
8
|
|
8
9
|
module Parsel
|
9
|
-
DEFAULT_IV =
|
10
|
-
CIPHER =
|
10
|
+
DEFAULT_IV = "f89209ffcdd1a225".freeze
|
11
|
+
CIPHER = "AES-256-CBC".freeze
|
12
|
+
|
13
|
+
def self.print_deprecation_message
|
14
|
+
warn <<~TEXT
|
15
|
+
`parsel` is no longer supported. Use at your own risk.
|
16
|
+
Called from #{caller[1]}
|
17
|
+
|
18
|
+
TEXT
|
19
|
+
end
|
11
20
|
|
12
21
|
def self.default_iv=(iv)
|
13
22
|
@default_iv = iv
|
@@ -20,10 +29,12 @@ module Parsel
|
|
20
29
|
self.default_iv = DEFAULT_IV
|
21
30
|
|
22
31
|
def self.encrypt(*args)
|
32
|
+
Parsel.print_deprecation_message
|
23
33
|
encode cipher(:encrypt, *expand_args(args))
|
24
34
|
end
|
25
35
|
|
26
36
|
def self.decrypt(*args)
|
37
|
+
Parsel.print_deprecation_message
|
27
38
|
key, iv, data = expand_args(args)
|
28
39
|
cipher(:decrypt, key, iv, decode(data))
|
29
40
|
rescue Exception
|
@@ -45,15 +56,17 @@ module Parsel
|
|
45
56
|
def self.cipher(mode, key, iv, data)
|
46
57
|
cipher = OpenSSL::Cipher.new(CIPHER).public_send(mode)
|
47
58
|
cipher.key = Digest::SHA256.digest(key)
|
48
|
-
cipher.iv = iv
|
59
|
+
cipher.iv = iv[0...cipher.iv_len]
|
49
60
|
cipher.update(data) + cipher.final
|
50
61
|
end
|
51
62
|
|
52
63
|
def self.encode(data)
|
53
|
-
Base64.encode64(data).gsub(/\n/,
|
64
|
+
Base64.encode64(data).gsub(/\n/, "")
|
54
65
|
end
|
55
66
|
|
56
67
|
def self.decode(data)
|
57
68
|
Base64.decode64(data)
|
58
69
|
end
|
59
70
|
end
|
71
|
+
|
72
|
+
Parsel.print_deprecation_message
|
data/lib/parsel/json.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
module Parsel
|
2
2
|
module JSON
|
3
3
|
def self.encrypt(*args)
|
4
|
+
Parsel.print_deprecation_message
|
4
5
|
key, iv, data = Parsel.expand_args(args)
|
5
6
|
Parsel.encrypt(key, iv, ::JSON.dump(data))
|
6
7
|
end
|
7
8
|
|
8
9
|
def self.decrypt(*args)
|
10
|
+
Parsel.print_deprecation_message
|
9
11
|
key, iv, data = Parsel.expand_args(args)
|
10
12
|
::JSON.load Parsel.decrypt(key, iv, data)
|
11
13
|
rescue Exception
|
data/lib/parsel/marshal.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
module Parsel
|
2
2
|
module Marshal
|
3
3
|
def self.encrypt(*args)
|
4
|
+
Parsel.print_deprecation_message
|
4
5
|
key, iv, data = Parsel.expand_args(args)
|
5
6
|
Parsel.encrypt(key, iv, ::Marshal.dump(data))
|
6
7
|
end
|
7
8
|
|
8
9
|
def self.decrypt(*args)
|
10
|
+
Parsel.print_deprecation_message
|
9
11
|
key, iv, data = Parsel.expand_args(args)
|
10
12
|
::Marshal.load Parsel.decrypt(key, iv, data)
|
11
13
|
rescue Exception
|
data/lib/parsel/version.rb
CHANGED
data/parsel.gemspec
CHANGED
@@ -1,21 +1,22 @@
|
|
1
|
-
|
2
|
-
require './lib/parsel/version'
|
1
|
+
require "./lib/parsel/version"
|
3
2
|
|
4
3
|
Gem::Specification.new do |s|
|
5
|
-
s.name =
|
4
|
+
s.name = "parsel"
|
6
5
|
s.version = Parsel::Version::STRING
|
7
6
|
s.platform = Gem::Platform::RUBY
|
8
|
-
s.authors = [
|
9
|
-
s.email = [
|
10
|
-
s.homepage =
|
11
|
-
s.summary =
|
7
|
+
s.authors = ["Nando Vieira"]
|
8
|
+
s.email = ["fnando.vieira@gmail.com"]
|
9
|
+
s.homepage = "http://rubygems.org/gems/parsel"
|
10
|
+
s.summary = "Encrypt and decrypt data with a given key."
|
12
11
|
s.description = s.summary
|
12
|
+
s.post_install_message = "`parsel` is no longer supported. Use at your own risk."
|
13
13
|
|
14
14
|
s.files = `git ls-files`.split("\n")
|
15
15
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map {|f| File.basename(f) }
|
17
17
|
s.require_paths = ['lib']
|
18
18
|
|
19
|
-
s.add_development_dependency
|
20
|
-
s.add_development_dependency
|
19
|
+
s.add_development_dependency "rake"
|
20
|
+
s.add_development_dependency "minitest-utils"
|
21
|
+
s.add_development_dependency "pry-meta"
|
21
22
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class JsonTest < Minitest::Test
|
4
|
+
let(:key) { "my secret key" }
|
5
|
+
|
6
|
+
test "encrypts data" do
|
7
|
+
refute_equal JSON.dump(["hello"]), Parsel::JSON.encrypt(key, ["hello"])
|
8
|
+
end
|
9
|
+
|
10
|
+
test "decrypts data" do
|
11
|
+
encrypted = Parsel::JSON.encrypt(key, ["hello"])
|
12
|
+
assert_equal ["hello"], Parsel::JSON.decrypt(key, encrypted)
|
13
|
+
end
|
14
|
+
|
15
|
+
test "returns false when decryption fails" do
|
16
|
+
refute Parsel::JSON.decrypt("abc", "123")
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class MarshalTest < Minitest::Test
|
4
|
+
let(:key) { "my secret key" }
|
5
|
+
|
6
|
+
test "encrypts data" do
|
7
|
+
refute_equal Marshal.dump("hello"), Parsel::Marshal.encrypt(key, "hello")
|
8
|
+
end
|
9
|
+
|
10
|
+
test "decrypts data" do
|
11
|
+
encrypted = Parsel::Marshal.encrypt(key, "hello")
|
12
|
+
assert_equal "hello", Parsel::Marshal.decrypt(key, encrypted)
|
13
|
+
end
|
14
|
+
|
15
|
+
test "returns false when decryption fails" do
|
16
|
+
refute Parsel::Marshal.decrypt("abc", "123")
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class ParselTest < Minitest::Test
|
4
|
+
let(:key) { "my secret key" }
|
5
|
+
|
6
|
+
test "encrypts data" do
|
7
|
+
refute_equal "hello", Parsel.encrypt(key, "hello")
|
8
|
+
end
|
9
|
+
|
10
|
+
test "decrypts data" do
|
11
|
+
encrypted = Parsel.encrypt(key, "hello")
|
12
|
+
assert_equal "hello", Parsel.decrypt(key, encrypted)
|
13
|
+
end
|
14
|
+
|
15
|
+
test "returns false when decryption fails" do
|
16
|
+
refute Parsel.decrypt("abc", "123")
|
17
|
+
end
|
18
|
+
|
19
|
+
test "uses custom iv" do
|
20
|
+
iv = SecureRandom.hex(100)
|
21
|
+
encrypted = Parsel.encrypt(key, iv, "hello")
|
22
|
+
|
23
|
+
assert_equal "hello", Parsel.decrypt(key, iv, encrypted)
|
24
|
+
end
|
25
|
+
|
26
|
+
test "return false when decryption for custom iv fails" do
|
27
|
+
iv = SecureRandom.hex(100)
|
28
|
+
encrypted = Parsel.encrypt(key, iv, "hello")
|
29
|
+
|
30
|
+
refute Parsel.decrypt(key, encrypted)
|
31
|
+
end
|
32
|
+
|
33
|
+
test "changes global iv" do
|
34
|
+
iv = SecureRandom.hex(100)
|
35
|
+
Parsel.default_iv = iv
|
36
|
+
encrypted = Parsel.encrypt(key, "hello")
|
37
|
+
|
38
|
+
assert_equal "hello", Parsel.decrypt(key, iv, encrypted)
|
39
|
+
end
|
40
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parsel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nando Vieira
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-01-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -25,7 +25,21 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: minitest-utils
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry-meta
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
30
44
|
requirements:
|
31
45
|
- - ">="
|
@@ -46,7 +60,7 @@ extensions: []
|
|
46
60
|
extra_rdoc_files: []
|
47
61
|
files:
|
48
62
|
- ".gitignore"
|
49
|
-
- ".
|
63
|
+
- ".travis.yml"
|
50
64
|
- Gemfile
|
51
65
|
- README.md
|
52
66
|
- Rakefile
|
@@ -55,14 +69,14 @@ files:
|
|
55
69
|
- lib/parsel/marshal.rb
|
56
70
|
- lib/parsel/version.rb
|
57
71
|
- parsel.gemspec
|
58
|
-
-
|
59
|
-
-
|
60
|
-
-
|
61
|
-
-
|
72
|
+
- test/parsel/json_test.rb
|
73
|
+
- test/parsel/marshal_test.rb
|
74
|
+
- test/parsel/parsel_test.rb
|
75
|
+
- test/test_helper.rb
|
62
76
|
homepage: http://rubygems.org/gems/parsel
|
63
77
|
licenses: []
|
64
78
|
metadata: {}
|
65
|
-
post_install_message:
|
79
|
+
post_install_message: "`parsel` is no longer supported. Use at your own risk."
|
66
80
|
rdoc_options: []
|
67
81
|
require_paths:
|
68
82
|
- lib
|
@@ -77,13 +91,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
91
|
- !ruby/object:Gem::Version
|
78
92
|
version: '0'
|
79
93
|
requirements: []
|
80
|
-
|
81
|
-
rubygems_version: 2.4.5.1
|
94
|
+
rubygems_version: 3.1.2
|
82
95
|
signing_key:
|
83
96
|
specification_version: 4
|
84
97
|
summary: Encrypt and decrypt data with a given key.
|
85
98
|
test_files:
|
86
|
-
-
|
87
|
-
-
|
88
|
-
-
|
89
|
-
-
|
99
|
+
- test/parsel/json_test.rb
|
100
|
+
- test/parsel/marshal_test.rb
|
101
|
+
- test/parsel/parsel_test.rb
|
102
|
+
- test/test_helper.rb
|
data/.rspec
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
--color
|
data/spec/parsel/json_spec.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Parsel::JSON do
|
4
|
-
let(:key) { 'my secret key' }
|
5
|
-
|
6
|
-
it 'encrypts data' do
|
7
|
-
expect(Parsel::JSON.encrypt(key, ['hello'])).not_to eq(JSON.dump(['hello']))
|
8
|
-
end
|
9
|
-
|
10
|
-
it 'decrypts data' do
|
11
|
-
encrypted = Parsel::JSON.encrypt(key, ['hello'])
|
12
|
-
expect(Parsel::JSON.decrypt(key, encrypted)).to eq(['hello'])
|
13
|
-
end
|
14
|
-
|
15
|
-
it 'returns false when decryption fails' do
|
16
|
-
expect(Parsel::JSON.decrypt('abc', '123')).to be_falsy
|
17
|
-
end
|
18
|
-
end
|
data/spec/parsel/marshal_spec.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Parsel::Marshal do
|
4
|
-
let(:key) { 'my secret key' }
|
5
|
-
|
6
|
-
it 'encrypts data' do
|
7
|
-
expect(Parsel::Marshal.encrypt(key, 'hello')).not_to eq(Marshal.dump('hello'))
|
8
|
-
end
|
9
|
-
|
10
|
-
it 'decrypts data' do
|
11
|
-
encrypted = Parsel::Marshal.encrypt(key, 'hello')
|
12
|
-
expect(Parsel::Marshal.decrypt(key, encrypted)).to eq('hello')
|
13
|
-
end
|
14
|
-
|
15
|
-
it 'returns false when decryption fails' do
|
16
|
-
expect(Parsel::Marshal.decrypt('abc', '123')).to be_falsy
|
17
|
-
end
|
18
|
-
end
|
data/spec/parsel_spec.rb
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Parsel do
|
4
|
-
let(:key) { 'my secret key' }
|
5
|
-
|
6
|
-
it 'encrypts data' do
|
7
|
-
expect(Parsel.encrypt(key, 'hello')).not_to eq('hello')
|
8
|
-
end
|
9
|
-
|
10
|
-
it 'decrypts data' do
|
11
|
-
encrypted = Parsel.encrypt(key, 'hello')
|
12
|
-
expect(Parsel.decrypt(key, encrypted)).to eq('hello')
|
13
|
-
end
|
14
|
-
|
15
|
-
it 'returns false when decryption fails' do
|
16
|
-
expect(Parsel.decrypt('abc', '123')).to be_falsy
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'uses custom iv' do
|
20
|
-
iv = SecureRandom.hex(100)
|
21
|
-
encrypted = Parsel.encrypt(key, iv, 'hello')
|
22
|
-
|
23
|
-
expect(Parsel.decrypt(key, iv, encrypted)).to eq('hello')
|
24
|
-
end
|
25
|
-
|
26
|
-
it 'return false when decryption for custom iv fails' do
|
27
|
-
iv = SecureRandom.hex(100)
|
28
|
-
encrypted = Parsel.encrypt(key, iv, 'hello')
|
29
|
-
|
30
|
-
expect(Parsel.decrypt(key, encrypted)).to be_falsy
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'changes global iv' do
|
34
|
-
iv = SecureRandom.hex(100)
|
35
|
-
Parsel.default_iv = iv
|
36
|
-
encrypted = Parsel.encrypt(key, 'hello')
|
37
|
-
|
38
|
-
expect(Parsel.decrypt(key, iv, encrypted)).to eq('hello')
|
39
|
-
end
|
40
|
-
end
|