… | |
… | |
21 | =cut |
21 | =cut |
22 | |
22 | |
23 | package Digest::Hashcash; |
23 | package Digest::Hashcash; |
24 | |
24 | |
25 | use Time::Local; |
25 | use Time::Local; |
|
|
26 | use Time::HiRes; |
26 | |
27 | |
27 | require XSLoader; |
28 | require XSLoader; |
28 | |
29 | |
29 | no warnings; |
30 | no warnings; |
30 | |
31 | |
31 | $VERSION = 0.01; |
32 | $VERSION = 0.02; |
32 | |
33 | |
33 | XSLoader::load Digest::Hashcash, $VERSION; |
34 | XSLoader::load Digest::Hashcash, $VERSION; |
34 | |
35 | |
|
|
36 | =item $secs = estimate_time $size |
|
|
37 | |
|
|
38 | Estimate the average time necessary to calculate a token of the given |
|
|
39 | size. |
|
|
40 | |
|
|
41 | See also C<estimate_size>. |
|
|
42 | |
|
|
43 | =item $size = estimate_size $time[, $min] |
|
|
44 | |
|
|
45 | Estimate the size that can be calculated in the given time (which is an |
|
|
46 | upper bound). The function will not return a size less then C<min>. |
|
|
47 | |
|
|
48 | Estimating the time to be used can go wrong by as much as 50% (but is |
|
|
49 | usually quite accurate), and the estimation itself can take as much as a |
|
|
50 | second on slower (<pentium) machines, but faster machines (1Ghz P3 for |
|
|
51 | example) usually handle it within a hundredth of a second or so. |
|
|
52 | |
|
|
53 | The estimation will be done only once, so you can call this fucntion as |
|
|
54 | often as you like without incuring the overhead everytime. |
|
|
55 | |
|
|
56 | =cut |
|
|
57 | |
|
|
58 | my $rounds; |
|
|
59 | |
|
|
60 | sub _rounds { |
|
|
61 | $rounds ||= &_estimate_rounds(); |
|
|
62 | } |
|
|
63 | |
|
|
64 | sub estimate_time { |
|
|
65 | my ($size) = @_; |
|
|
66 | 2**$size / &_rounds; |
|
|
67 | } |
|
|
68 | |
|
|
69 | sub estimate_size { |
|
|
70 | my ($time, $min) = @_; |
|
|
71 | $time = (log $time * $rounds) / log 2; |
|
|
72 | $time < $min ? $min : int $time; |
|
|
73 | } |
|
|
74 | |
35 | =item $cipher = new [param => value...] |
75 | =item $cipher = new [param => value...] |
36 | |
76 | |
37 | =over 4 |
77 | =over 4 |
38 | |
78 | |
39 | =item size => 20 + some |
79 | =item size => 18 |
40 | |
80 | |
41 | The number of collisions, in bits. Every bit increases the time to create |
81 | The number of collisions, in bits. Every bit increases the time to create |
42 | the token (and thus the cash) by two. |
82 | the token (and thus the cash) by two. |
43 | |
83 | |
44 | =item uid => "" |
84 | =item uid => "" |