… | |
… | |
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 | |
26 | use Time::HiRes; |
27 | require XSLoader; |
|
|
28 | |
27 | |
29 | no warnings; |
28 | no warnings; |
30 | |
29 | |
31 | $VERSION = 0.01; |
30 | BEGIN { |
|
|
31 | our $VERSION = 1.1; |
32 | |
32 | |
|
|
33 | require XSLoader; |
33 | XSLoader::load Digest::Hashcash, $VERSION; |
34 | XSLoader::load Digest::Hashcash, $VERSION; |
|
|
35 | } |
34 | |
36 | |
|
|
37 | =item $secs = estimate_time $size |
|
|
38 | |
|
|
39 | Estimate the average time necessary to calculate a token of the given |
|
|
40 | size. |
|
|
41 | |
|
|
42 | See also C<estimate_size>. |
|
|
43 | |
|
|
44 | =item $size = estimate_size $time[, $min] |
|
|
45 | |
|
|
46 | Estimate the size that can be calculated in the given time (which is an |
|
|
47 | upper bound). The function will not return a size less then C<min>. |
|
|
48 | |
|
|
49 | Estimating the time to be used can go wrong by as much as 50% (but is |
|
|
50 | usually quite accurate), and the estimation itself can take as much as a |
|
|
51 | second on slower (<pentium) machines, but faster machines (1Ghz P3 for |
|
|
52 | example) usually handle it within a hundredth of a second or so. |
|
|
53 | |
|
|
54 | The estimation will be done only once, so you can call this function as |
|
|
55 | often as you like without incuring the overhead everytime. |
|
|
56 | |
|
|
57 | =cut |
|
|
58 | |
|
|
59 | my $rounds; |
|
|
60 | |
|
|
61 | sub _rounds() { |
|
|
62 | $rounds ||= _estimate_rounds |
|
|
63 | } |
|
|
64 | |
|
|
65 | sub estimate_time($) { |
|
|
66 | my ($size) = @_; |
|
|
67 | |
|
|
68 | 2**$size / _rounds |
|
|
69 | } |
|
|
70 | |
|
|
71 | sub estimate_size($$) { |
|
|
72 | my ($time, $min) = @_; |
|
|
73 | |
|
|
74 | $time = (log $time * _rounds) / log 2; |
|
|
75 | $time < $min ? $min : int $time |
|
|
76 | } |
|
|
77 | |
35 | =item $cipher = new [param => value...] |
78 | =item $cipher = new Digest::Hashcash [param => value...] |
36 | |
79 | |
37 | =over 4 |
80 | =over 4 |
38 | |
81 | |
39 | =item size => 20 + some |
82 | =item size => 18 |
40 | |
83 | |
41 | The number of collisions, in bits. Every bit increases the time to create |
84 | The number of collisions, in bits. Every bit increases the time to create |
42 | the token (and thus the cash) by two. |
85 | the token (and thus the cash) by two. |
43 | |
86 | |
44 | =item uid => "" |
87 | =item uid => "" |
… | |
… | |
61 | The timestamp to use. A value of 0 (the default) means to use the current |
104 | The timestamp to use. A value of 0 (the default) means to use the current |
62 | time. |
105 | time. |
63 | |
106 | |
64 | =back |
107 | =back |
65 | |
108 | |
66 | =item $token = $cipher->hash($data [, param => value...]) |
109 | =item $token = $cipher->hash ($data [, param => value...]) |
67 | |
110 | |
68 | Creates and returns a new token. This can take some time. |
111 | Creates and returns a new token. This can take some time. |
69 | |
112 | |
70 | Any additional parameters are interpreted the same way as arguments to |
113 | Any additional parameters are interpreted the same way as arguments to |
71 | C<new>. |
114 | C<new>. |
72 | |
115 | |
73 | =item $prefix = $cipher->verify($token [, param => value...])) |
116 | =item $prefix = $cipher->verify ($token [, param => value...])) |
74 | |
117 | |
75 | Checks the given token and returns true if the token has the minimum |
118 | 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 |
119 | 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 |
120 | the number of collisions, so to find the number of collisions bits specify |
78 | C<< collisions => 0 >>. |
121 | C<< collisions => 0 >>. |
79 | |
122 | |
80 | Any additional parameters are interpreted the same way as arguments to |
123 | Any additional parameters are interpreted the same way as arguments to |
81 | C<new>. |
124 | C<new>. |
82 | |
125 | |
83 | =item $resource = $cipher->resource($token) |
126 | =item $resource = $cipher->resource ($token) |
84 | |
127 | |
85 | Returns the resource part, or C<undef>. |
128 | Returns the resource part, or C<undef>. |
86 | |
129 | |
87 | =item $tstamp = $ciper->timestamp($token) |
130 | =item $tstamp = $ciper->timestamp ($token) |
88 | |
131 | |
89 | Returns the timestamp part (in the same format as perl's C<time>), or |
132 | Returns the timestamp part (in the same format as perl's C<time>), or |
90 | C<undef>. |
133 | C<undef>. |
91 | |
134 | |
92 | =back |
135 | =back |
… | |
… | |
101 | |
144 | |
102 | sub hash { |
145 | sub hash { |
103 | my $self = shift; |
146 | my $self = shift; |
104 | my %arg = (%$self, resource => @_); |
147 | my %arg = (%$self, resource => @_); |
105 | |
148 | |
106 | &_gentoken(@arg{qw(size timestamp resource uid extrarand)}); |
149 | &_gentoken (@arg{qw(size timestamp resource uid extrarand)}) |
107 | } |
150 | } |
108 | |
151 | |
109 | sub verify { |
152 | sub verify { |
110 | my ($self, $token) = (shift, shift); |
153 | my ($self, $token) = (shift, shift); |
111 | my %arg = (%$self, @_); |
154 | my %arg = (%$self, @_); |
112 | |
155 | |
113 | my $prefix = &_prefixlen($token); |
156 | my $prefix = _prefixlen $token; |
114 | |
157 | |
115 | $prefix < $arg{size} |
158 | $prefix < $arg{size} |
116 | ? undef |
159 | ? undef |
117 | : $prefix; |
160 | : $prefix |
118 | } |
161 | } |
119 | |
162 | |
120 | sub resource { |
163 | sub resource { |
121 | my ($self, $token) = @_; |
164 | my ($self, $token) = @_; |
122 | |
165 | |
123 | $token =~ /^\d+:\d*:(.*):/ |
166 | $token =~ /^\d+:\d*:(.*):/ |
124 | or return undef; |
167 | or return undef; |
125 | |
168 | |
126 | return $1; |
169 | $1 |
127 | } |
170 | } |
128 | |
171 | |
129 | sub timestamp { |
172 | sub timestamp { |
130 | my ($self, $token) = @_; |
173 | my ($self, $token) = @_; |
131 | |
174 | |
… | |
… | |
139 | $d = /\G(\d\d)/gc ? $1 : 1; |
182 | $d = /\G(\d\d)/gc ? $1 : 1; |
140 | $H = /\G(\d\d)/gc ? $1 : 0; |
183 | $H = /\G(\d\d)/gc ? $1 : 0; |
141 | $M = /\G(\d\d)/gc ? $1 : 0; |
184 | $M = /\G(\d\d)/gc ? $1 : 0; |
142 | $S = /\G(\d\d)/gc ? $1 : 0; |
185 | $S = /\G(\d\d)/gc ? $1 : 0; |
143 | |
186 | |
144 | return timegm $S, $M, $H, $d, $m - 1, $y; |
187 | timegm $S, $M, $H, $d, $m - 1, $y |
145 | } |
188 | } |
146 | |
189 | |
147 | =head1 SEE ALSO |
190 | =head1 SEE ALSO |
148 | |
191 | |
149 | L<http://www.hashcash.org>. |
192 | L<http://www.hashcash.org>. |
|
|
193 | |
|
|
194 | =head1 SUPPORT FOR THE PERL MULTICORE SPECIFICATION |
|
|
195 | |
|
|
196 | This module supports the perl multicore specification |
|
|
197 | (<http://perlmulticore.schmorp.de/>) for token generation of any length |
|
|
198 | and size. |
150 | |
199 | |
151 | =head1 BUGS |
200 | =head1 BUGS |
152 | |
201 | |
153 | * There is a y2k+100 problem, as I always assume the same as Time::Local. |
202 | * 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 |
203 | This is a problem with the hashcash specification, which specifies |
155 | yeras as 2 digits :( |
204 | years as 2 digits :( |
156 | |
205 | |
157 | =head1 AUTHOR |
206 | =head1 AUTHOR |
158 | |
207 | |
159 | Marc Lehmann <pcg@goof.com> |
208 | Marc Lehmann <schmorp@schmorp.de> |
160 | http://home.schmorp.de |
209 | http://home.schmorp.de |
161 | |
210 | |
162 | =cut |
211 | =cut |
163 | |
212 | |
164 | 1; |
213 | 1; |