proof_of_work 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/proof_of_work.rb +9 -6
- data/proof_of_work.gemspec +1 -1
- data/test/proof_of_work_test.rb +38 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f96c9123157f5ad6b7562413c3cf69ccb3fd5076
|
4
|
+
data.tar.gz: 1dc97b4c5e5bf5effb2898643e2253980d167036
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c34558d8983d6c947397be302f96884eba2456d48fc46bcdbaf6241a7a2c2ce564fb4848c2a6fb146ac13161bc4dc7759448778e09242f7ec02e0532bc877ba0
|
7
|
+
data.tar.gz: 7f1f79ea1e3e2a7be7d14afec2dc6bb76564b2de445a53d74cc718013310e1f8a132bb05b8d7b1b916146a63be7b8bee3b0d204ada4c34658403bbb0f475d56d
|
data/Gemfile.lock
CHANGED
data/lib/proof_of_work.rb
CHANGED
@@ -30,13 +30,13 @@ class ProofOfWork
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def valid?(stamp, options = {})
|
33
|
-
bits_claim, date, identifier, extension, rand, counter = stamp
|
33
|
+
_, bits_claim, date, identifier, extension, rand, counter = stamp.split(":")
|
34
34
|
|
35
|
-
return false if options[:identifier] != identifier
|
36
|
-
return false if options[:bits].to_i > bits_claim.to_i
|
35
|
+
return false if options[:identifier] && options[:identifier] != identifier
|
36
|
+
return false if options[:bits] && options[:bits].to_i > bits_claim.to_i
|
37
37
|
|
38
|
-
if options[:
|
39
|
-
return false if date < options[:
|
38
|
+
if options[:expiration_date]
|
39
|
+
return false if date < options[:expiration_date].strftime("%y%m%d%H%M%S")
|
40
40
|
end
|
41
41
|
|
42
42
|
hex_digits = (bits_claim.to_i/4.0).floor.to_i
|
@@ -44,7 +44,10 @@ class ProofOfWork
|
|
44
44
|
sha1 = OpenSSL::Digest::SHA1.new
|
45
45
|
sha1 << stamp
|
46
46
|
|
47
|
-
sha1.hexdigest.start_with?("0" * hex_digits)
|
47
|
+
is_valid = sha1.hexdigest.start_with?("0" * hex_digits)
|
48
|
+
yield(extension) if block_given?
|
49
|
+
|
50
|
+
is_valid
|
48
51
|
end
|
49
52
|
|
50
53
|
private
|
data/proof_of_work.gemspec
CHANGED
data/test/proof_of_work_test.rb
CHANGED
@@ -10,4 +10,42 @@ describe ProofOfWork do
|
|
10
10
|
When(:hashcash) { ProofOfWork.generate("test") }
|
11
11
|
Then { ProofOfWork.valid?(hashcash) == true }
|
12
12
|
end
|
13
|
+
|
14
|
+
context "Accessing extension" do
|
15
|
+
Given(:hashcash) do
|
16
|
+
ProofOfWork.generate("unique_user_id",
|
17
|
+
extension: "Hello_this_is_dog", # Something you want to add to the hashcash
|
18
|
+
salt_chars: 1
|
19
|
+
)
|
20
|
+
end
|
21
|
+
|
22
|
+
When(:extension) do
|
23
|
+
extension = nil
|
24
|
+
ProofOfWork.valid?(hashcash) { |ext| extension = ext }
|
25
|
+
extension
|
26
|
+
end
|
27
|
+
|
28
|
+
Then { extension == "Hello_this_is_dog" }
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
context "Show optional usage" do
|
33
|
+
When(:hashcash) do
|
34
|
+
ProofOfWork.generate("unique_user_id",
|
35
|
+
now: Time.now + 3600 * 10, # Change curernt time to perfonm the validation
|
36
|
+
bits: 2,
|
37
|
+
extension: "Im_a_unique_thing_you_can_add",
|
38
|
+
salt_chars: 1,
|
39
|
+
stamp_seconds: true
|
40
|
+
)
|
41
|
+
end
|
42
|
+
|
43
|
+
Then do
|
44
|
+
ProofOfWork.valid?(hashcash,
|
45
|
+
identifier: "unique_user_id", # Checks for the unique id
|
46
|
+
bits: 2, # Checks if it has the correct amount of bits
|
47
|
+
expiration_date: Time.now - 3600 # Allow only hashes that have not expired
|
48
|
+
)
|
49
|
+
end
|
50
|
+
end
|
13
51
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: proof_of_work
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- elcuervo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|