Passphrase/MySQL323.pm

filter: Statementstmt 98.0% 49 / 50 Branchbran 68.1% 15 / 22 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 12 / 12 Podpod 100% 4 / 4 totaltotal 80.5% 83 / 103 CC 15 SCAR 24.8
CC
15
Cov
83%
new
19.9
BEGIN
1.0
BEGIN
1.0
CRAP 11.9
« Passphrase/NetscapeMail.pm ↑ Summary Passphrase/MySQL41.pm »
1 =head1 NAME
2  
3 Authen::Passphrase::MySQL323 - passphrases using the MySQL v3.23 algorithm
4  
5 =head1 SYNOPSIS
6  
7 use Authen::Passphrase::MySQL323;
8  
9 $ppr = Authen::Passphrase::MySQL323->new(
10 hash_hex => "2af8a0a82c8f9086");
11  
12 $ppr = Authen::Passphrase::MySQL323->new(
13 passphrase => "passphrase");
14  
15 $hash = $ppr->hash;
16 $hash_hex = $ppr->hash_hex;
17  
18 if($ppr->match($passphrase)) { ...
19  
20 =head1 DESCRIPTION
21  
22 An object of this class encapsulates a passphrase hashed using the
23 algorithm used by MySQL from version 3.23. This is a subclass of
24 L<Authen::Passphrase>, and this document assumes that the reader is
25 familiar with the documentation for that class.
26  
27 The MySQL v3.23 hash scheme is composed entirely of linear operations.
28 It accepts an arbitrarily long passphrase, and ignores all space
29 and tab characters. No salt is used. 62 bits of hash are generated.
30 Each character influences only a minority of the result bits, so similar
31 passphrases of the same length have noticeably similar hashes.
32  
33 In MySQL the hash is represented as a string of sixteen lowercase
34 hexadecimal digits.
35  
36 I<Warning:> This is not a serious cryptographic algorithm. Do not use
37 for any security purpose.
38  
39 =cut
40  
41 package Authen::Passphrase::MySQL323;
42  
43 2 ▶{ use 5.006; }
Subroutine BEGIN: called CC=1 SCAR=0.0
44 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 2 ▶use Authen::Passphrase 0.003;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
49 2 ▶use Crypt::MySQL 0.03 qw(password);
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 2 our $VERSION = "0.009";
52  
53 2 ▶use parent "Authen::Passphrase";
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 =head1 CONSTRUCTOR
56  
57 =over
58  
59 =item Authen::Passphrase::MySQL323->new(ATTR => VALUE, ...)
60  
61 Generates a new passphrase recogniser object using the MySQL v3.23
62 algorithm. The following attributes may be given:
63  
64 =over
65  
66 =item B<hash>
67  
68 The hash, as a string of eight bytes. The first and fifth bytes must
69 have their top bit clear.
70  
71 =item B<hash_hex>
72  
73 The hash, as a string of 16 hexadecimal digits.
74  
75 =item B<passphrase>
76  
77 A passphrase that will be accepted.
78  
79 =back
80  
81 Either the hash or the passphrase must be given.
82  
83 =cut
84  
85 6 ▶sub new {
Subroutine new: called CC=15 SCAR=29.9
Pod documented
86 6 my $class = shift;
87 6 my $self = bless({}, $class);
88 6 my $passphrase;
89 6 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 6 12
90 6 my $attr = shift;
91 6 my $value = shift;
92 6 ▶ if($attr eq "hash") {
Branch: 3 branches
BranchTrueFalseTotal
if ($attr eq 'hash') { } 2 4 6
elsif ($attr eq 'hash_hex') { } 3 1 4
elsif ($attr eq 'passphrase') { } 1 0 1
93 croak "hash specified redundantly"
94 2 ▶ if exists($self->{hash}) ||
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"hash"} or defined $passphrase 0 2 2
Condition33%1/3
exists $self->{"hash"} or defined $passphrase
l!l&&r!l&&!r
002
MC/DC0%0/2
exists $self->{"hash"} or defined $passphrase
exists $self->{"hash"} defined $passphrase
Truth table33%1/3
A or B
A= exists $self->{"hash"}B= defined $passphrase
ABresult
1X1
011
000
95 defined($passphrase);
96 2 ▶ $value =~ m#\A(?:[\x00-\x7f][\x00-\xff]{3}){2}\z#
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /\A(?:[\x00-\x7f][\x00-\xff]{3}){2}\z/ or croak("not a valid MySQL v3.23 hash") 2 0 2
97 or croak "not a valid MySQL v3.23 hash";
98 2 $self->{hash} = "$value";
99 } elsif($attr eq "hash_hex") {
100 croak "hash specified redundantly"
101 3 ▶ if exists($self->{hash}) ||
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"hash"} or defined $passphrase 0 3 3
Condition33%1/3
exists $self->{"hash"} or defined $passphrase
l!l&&r!l&&!r
003
MC/DC0%0/2
exists $self->{"hash"} or defined $passphrase
exists $self->{"hash"} defined $passphrase
Truth table33%1/3
A or B
A= exists $self->{"hash"}B= defined $passphrase
ABresult
1X1
011
000
102 defined($passphrase);
103 3 ▶ $value =~ m#\A(?:[0-7][0-9A-Fa-f]{7}){2}\z#
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /\A(?:[0-7][0-9A-Fa-f]{7}){2}\z/ or croak(qq["$value" is not a valid ] . 'hex MySQL v3.23 hash') 3 0 3
104 or croak "\"$value\" is not a valid ".
105 "hex MySQL v3.23 hash";
106 3 $self->{hash} = pack("H*", $value);
107 } elsif($attr eq "passphrase") {
108 croak "passphrase specified redundantly"
109 1 ▶ if exists($self->{hash}) ||
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"hash"} or defined $passphrase 0 1 1
Condition33%1/3
exists $self->{"hash"} or defined $passphrase
l!l&&r!l&&!r
001
MC/DC0%0/2
exists $self->{"hash"} or defined $passphrase
exists $self->{"hash"} defined $passphrase
Truth table33%1/3
A or B
A= exists $self->{"hash"}B= defined $passphrase
ABresult
1X1
011
000
110 defined($passphrase);
111 1 $passphrase = $value;
112 } else {
113 0 croak "unrecognised attribute `$attr'";
114 }
115 }
116 6 ▶ $self->{hash} = $self->_hash_of($passphrase) if defined $passphrase;
Branch: 1 branch
BranchTrueFalseTotal
if defined $passphrase 1 5 6
117 6 ▶ croak "hash not specified" unless exists $self->{hash};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"hash"} 0 6 6
118 6 return $self;
119 }
120  
121 =back
122  
123 =head1 METHODS
124  
125 =over
126  
127 =item $ppr->hash
128  
129 Returns the hash value, as a string of eight bytes.
130  
131 =cut
132  
133 6 ▶sub hash {
Subroutine hash: called CC=1 SCAR=0.0
Pod documented
134 6 my($self) = @_;
135 6 return $self->{hash};
136 }
137  
138 =item $ppr->hash_hex
139  
140 Returns the hash value, as a string of 16 hexadecimal digits.
141  
142 =cut
143  
144 6 ▶sub hash_hex {
Subroutine hash_hex: called CC=1 SCAR=0.0
Pod documented
145 6 my($self) = @_;
146 6 return unpack("H*", $self->{hash});
147 }
148  
149 =item $ppr->match(PASSPHRASE)
150  
151 This method is part of the standard L<Authen::Passphrase> interface.
152  
153 =cut
154  
155 26 ▶sub _hash_of {
Subroutine _hash_of: called CC=1 SCAR=0.0
156 26 my($self, $passphrase) = @_;
157 26 return pack("H*", password($passphrase));
158 }
159  
160 25 ▶sub match {
Subroutine match: called CC=1 SCAR=0.0
Pod documented
161 25 my($self, $passphrase) = @_;
162 25 return $self->_hash_of($passphrase) eq $self->{hash};
163 }
164  
165 =back
166  
167 =head1 SEE ALSO
168  
169 L<Authen::Passphrase>,
170 L<Crypt::MySQL>
171  
172 =head1 AUTHOR
173  
174 Andrew Main (Zefram) <zefram@fysh.org>
175  
176 =head1 COPYRIGHT
177  
178 Copyright (C) 2006, 2007, 2009, 2010, 2012
179 Andrew Main (Zefram) <zefram@fysh.org>
180  
181 =head1 LICENSE
182  
183 This module is free software; you can redistribute it and/or modify it
184 under the same terms as Perl itself.
185  
186 =cut
187  
188 2 1;
« Passphrase/NetscapeMail.pm ↑ Summary Passphrase/MySQL41.pm »