ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Digest-Hashcash/Hashcash.pm
(Generate patch)

Comparing Digest-Hashcash/Hashcash.pm (file contents):
Revision 1.1 by root, Sat Sep 6 22:12:00 2003 UTC vs.
Revision 1.7 by root, Wed Jul 22 10:33:08 2015 UTC

21=cut 21=cut
22 22
23package Digest::Hashcash; 23package Digest::Hashcash;
24 24
25use Time::Local; 25use Time::Local;
26use Time::HiRes;
26 27
27require XSLoader; 28require XSLoader;
28 29
29no warnings; 30no warnings;
30 31
31$VERSION = 0.01; 32$VERSION = 1.1;
32 33
33XSLoader::load Digest::Hashcash, $VERSION; 34XSLoader::load Digest::Hashcash, $VERSION;
34 35
36=item $secs = estimate_time $size
37
38Estimate the average time necessary to calculate a token of the given
39size.
40
41See also C<estimate_size>.
42
43=item $size = estimate_size $time[, $min]
44
45Estimate the size that can be calculated in the given time (which is an
46upper bound). The function will not return a size less then C<min>.
47
48Estimating the time to be used can go wrong by as much as 50% (but is
49usually quite accurate), and the estimation itself can take as much as a
50second on slower (<pentium) machines, but faster machines (1Ghz P3 for
51example) usually handle it within a hundredth of a second or so.
52
53The estimation will be done only once, so you can call this fucntion as
54often as you like without incuring the overhead everytime.
55
56=cut
57
58my $rounds;
59
60sub _rounds {
61 $rounds ||= &_estimate_rounds();
62}
63
64sub estimate_time {
65 my ($size) = @_;
66 2**$size / &_rounds;
67}
68
69sub 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
41The number of collisions, in bits. Every bit increases the time to create 81The number of collisions, in bits. Every bit increases the time to create
42the token (and thus the cash) by two. 82the token (and thus the cash) by two.
43 83
44=item uid => "" 84=item uid => ""
61The timestamp to use. A value of 0 (the default) means to use the current 101The timestamp to use. A value of 0 (the default) means to use the current
62time. 102time.
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
68Creates and returns a new token. This can take some time. 108Creates and returns a new token. This can take some time.
69 109
70Any additional parameters are interpreted the same way as arguments to 110Any additional parameters are interpreted the same way as arguments to
71C<new>. 111C<new>.
72 112
73=item $prefix = $cipher->verify($token [, param => value...])) 113=item $prefix = $cipher->verify ($token [, param => value...]))
74 114
75Checks the given token and returns true if the token has the minimum 115Checks the given token and returns true if the token has the minimum
76number of prefix bits, or false otherwise. The value returned is actually 116number of prefix bits, or false otherwise. The value returned is actually
77the number of collisions, so to find the number of collisions bits specify 117the number of collisions, so to find the number of collisions bits specify
78C<< collisions => 0 >>. 118C<< collisions => 0 >>.
79 119
80Any additional parameters are interpreted the same way as arguments to 120Any additional parameters are interpreted the same way as arguments to
81C<new>. 121C<new>.
82 122
83=item $resource = $cipher->resource($token) 123=item $resource = $cipher->resource ($token)
84 124
85Returns the resource part, or C<undef>. 125Returns the resource part, or C<undef>.
86 126
87=item $tstamp = $ciper->timestamp($token) 127=item $tstamp = $ciper->timestamp ($token)
88 128
89Returns the timestamp part (in the same format as perl's C<time>), or 129Returns the timestamp part (in the same format as perl's C<time>), or
90C<undef>. 130C<undef>.
91 131
92=back 132=back
146 186
147=head1 SEE ALSO 187=head1 SEE ALSO
148 188
149L<http://www.hashcash.org>. 189L<http://www.hashcash.org>.
150 190
191=head1 SUPPORT FOR THE PERL MULTICORE SPECIFICATION
192
193This module supports the perl multicore specification
194(<http://perlmulticore.schmorp.de/>) for token generation of any length
195and 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
1641; 2101;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines