… | |
… | |
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 = 1.1; |
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 Digest::Hashcash [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 => "" |
… | |
… | |
61 | The timestamp to use. A value of 0 (the default) means to use the current |
101 | The timestamp to use. A value of 0 (the default) means to use the current |
62 | time. |
102 | time. |
63 | |
103 | |
64 | =back |
104 | =back |
65 | |
105 | |
66 | =item $token = $cipher->hash($data [, param => value...]) |
106 | =item $token = $cipher->hash ($data [, param => value...]) |
67 | |
107 | |
68 | Creates and returns a new token. This can take some time. |
108 | Creates and returns a new token. This can take some time. |
69 | |
109 | |
70 | Any additional parameters are interpreted the same way as arguments to |
110 | Any additional parameters are interpreted the same way as arguments to |
71 | C<new>. |
111 | C<new>. |
72 | |
112 | |
73 | =item $prefix = $cipher->verify($token [, param => value...])) |
113 | =item $prefix = $cipher->verify ($token [, param => value...])) |
74 | |
114 | |
75 | Checks the given token and returns true if the token has the minimum |
115 | Checks the given token and returns true if the token has the minimum |
76 | number of prefix bits, or false otherwise. The value returned is actually |
116 | number of prefix bits, or false otherwise. The value returned is actually |
77 | the number of collisions, so to find the number of collisions bits specify |
117 | the number of collisions, so to find the number of collisions bits specify |
78 | C<< collisions => 0 >>. |
118 | C<< collisions => 0 >>. |
79 | |
119 | |
80 | Any additional parameters are interpreted the same way as arguments to |
120 | Any additional parameters are interpreted the same way as arguments to |
81 | C<new>. |
121 | C<new>. |
82 | |
122 | |
83 | =item $resource = $cipher->resource($token) |
123 | =item $resource = $cipher->resource ($token) |
84 | |
124 | |
85 | Returns the resource part, or C<undef>. |
125 | Returns the resource part, or C<undef>. |
86 | |
126 | |
87 | =item $tstamp = $ciper->timestamp($token) |
127 | =item $tstamp = $ciper->timestamp ($token) |
88 | |
128 | |
89 | Returns the timestamp part (in the same format as perl's C<time>), or |
129 | Returns the timestamp part (in the same format as perl's C<time>), or |
90 | C<undef>. |
130 | C<undef>. |
91 | |
131 | |
92 | =back |
132 | =back |
… | |
… | |
146 | |
186 | |
147 | =head1 SEE ALSO |
187 | =head1 SEE ALSO |
148 | |
188 | |
149 | L<http://www.hashcash.org>. |
189 | L<http://www.hashcash.org>. |
150 | |
190 | |
|
|
191 | =head1 SUPPORT FOR THE PERL MULTICORE SPECIFICATION |
|
|
192 | |
|
|
193 | This module supports the perl multicore specification |
|
|
194 | (<http://perlmulticore.schmorp.de/>) for token generation of any length |
|
|
195 | and size. |
|
|
196 | |
151 | =head1 BUGS |
197 | =head1 BUGS |
152 | |
198 | |
153 | * There is a y2k+100 problem, as I always assume the same as Time::Local. |
199 | * There is a y2k+100 problem, as I always assume the same as Time::Local. |
154 | This is a problem with the hashcash specification, which specifies |
200 | This is a problem with the hashcash specification, which specifies |
155 | yeras as 2 digits :( |
201 | years as 2 digits :( |
156 | |
202 | |
157 | =head1 AUTHOR |
203 | =head1 AUTHOR |
158 | |
204 | |
159 | Marc Lehmann <pcg@goof.com> |
205 | Marc Lehmann <schmorp@schmorp.de> |
160 | http://home.schmorp.de |
206 | http://home.schmorp.de |
161 | |
207 | |
162 | =cut |
208 | =cut |
163 | |
209 | |
164 | 1; |
210 | 1; |