hashit 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 27906b60ba58d660f3c5a72dba420b742226745d
4
+ data.tar.gz: 8a217dac55d6ca4146260262287623e3eb39f906
5
+ SHA512:
6
+ metadata.gz: da00ea2110c2410cdcc65aa06f2f2dcdfb7abda7d4fe38ac97655e553e50a95f20e2ad45254388074ed6c5a926580395a6054ae4054f8901294438f06dc2e2dc
7
+ data.tar.gz: 2ce32932e0d5e08a0feebd581a693a85eab9fb955f0749acf46256fb670efe28f0cb0727e3e75fc0bce7d99edf2fe90bafc31bc541313518912f522548a0002d
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hashit.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2014 Nitzan Blankleder
2
+ MIT License
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # Hashit
2
+
3
+ Wrapper for Ruby's hashing functions
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'hashit'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install hashit
20
+
21
+ ## Usage
22
+
23
+ Hashit supports the following hashing algorithms
24
+
25
+ - md2
26
+ - md4
27
+ - md5
28
+ - sha
29
+ - sha1
30
+ - sha224
31
+ - sha256
32
+ - sha384
33
+ - sha512
34
+
35
+ For each of these there is the standard method and a timed method. Both accept a key and some text to hash, but the timed version will create a hash that is only valid for a certain amount of time
36
+
37
+ ### Example
38
+ ```ruby
39
+ hash = Hashit.sha256("key", "some text")
40
+ timed_hash = Hashit.timed_sha256("key", "some text")
41
+ ```
42
+
43
+ You can then validate those hashes using `matches?`:
44
+ ```ruby
45
+ if Hashit.matches?(hash, "key", "some text")
46
+ # The data matches the hash
47
+ end
48
+ ```
49
+
50
+ In the case of the timed hash you may want to check if it previously matched the data. In such a case you can use the `did_match?` function:
51
+ ```ruby
52
+ if Hashit.matches?(timed_hash, "key", "some text")
53
+ # The data matches the current timed hash
54
+ elsif Hashit.did_match?(timed_hash, "key", "some text")
55
+ # The data matched the previous timed hash
56
+ end
57
+ ```
58
+
59
+ ## Contributing
60
+
61
+ 1. Fork it ( https://github.com/nitzanbl/hashit/fork )
62
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
63
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
64
+ 4. Push to the branch (`git push origin my-new-feature`)
65
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
data/hashit.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hashit'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hashit"
8
+ spec.version = Hashit::VERSION
9
+ spec.authors = ["Nitzan Blankleder","Gabriel Manricks"]
10
+ spec.email = ["nitzanblanko@gmail.com"]
11
+ spec.summary = "Wrapper for Ruby's hashing functions"
12
+ spec.homepage = "https://github.com/nitzanbl/Hashit"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec"
23
+ spec.add_runtime_dependency "openssl"
24
+ spec.add_runtime_dependency "date"
25
+ end
data/lib/hashit.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "hashit/digests"
2
+ require "hashit/verifier"
3
+
4
+ module Hashit
5
+ VERSION = "0.0.1"
6
+ end
@@ -0,0 +1,47 @@
1
+ require 'openssl'
2
+ require 'date'
3
+ require 'hashit/prepare'
4
+
5
+ module Hashit
6
+ class << self
7
+ %w[md2 md4 md5 sha sha1 sha224 sha256 sha384 sha512].each do |type|
8
+ define_method(type.to_sym) do |key, text|
9
+ "#{type}|" + generate_hash(OpenSSL::Digest.const_get(type.upcase.to_sym).new, key, text)
10
+ end
11
+
12
+ define_method("timed_#{type}".to_sym) do |key, text|
13
+ key = ([] << key).flatten << current_time
14
+ "timed_#{type}|" + generate_hash(OpenSSL::Digest.const_get(type.upcase.to_sym).new, key, text)
15
+ end
16
+
17
+ define_method("previous_#{type}".to_sym) do |key, text|
18
+ key = ([] << key).flatten << last_time
19
+ "timed_#{type}|" + generate_hash(OpenSSL::Digest.const_get(type.upcase.to_sym).new, key, text)
20
+ end
21
+ end
22
+
23
+ def current_time
24
+ d = DateTime.now
25
+ ts = d.strftime("%s").to_i
26
+ ts - ((d.minute % 30) * 60) - d.second
27
+ end
28
+
29
+ def last_time
30
+ current_time - (30 * 60)
31
+ end
32
+
33
+ def generate_hash digest, key, text
34
+ text = Hashit.prepare text
35
+
36
+ if key.is_a? Array
37
+ key.reduce(text) do |text, k|
38
+ k = Hashit.prepare k
39
+ OpenSSL::HMAC.hexdigest(digest, k, text)
40
+ end
41
+ else
42
+ key = Hashit.prepare key
43
+ OpenSSL::HMAC.hexdigest(digest, key, text)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,25 @@
1
+ module Hashit
2
+ class << self
3
+ def prepare obj
4
+ return obj if obj.is_a? String
5
+
6
+ if obj.is_a? Array
7
+ obj = prepare_array obj
8
+ elsif obj.nil?
9
+ raise ArgumentError, "Nil passed in as a text parameter"
10
+ elsif obj.respond_to? :to_s
11
+ obj = obj.to_s
12
+ else
13
+ raise ArgumentError, "Parameter #{obj} cannot be converted to string"
14
+ end
15
+
16
+ obj
17
+ end
18
+
19
+ def prepare_array arr
20
+ arr.reduce("") do |str, el|
21
+ str += prepare el
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,19 @@
1
+ require 'hashit/digests'
2
+
3
+ module Hashit
4
+ class << self
5
+ def matches? hash, key, text
6
+ type = hash.split('|').first
7
+ raise ArgumentError, "invalid hash" unless Hashit.respond_to?(type.to_sym)
8
+ new_hash = Hashit.send type.to_sym, key, text
9
+ new_hash == hash
10
+ end
11
+
12
+ def did_match? hash, key, text
13
+ type = hash.split('|').first.sub("timed", "previous")
14
+ raise ArgumentError, "invalid hash" unless Hashit.respond_to?(type.to_sym)
15
+ new_hash = Hashit.send type.to_sym, key, text
16
+ new_hash == hash
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Hashit Digests" do
4
+ it 'responds to all the hashing digests' do
5
+ fns = [:md2, :md4, :md5, :sha, :sha1, :sha224, :sha256, :sha384, :sha512]
6
+
7
+ expect(Hashit).to respond_to(*fns)
8
+ end
9
+
10
+ it 'generates a hash from a key and text' do
11
+ key = "Key"
12
+ text = "Some Text"
13
+ hash = "sha256|cd904d6df2122eddf1d6df1240730d29056c171539f171a57ada2a16f5a54bf0"
14
+
15
+ expect(Hashit.sha256 key, text).to be == hash
16
+ end
17
+
18
+ it 'generates a hash from an array of strings' do
19
+ key = "key"
20
+ text = %w[nitzan gabriel]
21
+ hash = "sha256|08665f6532b0b28c1298955e2db2558a7ef06624708ac6cba6e73e88e0351b7d"
22
+
23
+ expect(Hashit.sha256 key, text).to be == hash
24
+ end
25
+
26
+ it 'generates a recursive hash from an array of keys' do
27
+ key = %w[ron vivi]
28
+ text = "text"
29
+ hash = "sha256|54b11c895b426b527bc8c7029b3064d9b48dace3359b143268dac21d472def9c"
30
+
31
+ expect(Hashit.sha256 key, text).to be == hash
32
+ end
33
+
34
+ it 'converts parameters to strings' do
35
+ key = 32
36
+ text = 45
37
+ hash="sha256|ef4f59fe5e91a86be6720d34a0b3413b1119638e271bcc4d2c24dbb7ef6a4bba"
38
+
39
+ expect(Hashit.sha256 key, text).to be == hash
40
+ end
41
+
42
+ it 'raises an error on invalid parameters' do
43
+ key = nil
44
+ text = 45
45
+
46
+ expect{Hashit.sha256 key, text}.to raise_error(ArgumentError)
47
+ end
48
+
49
+ it 'generates a timed hash' do
50
+ key = "key"
51
+ text = "some text"
52
+ hash = "timed_#{Hashit.sha256([key, Hashit.current_time], text)}"
53
+
54
+ expect(Hashit.timed_sha256 key, text).to be == hash
55
+ end
56
+
57
+ it 'generates a previous timed hash' do
58
+ key = "key"
59
+ text = "some text"
60
+ hash = "timed_#{Hashit.sha256([key, Hashit.last_time], text)}"
61
+
62
+ expect(Hashit.previous_sha256 key, text).to be == hash
63
+ end
64
+
65
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hashit do
4
+ it 'has a version number' do
5
+ expect(Hashit::VERSION).not_to be nil
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'hashit'
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Hashit Verifier" do
4
+
5
+ it 'can verify an old hash' do
6
+ hash = "sha256|cd904d6df2122eddf1d6df1240730d29056c171539f171a57ada2a16f5a54bf0"
7
+ key = "Key"
8
+ text = "Some Text"
9
+
10
+ expect(Hashit.matches? hash, key, text).to be true
11
+ end
12
+
13
+ it 'raises an error on invalid parameters' do
14
+ hash = "cd904d6df2122eddf1d6df1240730d29056c171539f171a57ada2a16f5a54bf0"
15
+ key = "Key"
16
+ text = "Some Text"
17
+
18
+ expect{Hashit.matches? hash, key, text}.to raise_error(ArgumentError)
19
+ end
20
+
21
+ it 'matches timed hashes' do
22
+ key = "Key"
23
+ text = "Some Text"
24
+ hash = Hashit.timed_sha256 key, text
25
+
26
+ expect(Hashit.matches? hash, key, text).to be true
27
+ end
28
+
29
+ it 'matches previous hashes' do
30
+ key = "Key"
31
+ text = "Some Text"
32
+ hash = Hashit.previous_sha256 key, text
33
+
34
+ expect(Hashit.matches? hash, key, text).to be false
35
+ expect(Hashit.did_match? hash, key, text).to be true
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hashit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nitzan Blankleder
8
+ - Gabriel Manricks
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-08-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: '1.7'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: '1.7'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: openssl
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: date
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ description:
85
+ email:
86
+ - nitzanblanko@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - .gitignore
92
+ - .rspec
93
+ - .travis.yml
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - hashit.gemspec
99
+ - lib/hashit.rb
100
+ - lib/hashit/digests.rb
101
+ - lib/hashit/prepare.rb
102
+ - lib/hashit/verifier.rb
103
+ - spec/digests_spec.rb
104
+ - spec/hashit_spec.rb
105
+ - spec/spec_helper.rb
106
+ - spec/verifier_spec.rb
107
+ homepage: https://github.com/nitzanbl/Hashit
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.2.2
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Wrapper for Ruby's hashing functions
131
+ test_files:
132
+ - spec/digests_spec.rb
133
+ - spec/hashit_spec.rb
134
+ - spec/spec_helper.rb
135
+ - spec/verifier_spec.rb