signed_parameters 0.0.1
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 +17 -0
- data/.rspec +1 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +80 -0
- data/Rakefile +1 -0
- data/lib/signed_parameters/builder.rb +45 -0
- data/lib/signed_parameters/version.rb +3 -0
- data/lib/signed_parameters.rb +8 -0
- data/signed_parameters.gemspec +26 -0
- data/spec/signed_parameters/builder_spec.rb +35 -0
- data/spec/spec_helper.rb +4 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2c8be0169eeedb917b05bda02043e41fa0ae0bad
|
4
|
+
data.tar.gz: 92d935701596c8ca75206541ce3c706ad6a74fc7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2f25316bf6a23ede20f549c1b5eae24c704e1a76139be7aec332e5dc3ad3f8bd9a5118ce828b55f48947c01a022d2c6805db1a1af654f56fa1a8fe88a86f361a
|
7
|
+
data.tar.gz: e7bcf8acfe8aa9e100d7d875f77afefde21ed720257ff8176d299e3754520903265c77cdb3caf4c92ae2830aa7219d483bda9faf0d78f0f6d01ba5942954253d
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Evgeniy Serykh
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
[](https://travis-ci.org/evserykh/signed_parameters) [](https://gemnasium.com/evserykh/signed_parameters)
|
2
|
+
***
|
3
|
+
# SignedParameters
|
4
|
+
|
5
|
+
Allow to sign data with secret key
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'signed_parameters'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
##How it works
|
18
|
+
|
19
|
+
Assume, the both sides know secret key, and one of them must send a data to another, and another side must receive the data and verify the data is signed with correct key.
|
20
|
+
|
21
|
+
The sending side gets the sign and adds it to sending data, the receiving side does the same things with received data and compares obtained sign with received sign.
|
22
|
+
|
23
|
+
## Algorithm of computing of sign
|
24
|
+
|
25
|
+
1. Build an array of values to be sended
|
26
|
+
2. Remove from the array empty values
|
27
|
+
3. Sort the array
|
28
|
+
4. Add secret key to the end of array
|
29
|
+
5. Join elements of array into a string
|
30
|
+
6. Compute the SHA-1 digest from the string from step 5
|
31
|
+
7. Compute the MD5 digest from the string from step 6
|
32
|
+
|
33
|
+
## Example
|
34
|
+
|
35
|
+
The sending data are { :email => 'test@test.com', :username => 'John Smith', :age => 66, :address => '' }
|
36
|
+
|
37
|
+
The secret key is c5fc6b5cff2d52791ecaae659200de5e
|
38
|
+
|
39
|
+
Step 1. [ 'test@test.com', 'John Smith', 66, '' ]
|
40
|
+
|
41
|
+
Step 2. [ 'test@test.com', 'John Smith', 66 ]
|
42
|
+
|
43
|
+
Step 3. [ 66, 'John Smith', 'test@test.com' ]
|
44
|
+
|
45
|
+
Step 4. [ 66, 'John Smith', 'test@test.com', 'c5fc6b5cff2d52791ecaae659200de5e' ]
|
46
|
+
|
47
|
+
Step 5. str = 66;John Smith;test@test.com;c5fc6b5cff2d52791ecaae659200de5e
|
48
|
+
|
49
|
+
Step 6. sha1 = SHA1(str)
|
50
|
+
|
51
|
+
Step 7. md5 = MD5(sha1)
|
52
|
+
|
53
|
+
So, for the data { :email => 'test@test.com', :username => 'John Smith', :age => 66, :address => '' } the sign will be 0fc248b16df686b7fcb5c5dc9ce701d
|
54
|
+
|
55
|
+
## Usage
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
> params = { :email => 'test@test.com', :username => 'John Smith', :age => 66, :address => '' }
|
59
|
+
=> {:email=>"test@test.com", :username=>"John Smith", :age=>66, :address=>""}
|
60
|
+
> secret = 'c5fc6b5cff2d52791ecaae659200de5e'
|
61
|
+
=> "c5fc6b5cff2d52791ecaae659200de5e"
|
62
|
+
> SignedParameters.to_query(params, secret)
|
63
|
+
=> "age=66&email=test%40test.com&sign=0fc248b16df686b7fcb5c5dc9ce701d8&username=John+Smith"
|
64
|
+
```
|
65
|
+
or
|
66
|
+
```ruby
|
67
|
+
> builder = SignedParameters::Builder.new(params, secret)
|
68
|
+
=> #<SignedParameters::Builder:0x007f8f8835c770 @parameters={:email=>"test@test.com", :username=>"John Smith", :age=>66, :address=>""}, @secret="c5fc6b5cff2d52791ecaae659200de5e", @separator=";">
|
69
|
+
> builder.sign
|
70
|
+
=> "0fc248b16df686b7fcb5c5dc9ce701d8"
|
71
|
+
```
|
72
|
+
|
73
|
+
|
74
|
+
## Contributing
|
75
|
+
|
76
|
+
1. Fork it ( http://github.com/<my-github-username>/signed_parameters/fork )
|
77
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
78
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
79
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
80
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'active_support/core_ext/string'
|
2
|
+
|
3
|
+
module SignedParameters
|
4
|
+
class Builder
|
5
|
+
def initialize(parameters, secret, separator = ';')
|
6
|
+
@parameters = parameters
|
7
|
+
@secret = secret
|
8
|
+
@separator = separator
|
9
|
+
end
|
10
|
+
|
11
|
+
def sign
|
12
|
+
md5
|
13
|
+
end
|
14
|
+
|
15
|
+
def parameters_with_sign
|
16
|
+
used_parameters.merge :sign => sign
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_query
|
20
|
+
parameters_with_sign.to_query
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def used_parameters
|
26
|
+
@parameters.select { |k, v| v.present? }
|
27
|
+
end
|
28
|
+
|
29
|
+
def sorted_parameters_values
|
30
|
+
used_parameters.values.map(&:to_s).sort
|
31
|
+
end
|
32
|
+
|
33
|
+
def string_for_sign
|
34
|
+
(sorted_parameters_values + [@secret]).join @separator
|
35
|
+
end
|
36
|
+
|
37
|
+
def sha1
|
38
|
+
Digest::SHA1.hexdigest string_for_sign
|
39
|
+
end
|
40
|
+
|
41
|
+
def md5
|
42
|
+
Digest::MD5.hexdigest sha1
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'signed_parameters/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "signed_parameters"
|
8
|
+
spec.version = SignedParameters::VERSION
|
9
|
+
spec.authors = ["Evgeniy Serykh"]
|
10
|
+
spec.email = ["e.v.serykh@gmail.com"]
|
11
|
+
spec.summary = %q{Signed parameters}
|
12
|
+
spec.description = %q{Signed parameters}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency 'activesupport', '>= 2.2.1'
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SignedParameters::Builder do
|
4
|
+
let(:params) { { :foo => 'foo', :bar => :bar, :baz => '', 'qux' => 42 } }
|
5
|
+
let(:secret) { 'some secret token' }
|
6
|
+
let(:separator) { ';' }
|
7
|
+
|
8
|
+
let(:sign) { '17223137b3c5f28247367deb3f86dc9d' }
|
9
|
+
|
10
|
+
subject { SignedParameters::Builder.new params, secret, separator }
|
11
|
+
|
12
|
+
describe '#used_parameters' do
|
13
|
+
its(:used_parameters) { should == { :foo => 'foo', :bar => :bar, 'qux' => 42 } }
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#sorted_parameters_values' do
|
17
|
+
its(:sorted_parameters_values) { should == ['42', 'bar', 'foo'] }
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#string_for_sign' do
|
21
|
+
its(:string_for_sign) { should == '42;bar;foo;some secret token' }
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#sign' do
|
25
|
+
its(:sign) { should == sign }
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#parameters_with_sign' do
|
29
|
+
its(:parameters_with_sign) { should == { :foo => 'foo', :bar => :bar, 'qux' => 42, :sign => sign } }
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#to_query' do
|
33
|
+
its(:to_query) { should == 'bar=bar&foo=foo&qux=42&sign=17223137b3c5f28247367deb3f86dc9d' }
|
34
|
+
end
|
35
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: signed_parameters
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Evgeniy Serykh
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.2.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.2.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.5'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.5'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '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'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Signed parameters
|
70
|
+
email:
|
71
|
+
- e.v.serykh@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- lib/signed_parameters.rb
|
84
|
+
- lib/signed_parameters/builder.rb
|
85
|
+
- lib/signed_parameters/version.rb
|
86
|
+
- signed_parameters.gemspec
|
87
|
+
- spec/signed_parameters/builder_spec.rb
|
88
|
+
- spec/spec_helper.rb
|
89
|
+
homepage: ''
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.2.2
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: Signed parameters
|
113
|
+
test_files:
|
114
|
+
- spec/signed_parameters/builder_spec.rb
|
115
|
+
- spec/spec_helper.rb
|