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.3 by root, Sun May 30 21:59:43 2004 UTC vs.
Revision 1.8 by root, Wed Jul 22 12:09:48 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines