simple_signer 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "rake/testtask"
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.test_files = Dir["test/**/*_test.rb"]
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
@@ -0,0 +1,2 @@
1
+ require 'simple_signer/signer'
2
+
@@ -0,0 +1,40 @@
1
+ =begin
2
+ This file is part of SimpleSigner.
3
+
4
+ SimpleSigner is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU Lesser General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License.
7
+
8
+ SimpleSigner is distributed in the hope that it will be useful,
9
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ GNU Lesser General Public License for more details.
12
+
13
+ You should have received a copy of the GNU Lesser General Public License
14
+ along with SimpleSigner. If not, see <http://www.gnu.org/licenses/>.
15
+ =end
16
+
17
+ require 'openssl'
18
+ require 'base64'
19
+ require 'cgi'
20
+
21
+ class SimpleSigner
22
+
23
+ def self.sign options = {}
24
+ raise 'secret_key is required!' unless options.include?(:secret_key) and options[:secret_key]
25
+ raise 'string is required!' unless options.include?(:string) and options[:string]
26
+
27
+ signed = OpenSSL::HMAC.digest('sha1', options[:secret_key], options[:string].encode('utf-8'))
28
+
29
+ Base64.encode64(signed).strip
30
+ end
31
+
32
+ def self.escape token
33
+ token.empty? ? token : CGI.escape(token)
34
+ end
35
+
36
+ def self.unescape token
37
+ (token.empty? or not token.include?('%')) ? token : CGI.unescape(token)
38
+ end
39
+
40
+ end
@@ -0,0 +1,10 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "simple_signer"
3
+ s.version = "0.0.2"
4
+ s.description = "Sign your strings!"
5
+ s.summary = "Simple signer!"
6
+ s.author = "Lucas D'Avila"
7
+ s.email = 'lucas@lucasdavi.la'
8
+ s.homepage = 'https://github.com/simpleservices/simple_signer'
9
+ s.files = Dir["{lib/**/*.rb,README.rdoc,test/**/*.rb,Rakefile,*.gemspec}"]
10
+ end
@@ -0,0 +1,67 @@
1
+ # coding=utf-8
2
+
3
+ =begin
4
+ This file is part of SimpleSigner.
5
+
6
+ SimpleSigner is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU Lesser General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License.
9
+
10
+ SimpleSigner is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with SimpleSigner. If not, see <http://www.gnu.org/licenses/>.
17
+ =end
18
+
19
+ require 'test/unit'
20
+ require 'simple_signer'
21
+
22
+ class SimpleSignerTest < Test::Unit::TestCase
23
+
24
+ def test_raise_secret_key_is_required
25
+ assert_raise RuntimeError, 'secret_key is required!' do
26
+ SimpleSigner.sign
27
+ end
28
+ end
29
+
30
+ def test_raise_string_is_required
31
+ assert_raise RuntimeError, 'string is required!' do
32
+ SimpleSigner.sign :secret_key => 'abc'
33
+ end
34
+ end
35
+
36
+ def test_sign_strings
37
+ key = 'DrAGMrjPbJWHn+9ioJrA3s5/Q3ownDFBYZqFcZLm'
38
+ string = 'Some string to sign => é ê ç ê á ṕ ǵ ś "a \'b <c/> \\d %e'
39
+
40
+ expected_signature = 'k0YywdmJaMbQYfWXUZzRkc/Zhiw='
41
+ given_signature = SimpleSigner.sign :secret_key => key, :string => string
42
+
43
+ assert_equal expected_signature, given_signature
44
+ end
45
+
46
+ def test_escape_strings
47
+ string = 'CfsldUIyOOORHjnE8PIl0k0xmOQ='
48
+ expected = 'CfsldUIyOOORHjnE8PIl0k0xmOQ%3D'
49
+
50
+ assert_equal expected, SimpleSigner.escape(string)
51
+ end
52
+
53
+ def test_unescape_escaped_strings
54
+ string = 'CfsldUIyOOORHjnE8PIl0k0xmOQ%3D'
55
+ expected = 'CfsldUIyOOORHjnE8PIl0k0xmOQ='
56
+
57
+ assert_equal expected, SimpleSigner.unescape(string)
58
+ end
59
+
60
+ def test_unescape_only_escaped_strings
61
+ string = 'CfsldUIyOOORHjnE8PIl0k0xmOQ='
62
+ expected = 'CfsldUIyOOORHjnE8PIl0k0xmOQ='
63
+
64
+ assert_equal expected, SimpleSigner.unescape(string)
65
+ end
66
+
67
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_signer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Lucas D'Avila
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-21 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Sign your strings!
15
+ email: lucas@lucasdavi.la
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/simple_signer.rb
21
+ - lib/simple_signer/signer.rb
22
+ - test/simple_signer_test.rb
23
+ - Rakefile
24
+ - simple_signer.gemspec
25
+ homepage: https://github.com/simpleservices/simple_signer
26
+ licenses: []
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 1.8.24
46
+ signing_key:
47
+ specification_version: 3
48
+ summary: Simple signer!
49
+ test_files: []