File Coverage

blib/lib/Digest/QuickXor.pm
Criterion Covered Total %
statement 64 64 100.0
branch 3 4 75.0
condition n/a
subroutine 15 15 100.0
pod 6 6 100.0
total 88 89 98.8


line stmt bran cond sub pod time code
1             package Digest::QuickXor;
2 4     4   262955 use parent qw|DynaLoader|;
  4         1248  
  4         22  
3              
4 4     4   202 use strict;
  4         7  
  4         65  
5 4     4   16 use warnings;
  4         6  
  4         96  
6 4     4   17 use utf8;
  4         6  
  4         20  
7 4     4   100 use v5.24;
  4         12  
8 4     4   22 use feature 'signatures';
  4         5  
  4         598  
9 4     4   21 no warnings 'experimental::signatures';
  4         7  
  4         173  
10              
11 4     4   31 use Carp 'croak';
  4         8  
  4         208  
12 4     4   21 use Exporter 'import';
  4         6  
  4         1962  
13             our @EXPORT_OK = qw|quickxorhash|;
14              
15             our $VERSION = '0.03';
16              
17             __PACKAGE__->bootstrap($VERSION);
18              
19             # functions
20              
21 1     1 1 1506 sub quickxorhash (@data) {
  1         3  
  1         2  
22 1         4 return __PACKAGE__->new->add(@data)->b64digest;
23             }
24              
25             # constructor
26              
27 4     4 1 2357 sub new ($class) {
  4         10  
  4         17  
28 4         35 my $qx = Digest::QuickXor::HashPtr->new();
29 4         12 my $self = {_qx => $qx};
30              
31 4         21 return bless $self, $class;
32             }
33              
34             # methods
35              
36 14     14 1 20 sub add ($self, @data) {
  14         17  
  14         148  
  14         16  
37 14         27 for (@data) {
38 18         866 $self->{_qx}->add($_, length $_);
39             }
40              
41 14         58 return $self;
42             }
43              
44 12     12 1 1383 sub addfile ($self, $fh) {
  12         14  
  12         17  
  12         15  
45 12 100       553 croak 'Not a file handle!' unless $fh->can('sysread');
46              
47 8         13 my $ret = '';
48 8         22 while ($ret = $fh->sysread(my $buffer, 131072, 0)) {
49 8         256 $self->add($buffer);
50             }
51 8 50       100 croak qq|Can't read from file: $!| unless defined $ret;
52              
53 8         22 return $self;
54             }
55              
56 12     12 1 19 sub b64digest ($self) {
  12         14  
  12         12  
57 12         62 my $rv = $self->{_qx}->b64digest;
58 12         31 $self->reset;
59              
60 12         50 return $rv;
61             }
62              
63 12     12 1 14 sub reset ($self) {
  12         15  
  12         14  
64 12         31 $self->{_qx}->reset;
65              
66 12         14 return $self;
67             }
68              
69             1;
70              
71             =encoding utf8
72              
73             =head1 NAME
74              
75             Digest::QuickXor - The QuickXorHash
76              
77             =head1 SYNOPSIS
78              
79             use Digest::QuickXor;
80              
81             my $qx = Digest::QuickXor->new;
82              
83             $qx->add(@data);
84             $qx->b64digest;
85              
86             $qx->addfile($filehandle);
87             $qx->b64digest;
88              
89             $qx->add($wrong_data);
90             $qx->reset;
91             $qx->add($correct_data);
92              
93             # use as function
94             use Digest::QuickXor 'quickxorhash';
95              
96             my $hash = quickxorhash(@data);
97              
98             =head1 DESCRIPTION
99              
100             L implements the QuickXorHash.
101              
102             The QuickXorHash is the digest used by Microsoft on Office 365 OneDrive for Business and Sharepoint.
103             It was published by Microsoft in 2016 in form of a C# script. The explanation describes it as a
104             "quick, simple non-cryptographic hash algorithm that works by XORing the bytes in a circular-shifting fashion".
105              
106             =head2 FUNCTIONS
107              
108             None of the functions is exported by default.
109              
110             =head2 quickxorhash
111              
112             use Digest::QuickXor 'quickxorhash';
113              
114             my $hash = quickxorhash(@data);
115              
116             Returns the digest for the provided data.
117              
118             =head1 CONSTRUCTOR
119              
120             =head2 new
121              
122             $qx = Digest::QuickXor->new;
123              
124             =head1 METHODS
125              
126             =head2 add
127              
128             $qx = $qx->add($data);
129             $qx = $qx->add(@data);
130              
131             Adds new blocks of data.
132              
133             =head2 addfile
134              
135             $qx = $qx->addfile($filehandle);
136              
137             Adds data from a file handle.
138              
139             =head2 b64digest
140              
141             $string = $qx->b64digest;
142              
143             Returns the digest and resets the object.
144              
145             =head2 reset
146              
147             $qx = $qx->reset;
148              
149             Resets the object so it is ready to accept new data.
150              
151             =head1 AUTHOR & COPYRIGHT
152              
153             © 2019 by Tekki (Rolf Stöckli).
154              
155             © for the original algorithm 2016 by Microsoft.
156              
157             This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
158              
159             =head1 SEE ALSO
160              
161             Explanation of the
162             L
163             in the OneDrive Dev Center.
164              
165             L
166             by Ryan Gregg.
167              
168             L, C implementation of the hash, base code for this module.
169              
170             =cut