hashit 0.0.2 → 0.0.3
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 +4 -4
- data/lib/hashit.rb +2 -2
- data/lib/hashit/digests.rb +55 -7
- data/spec/digests_spec.rb +10 -8
- data/spec/verifier_spec.rb +3 -1
- metadata +2 -4
- data/lib/hashit/prepare.rb +0 -25
- data/lib/hashit/verifier.rb +0 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: beffcc18c2dc50482e9b341149136dc4f0240365
|
4
|
+
data.tar.gz: ca9eedf8bef02179ea1750fe11576598bcca7940
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a6bb0d4aef6dfa2413d6ca8f9f24ba5833b513282f3a37acea2def47cc8e22c8699359093252b51c5adab718852efe62e6f94656f328feec1d07279ecef8462
|
7
|
+
data.tar.gz: 5a36dd04fc4e313b0a06f4717976887afae92a10a467b391400e94eb6c942d0a41d06d653aec66a22a785a8070aafb2c6c60815c60def10a37ab15a8270a906c
|
data/lib/hashit.rb
CHANGED
data/lib/hashit/digests.rb
CHANGED
@@ -3,20 +3,22 @@ require 'date'
|
|
3
3
|
require 'hashit/prepare'
|
4
4
|
|
5
5
|
module Hashit
|
6
|
-
|
7
|
-
%w[md2 md4 md5 sha sha1 sha224 sha256 sha384 sha512]
|
6
|
+
module Digests
|
7
|
+
HASHING_DIGESTS = %w[md2 md4 md5 sha sha1 sha224 sha256 sha384 sha512]
|
8
|
+
|
9
|
+
HASHING_DIGESTS.each.with_index do |type, i|
|
8
10
|
define_method(type.to_sym) do |key, text|
|
9
|
-
"#{
|
11
|
+
"r#{i}|" + generate_hash(OpenSSL::Digest.const_get(type.upcase.to_sym).new, key, text)
|
10
12
|
end
|
11
13
|
|
12
14
|
define_method("timed_#{type}".to_sym) do |key, text|
|
13
15
|
key = ([] << key).flatten << current_time
|
14
|
-
"
|
16
|
+
"t#{i}|" + generate_hash(OpenSSL::Digest.const_get(type.upcase.to_sym).new, key, text)
|
15
17
|
end
|
16
18
|
|
17
19
|
define_method("previous_#{type}".to_sym) do |key, text|
|
18
20
|
key = ([] << key).flatten << last_time
|
19
|
-
"
|
21
|
+
"t#{i}|" + generate_hash(OpenSSL::Digest.const_get(type.upcase.to_sym).new, key, text)
|
20
22
|
end
|
21
23
|
end
|
22
24
|
|
@@ -31,11 +33,11 @@ module Hashit
|
|
31
33
|
end
|
32
34
|
|
33
35
|
def generate_hash digest, key, text
|
34
|
-
text =
|
36
|
+
text = prepare text
|
35
37
|
|
36
38
|
if key.is_a? Array
|
37
39
|
key.reduce(text) do |text, k|
|
38
|
-
k =
|
40
|
+
k = prepare k
|
39
41
|
OpenSSL::HMAC.hexdigest(digest, k, text)
|
40
42
|
end
|
41
43
|
else
|
@@ -43,5 +45,51 @@ module Hashit
|
|
43
45
|
OpenSSL::HMAC.hexdigest(digest, key, text)
|
44
46
|
end
|
45
47
|
end
|
48
|
+
|
49
|
+
def prepare obj
|
50
|
+
return obj if obj.is_a? String
|
51
|
+
|
52
|
+
if obj.is_a? Array
|
53
|
+
obj = prepare_array obj
|
54
|
+
elsif obj.nil?
|
55
|
+
raise ArgumentError, "Nil passed in as a text parameter"
|
56
|
+
elsif obj.respond_to? :to_s
|
57
|
+
obj = obj.to_s
|
58
|
+
else
|
59
|
+
raise ArgumentError, "Parameter #{obj} cannot be converted to string"
|
60
|
+
end
|
61
|
+
|
62
|
+
obj
|
63
|
+
end
|
64
|
+
|
65
|
+
def prepare_array arr
|
66
|
+
arr.reduce("") do |str, el|
|
67
|
+
str += prepare el
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def matches? hash, key, text
|
72
|
+
type = get_hash_type hash.split('|').first
|
73
|
+
raise ArgumentError, "invalid hash" if type.nil? || !Hashit.respond_to?(type)
|
74
|
+
new_hash = Hashit.send type, key, text
|
75
|
+
new_hash == hash
|
76
|
+
end
|
77
|
+
|
78
|
+
def did_match? hash, key, text
|
79
|
+
type = get_hash_type hash.split('|').first
|
80
|
+
fn = type.to_s.sub("timed", "previous").to_sym
|
81
|
+
raise ArgumentError, "invalid hash" if type.nil? || !Hashit.respond_to?(fn)
|
82
|
+
new_hash = Hashit.send fn, key, text
|
83
|
+
new_hash == hash
|
84
|
+
end
|
85
|
+
|
86
|
+
private
|
87
|
+
def get_hash_type segment
|
88
|
+
i = segment[1..-1].to_i
|
89
|
+
return nil if segment[1..-1] != i.to_s
|
90
|
+
type = HASHING_DIGESTS[i]
|
91
|
+
type = "timed_#{type}" if segment[0] == 't'
|
92
|
+
type.to_sym
|
93
|
+
end
|
46
94
|
end
|
47
95
|
end
|
data/spec/digests_spec.rb
CHANGED
@@ -10,7 +10,7 @@ describe "Hashit Digests" do
|
|
10
10
|
it 'generates a hash from a key and text' do
|
11
11
|
key = "Key"
|
12
12
|
text = "Some Text"
|
13
|
-
hash = "
|
13
|
+
hash = "r6|cd904d6df2122eddf1d6df1240730d29056c171539f171a57ada2a16f5a54bf0"
|
14
14
|
|
15
15
|
expect(Hashit.sha256 key, text).to be == hash
|
16
16
|
end
|
@@ -18,7 +18,7 @@ describe "Hashit Digests" do
|
|
18
18
|
it 'generates a hash from an array of strings' do
|
19
19
|
key = "key"
|
20
20
|
text = %w[nitzan gabriel]
|
21
|
-
hash = "
|
21
|
+
hash = "r6|08665f6532b0b28c1298955e2db2558a7ef06624708ac6cba6e73e88e0351b7d"
|
22
22
|
|
23
23
|
expect(Hashit.sha256 key, text).to be == hash
|
24
24
|
end
|
@@ -26,7 +26,7 @@ describe "Hashit Digests" do
|
|
26
26
|
it 'generates a recursive hash from an array of keys' do
|
27
27
|
key = %w[ron vivi]
|
28
28
|
text = "text"
|
29
|
-
hash = "
|
29
|
+
hash = "r6|54b11c895b426b527bc8c7029b3064d9b48dace3359b143268dac21d472def9c"
|
30
30
|
|
31
31
|
expect(Hashit.sha256 key, text).to be == hash
|
32
32
|
end
|
@@ -34,7 +34,7 @@ describe "Hashit Digests" do
|
|
34
34
|
it 'converts parameters to strings' do
|
35
35
|
key = 32
|
36
36
|
text = 45
|
37
|
-
hash="
|
37
|
+
hash="r6|ef4f59fe5e91a86be6720d34a0b3413b1119638e271bcc4d2c24dbb7ef6a4bba"
|
38
38
|
|
39
39
|
expect(Hashit.sha256 key, text).to be == hash
|
40
40
|
end
|
@@ -49,17 +49,19 @@ describe "Hashit Digests" do
|
|
49
49
|
it 'generates a timed hash' do
|
50
50
|
key = "key"
|
51
51
|
text = "some text"
|
52
|
-
|
52
|
+
allow(Hashit).to receive(:current_time).and_return(1408350600)
|
53
|
+
hash = Hashit.sha256([key, 1408350600], text)
|
53
54
|
|
54
|
-
expect(Hashit.timed_sha256
|
55
|
+
expect(Hashit.timed_sha256(key, text)[1..-1]).to be == hash[1..-1]
|
55
56
|
end
|
56
57
|
|
57
58
|
it 'generates a previous timed hash' do
|
58
59
|
key = "key"
|
59
60
|
text = "some text"
|
60
|
-
|
61
|
+
allow(Hashit).to receive(:current_time).and_return(1408350600)
|
62
|
+
hash = Hashit.sha256([key, 1408348800], text)
|
61
63
|
|
62
|
-
expect(Hashit.previous_sha256
|
64
|
+
expect(Hashit.previous_sha256(key, text)[1..-1]).to be == hash[1..-1]
|
63
65
|
end
|
64
66
|
|
65
67
|
end
|
data/spec/verifier_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe "Hashit Verifier" do
|
4
4
|
|
5
5
|
it 'can verify an old hash' do
|
6
|
-
hash = "
|
6
|
+
hash = "r6|cd904d6df2122eddf1d6df1240730d29056c171539f171a57ada2a16f5a54bf0"
|
7
7
|
key = "Key"
|
8
8
|
text = "Some Text"
|
9
9
|
|
@@ -21,6 +21,7 @@ describe "Hashit Verifier" do
|
|
21
21
|
it 'matches timed hashes' do
|
22
22
|
key = "Key"
|
23
23
|
text = "Some Text"
|
24
|
+
allow(Hashit).to receive(:current_time).and_return(1408350600)
|
24
25
|
hash = Hashit.timed_sha256 key, text
|
25
26
|
|
26
27
|
expect(Hashit.matches? hash, key, text).to be true
|
@@ -29,6 +30,7 @@ describe "Hashit Verifier" do
|
|
29
30
|
it 'matches previous hashes' do
|
30
31
|
key = "Key"
|
31
32
|
text = "Some Text"
|
33
|
+
allow(Hashit).to receive(:current_time).and_return(1408350600)
|
32
34
|
hash = Hashit.previous_sha256 key, text
|
33
35
|
|
34
36
|
expect(Hashit.matches? hash, key, text).to be false
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hashit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nitzan Blankleder
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-08-
|
12
|
+
date: 2014-08-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -70,8 +70,6 @@ files:
|
|
70
70
|
- hashit.gemspec
|
71
71
|
- lib/hashit.rb
|
72
72
|
- lib/hashit/digests.rb
|
73
|
-
- lib/hashit/prepare.rb
|
74
|
-
- lib/hashit/verifier.rb
|
75
73
|
- spec/digests_spec.rb
|
76
74
|
- spec/hashit_spec.rb
|
77
75
|
- spec/spec_helper.rb
|
data/lib/hashit/prepare.rb
DELETED
@@ -1,25 +0,0 @@
|
|
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
|
data/lib/hashit/verifier.rb
DELETED
@@ -1,19 +0,0 @@
|
|
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
|