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.8 by root, Wed Jul 22 12:09:48 2015 UTC

21=cut 21=cut
22 22
23package Digest::Hashcash; 23package Digest::Hashcash;
24 24
25use Time::Local; 25use Time::Local;
26 26use Time::HiRes;
27require XSLoader;
28 27
29no warnings; 28no warnings;
30 29
31$VERSION = 0.01; 30BEGIN {
31 our $VERSION = 1.1;
32 32
33 require XSLoader;
33XSLoader::load Digest::Hashcash, $VERSION; 34 XSLoader::load Digest::Hashcash, $VERSION;
35}
34 36
37=item $secs = estimate_time $size
38
39Estimate the average time necessary to calculate a token of the given
40size.
41
42See also C<estimate_size>.
43
44=item $size = estimate_size $time[, $min]
45
46Estimate the size that can be calculated in the given time (which is an
47upper bound). The function will not return a size less then C<min>.
48
49Estimating the time to be used can go wrong by as much as 50% (but is
50usually quite accurate), and the estimation itself can take as much as a
51second on slower (<pentium) machines, but faster machines (1Ghz P3 for
52example) usually handle it within a hundredth of a second or so.
53
54The estimation will be done only once, so you can call this function as
55often as you like without incuring the overhead everytime.
56
57=cut
58
59my $rounds;
60
61sub _rounds() {
62 $rounds ||= _estimate_rounds
63}
64
65sub estimate_time($) {
66 my ($size) = @_;
67
68 2**$size / _rounds
69}
70
71sub 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
41The number of collisions, in bits. Every bit increases the time to create 84The number of collisions, in bits. Every bit increases the time to create
42the token (and thus the cash) by two. 85the token (and thus the cash) by two.
43 86
44=item uid => "" 87=item uid => ""
61The 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
62time. 105time.
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
68Creates and returns a new token. This can take some time. 111Creates and returns a new token. This can take some time.
69 112
70Any additional parameters are interpreted the same way as arguments to 113Any additional parameters are interpreted the same way as arguments to
71C<new>. 114C<new>.
72 115
73=item $prefix = $cipher->verify($token [, param => value...])) 116=item $prefix = $cipher->verify ($token [, param => value...]))
74 117
75Checks 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
76number of prefix bits, or false otherwise. The value returned is actually 119number of prefix bits, or false otherwise. The value returned is actually
77the 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
78C<< collisions => 0 >>. 121C<< collisions => 0 >>.
79 122
80Any additional parameters are interpreted the same way as arguments to 123Any additional parameters are interpreted the same way as arguments to
81C<new>. 124C<new>.
82 125
83=item $resource = $cipher->resource($token) 126=item $resource = $cipher->resource ($token)
84 127
85Returns the resource part, or C<undef>. 128Returns the resource part, or C<undef>.
86 129
87=item $tstamp = $ciper->timestamp($token) 130=item $tstamp = $ciper->timestamp ($token)
88 131
89Returns 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
90C<undef>. 133C<undef>.
91 134
92=back 135=back
101 144
102sub hash { 145sub 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
109sub verify { 152sub 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
120sub resource { 163sub 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
129sub timestamp { 172sub 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
149L<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.
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
1641; 2131;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines