securecompare 1.0.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.
@@ -0,0 +1,51 @@
1
+ class Readme < String
2
+ attr_reader :path
3
+
4
+ def initialize(path)
5
+ @path = path
6
+ super(File.read(self.path))
7
+ end
8
+
9
+ def summary
10
+ if self =~ /^# (?:\S+)\s+(.+?)\s{2,}/m
11
+ scrub $1
12
+ else
13
+ raise "could not find summary in #{path}"
14
+ end
15
+ end
16
+
17
+ def description
18
+ if self =~ /^## Description\s+(.+?)\s{2,}/m
19
+ scrub $1
20
+ else
21
+ raise "could not find description in #{path}"
22
+ end
23
+ end
24
+
25
+ private
26
+ def scrub(string)
27
+ string.delete("\\`").gsub(/\[([^\]]+)\]\([^)]*\)/, "\\1").tr("\n", " ").to_s
28
+ end
29
+ end
30
+
31
+ class Files < Array
32
+ def executables
33
+ grep(%r{^bin/}) { |f| File.basename(f) }
34
+ end
35
+
36
+ def requires
37
+ ["lib"]
38
+ end
39
+
40
+ def tests
41
+ grep(%r{^(test|spec|features)/})
42
+ end
43
+ end
44
+
45
+ def files
46
+ @files ||= Files.new(`git ls-files`.split($/))
47
+ end
48
+
49
+ def readme(path = File.expand_path("./README.md"))
50
+ (@readmes ||= {})[path] ||= Readme.new(path)
51
+ end
@@ -0,0 +1,2 @@
1
+ /Gemfile.lock
2
+ /pkg
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - jruby-19mode
5
+ script: bundle exec rake test
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Samuel Kadolph
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.
@@ -0,0 +1,77 @@
1
+ [![Build Status](https://secure.travis-ci.org/samuelkadolph/securecompare.png?branch=master)](http://travis-ci.org/samuelkadolph/securecompare)
2
+ [![Gem Version](https://badge.fury.io/rb/securecompare.png)](http://badge.fury.io/rb/securecompare)
3
+ [![Dependency Status](https://gemnasium.com/samuelkadolph/securecompare.png)](https://gemnasium.com/samuelkadolph/securecompare)
4
+ [![Code Climate](https://codeclimate.com/github/samuelkadolph/securecompare.png)](https://codeclimate.com/github/samuelkadolph/securecompare)
5
+
6
+ # securecompare
7
+
8
+ securecompare is a gem that implements a constant time string comparison method safe for use in cryptographic functions.
9
+
10
+ ## Description
11
+
12
+ securecompare borrows the `secure_compare` private method from `ActiveSupport::MessageVerifier` which lets you do safely compare strings without being vulnerable to timing attacks. Useful for Basic HTTP Authentication in your rack/rails application.
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem "securecompare"
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ ```
25
+ $ bundle install
26
+ ```
27
+
28
+ Or install it yourself as:
29
+
30
+ ```
31
+ $ gem install securecompare
32
+ ```
33
+
34
+ ## Usage
35
+
36
+ ```ruby
37
+ require "securecompare"
38
+
39
+ SecureCompare.compare("password", "password") # => true
40
+ SecureCompare.compare("password", "passw0rd") # => false
41
+ ```
42
+
43
+ ```ruby
44
+ require "securecompare"
45
+
46
+ class Password < String
47
+ include SecureCompare
48
+
49
+ def ==(other)
50
+ secure_compare(self, other)
51
+ end
52
+ end
53
+
54
+ Password.new("password") == "password" # => true
55
+ Password.new("password") == "passw0rd" # => false
56
+ ```
57
+
58
+ ```ruby
59
+ require "securecompare"
60
+
61
+ class ApplicationController < ActionController::Base
62
+ include SecureCompare
63
+
64
+ before_filter :authenticate
65
+
66
+ proctected
67
+ def authenticate
68
+ authenticate_or_request_with_http_basic("My Rails App") do |username, password|
69
+ secure_compare(username, "username") & secure_compare(password, "password")
70
+ end
71
+ end
72
+ end
73
+ ```
74
+
75
+ ## Contributing
76
+
77
+ Fork, branch & pull request.
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new do |task|
7
+ task.libs << "test"
8
+ task.test_files = Dir["test/**/*_test.rb"]
9
+ task.verbose = true
10
+ end
@@ -0,0 +1,19 @@
1
+ module SecureCompare
2
+ require "securecompare/version"
3
+
4
+ # constant-time comparison algorithm to prevent timing attacks; borrowed from ActiveSupport::MessageVerifier
5
+ def secure_compare(a, b)
6
+ return false unless a.bytesize == b.bytesize
7
+
8
+ l = a.unpack("C#{a.bytesize}")
9
+
10
+ res = 0
11
+ b.each_byte { |byte| res |= byte ^ l.shift }
12
+ res == 0
13
+ end
14
+ module_function :secure_compare
15
+
16
+ class << self
17
+ alias_method :compare, :secure_compare
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module SecureCompare
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,23 @@
1
+ require File.expand_path("../.gemspec", __FILE__)
2
+ require File.expand_path("../lib/securecompare/version", __FILE__)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "securecompare"
6
+ spec.version = SecureCompare::VERSION
7
+ spec.authors = ["Samuel Kadolph"]
8
+ spec.email = ["samuel@kadolph.com"]
9
+ spec.description = readme.description
10
+ spec.summary = readme.summary
11
+ spec.homepage = "https://github.com/samuelkadolph/securecompare"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = files
15
+ spec.executables = files.executables
16
+ spec.test_files = files.tests
17
+ spec.require_paths = files.requires
18
+
19
+ spec.required_ruby_version = ">= 1.9.3"
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,27 @@
1
+ require "test_helper"
2
+
3
+ describe SecureCompare do
4
+ it "should return true for equal strings" do
5
+ SecureCompare.secure_compare("abc", "abc").must_equal(true)
6
+ end
7
+
8
+ it "should return false for not equal strings" do
9
+ SecureCompare.secure_compare("abc", "def").must_equal(false)
10
+ end
11
+
12
+ it "should respond to compare" do
13
+ SecureCompare.must_respond_to(:compare)
14
+ end
15
+
16
+ it "should add secure_compare to anything that includes it" do
17
+ klass = Class.new
18
+ klass.send(:include, SecureCompare)
19
+ klass.private_instance_methods.include?(:secure_compare).must_equal(true)
20
+ end
21
+
22
+ it "should add secure_compare to anything that extends it" do
23
+ klass = Class.new
24
+ klass.send(:extend, SecureCompare)
25
+ klass.private_methods.include?(:secure_compare).must_equal(true)
26
+ end
27
+ end
@@ -0,0 +1,4 @@
1
+ require "minitest/autorun"
2
+ require "minitest/benchmark"
3
+ require "minitest/spec"
4
+ require "securecompare"
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: securecompare
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Samuel Kadolph
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: securecompare borrows the secure_compare private method from ActiveSupport::MessageVerifier
47
+ which lets you do safely compare strings without being vulnerable to timing attacks.
48
+ Useful for Basic HTTP Authentication in your rack/rails application.
49
+ email:
50
+ - samuel@kadolph.com
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - .gemspec.rb
56
+ - .gitignore
57
+ - .travis.yml
58
+ - Gemfile
59
+ - LICENSE
60
+ - README.md
61
+ - Rakefile
62
+ - lib/securecompare.rb
63
+ - lib/securecompare/version.rb
64
+ - securecompare.gemspec
65
+ - test/securecompare_test.rb
66
+ - test/test_helper.rb
67
+ homepage: https://github.com/samuelkadolph/securecompare
68
+ licenses:
69
+ - MIT
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: 1.9.3
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ segments:
87
+ - 0
88
+ hash: 612519151303698657
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 1.8.25
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: securecompare is a gem that implements a constant time string comparison
95
+ method safe for use in cryptographic functions.
96
+ test_files:
97
+ - test/securecompare_test.rb
98
+ - test/test_helper.rb