File Coverage

blib/lib/Digest/SipHash.pm
Criterion Covered Total %
statement 34 34 100.0
branch 4 6 66.6
condition 2 6 33.3
subroutine 10 10 100.0
pod 1 1 100.0
total 51 57 89.4


line stmt bran cond sub pod time code
1             package Digest::SipHash;
2              
3 2     2   57723 use 5.008001;
  2         8  
  2         96  
4 2     2   18 use strict;
  2         9  
  2         66  
5 2     2   10 use warnings;
  2         12  
  2         207  
6              
7             our $VERSION = sprintf "%d.%02d", q$Revision: 0.19 $ =~ /(\d+)/g;
8             require XSLoader;
9             XSLoader::load( 'Digest::SipHash', $VERSION );
10              
11 2     2   11 use base 'Exporter';
  2         4  
  2         255  
12             our @EXPORT_OK = qw/siphash siphash32/;
13              
14 2     2   12 use constant BIG_ENDIAN => pack( "L", 1 ) eq pack( "N", 1 );
  2         4  
  2         160  
15 2     2   10 use constant USE64BITINT => eval { pack 'Q', 1 };
  2         3  
  2         4  
  2         304  
16              
17             push @EXPORT_OK, 'siphash64' if USE64BITINT;
18             our %EXPORT_TAGS = ( all => [@EXPORT_OK] );
19             our $DEFAULT_SEED = pack 'C16', map { int( rand(256) ) } ( 0 .. 0xF );
20              
21             sub siphash {
22 3     3 1 216 my $str = shift;
23 3   33     12 my $seed = shift || $DEFAULT_SEED;
24 2     2   2037 use bytes;
  2         23  
  2         10  
25 3 50       9 $seed .= substr( $DEFAULT_SEED, length($seed) ) if length($seed) < 16;
26 3         15 my $lohi = _xs_siphash_av( $str, $seed );
27 3 100       18 return wantarray ? @$lohi : $lohi->[0];
28             }
29              
30             *siphash32 = \&siphash;
31              
32             if (USE64BITINT) {
33             *siphash64 = sub {
34 1     1   247 my $str = shift;
35 1   33     7 my $seed = shift || $DEFAULT_SEED;
36 2     2   290 use bytes;
  2         3  
  2         10  
37 1 50       5 $seed .= substr( $DEFAULT_SEED, length($seed) ) if length($seed) < 16;
38 1         5 return _xs_siphash64( $str, $seed );
39             };
40             }
41              
42             1;
43              
44             =head1 NAME
45              
46             Digest::SipHash - Perl XS interface to the SipHash algorithm
47              
48             =head1 VERSION
49              
50             $Id: SipHash.pm,v 0.19 2013/03/05 09:07:53 dankogai Exp $
51              
52             =head1 SYNOPSIS
53              
54             use Digest::SipHash qw/siphash/;
55             my $seed = pack 'C16', 0 .. 0xF; # 16 chars long
56             my $str = "hello world!";
57             my ( $lo, $hi ) = siphash( $str, $seed );
58             # $lo = 0x10cf32e0, $hi == 0x7da9cd17
59             my $u32 = siphash( $str, $seed )
60             # $u32 = 0x10cf32e0
61              
62             use Config;
63             if ( $Config{use64bitint} ) {
64             use Digest::SipHash qw/siphash64/;
65             my $uint64 = siphash64( $str, $seed ); # scalar context;
66             # $uint64 == 0x7da9cd1710cf32e0
67             }
68              
69             =head1 DESCRIPTION
70              
71             SipHash is the default perl hash function for 64 bit builds now.
72              
73             L
74              
75             L
76              
77             This module does only one thing - culculates the SipHash value of the
78             given string.
79              
80             =head1 EXPORT
81              
82             C, C and C on demand.
83              
84             C<:all> to all of above
85              
86             =head1 SUBROUTINES/METHODS
87              
88             =head2 siphash
89              
90             my ($hi, $lo) = siphash($str [, $seed]);
91             my $uint32 = siphash($str [, $seed]);
92              
93             Calculates the SipHash value of C<$src> with $<$seed>.
94              
95             If C<$seed> is omitted, it defaults to C<$Digest:::SipHash::DEFAULT_SEED>,
96             which is set randomly upon initialization of this module.
97              
98             If C<$seed> is set but less than 16 bytes long, it is padded with C<$DEFAULT_SEED>.
99              
100             To be compatible with 32-bit perl, It returns a pair of 32-bit
101             integers instead of a 64-bit integer. Since C
102             always returns the lower 32-bit first so that:
103              
104             use Hash::Util qw/hash_seed hash_value/;
105             use Digest::SipHash qw/siphash/;
106             hash_value($str) == siphash($str, hash_seed()); # scalar context
107              
108             always holds true when PERL_HASH_FUN_SIPHASH is in effect.
109              
110             =head2 siphash32
111              
112             just an alias of C.
113              
114             =head2 siphash64
115              
116             my $uint64 = siphash64($str [, $seed]);
117              
118             Calculates the SipHash value of C<$src> with C<$seed> in 64-bit.
119             Available on 64-bit platforms only.
120              
121             =head1 AUTHOR
122              
123             Dan Kogai, C<< >>
124              
125             =head1 BUGS
126              
127             Please report any bugs or feature requests to C, or through
128             the web interface at L. I will be notified, and then you'll
129             automatically be notified of progress on your bug as I make changes.
130              
131             =head1 SUPPORT
132              
133             You can find documentation for this module with the perldoc command.
134              
135             perldoc Digest::SipHash
136              
137             You can also look for information at:
138              
139             =over 4
140              
141             =item * RT: CPAN's request tracker (report bugs here)
142              
143             L
144              
145             =item * AnnoCPAN: Annotated CPAN documentation
146              
147             L
148              
149             =item * CPAN Ratings
150              
151             L
152              
153             =item * Search CPAN
154              
155             L
156              
157             =back
158              
159             =head1 SEE ALSO
160              
161             L, L
162              
163             =head1 ACKNOWLEDGEMENTS
164              
165             B
166              
167             by Jean-Philippe Aumasson & Daniel J. Bernstein
168              
169             L
170              
171             =head1 LICENSE AND COPYRIGHT
172              
173             =head2 csiphash.c
174              
175             Copyright (c) 2013 Marek Majkowski
176              
177             MIT License L
178              
179             L
180              
181             =head2 The rest of this module
182              
183             Copyright 2013 Dan Kogai.
184              
185             This program is free software; you can redistribute it and/or modify it
186             under the terms of the the Artistic License (2.0). You may obtain a
187             copy of the full license at:
188              
189             L
190              
191             Any use, modification, and distribution of the Standard or Modified
192             Versions is governed by this Artistic License. By using, modifying or
193             distributing the Package, you accept this license. Do not use, modify,
194             or distribute the Package, if you do not accept this license.
195              
196             If your Modified Version has been derived from a Modified Version made
197             by someone other than you, you are nevertheless required to ensure that
198             your Modified Version complies with the requirements of this license.
199              
200             This license does not grant you the right to use any trademark, service
201             mark, tradename, or logo of the Copyright Holder.
202              
203             This license includes the non-exclusive, worldwide, free-of-charge
204             patent license to make, have made, use, offer to sell, sell, import and
205             otherwise transfer the Package with respect to any patent claims
206             licensable by the Copyright Holder that are necessarily infringed by the
207             Package. If you institute patent litigation (including a cross-claim or
208             counterclaim) against any party alleging that the Package constitutes
209             direct or contributory patent infringement, then this Artistic License
210             to you shall terminate on the date that such litigation is filed.
211              
212             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
213             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
214             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
215             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
216             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
217             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
218             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
219             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
220              
221             =cut
222              
223             1; # End of Digest::SipHash