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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 000da3afd390eaf04fe6f51c600f93b7be937945
4
- data.tar.gz: d7c0e267622a70aca871c6f2fb16837646d4dc60
3
+ metadata.gz: f96c9123157f5ad6b7562413c3cf69ccb3fd5076
4
+ data.tar.gz: 1dc97b4c5e5bf5effb2898643e2253980d167036
5
5
  SHA512:
6
- metadata.gz: 2f69fa492e820633d987da82cffea1e509a1ddc756b4e6a6a101b32399c36e1366fb0023a58440f21e7f0c3edbb480aa4e89a23a655290c967a2f6db73c6456d
7
- data.tar.gz: 637829568b0b91ff915a407088b3234ff7c70cc4155fb4fec40753d398e6154e1e9c4e389e611d1f708c8177fcd41d2b0565b7341e521c52432fefbda8a17dbb
6
+ metadata.gz: c34558d8983d6c947397be302f96884eba2456d48fc46bcdbaf6241a7a2c2ce564fb4848c2a6fb146ac13161bc4dc7759448778e09242f7ec02e0532bc877ba0
7
+ data.tar.gz: 7f1f79ea1e3e2a7be7d14afec2dc6bb76564b2de445a53d74cc718013310e1f8a132bb05b8d7b1b916146a63be7b8bee3b0d204ada4c34658403bbb0f475d56d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- proof_of_work (0.1)
4
+ proof_of_work (0.2)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
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[0...2].split(":")
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[:expiration]
39
- return false if date < options[:expiration].strftime("%y%m%d%H%M%S")
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "proof_of_work"
3
- s.version = "0.1"
3
+ s.version = "0.2"
4
4
  s.summary = "Hashcash algorithm"
5
5
  s.description = "Hashcash version 1 algorithm generator and checker"
6
6
  s.authors = ["elcuervo"]
@@ -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.1'
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-26 00:00:00.000000000 Z
11
+ date: 2014-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest