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.7 by root, Wed Jul 22 10:33:08 2015 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
30BEGIN {
32$VERSION = 1.1; 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 Digest::Hashcash [param => value...] 78=item $cipher = new Digest::Hashcash [param => value...]
76 79
77=over 4 80=over 4
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>.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines