Passphrase/EggdropBlowfish.pm

filter: Statementstmt 98.5% 69 / 70 Branchbran 76.6% 23 / 30 Conditioncond 50.0% 6 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 85.5% 118 / 138 CC 21 SCAR 23.7
CC
21
Cov
88%
new
20.7
_en_base64
3.0
_de_base64
3.0
CRAP 10.7
« Passphrase/LANManagerHalf.pm ↑ Summary Passphrase/MD5Crypt.pm »
1 =head1 NAME
2  
3 Authen::Passphrase::EggdropBlowfish - passphrases using Eggdrop's
4 blowfish.mod
5  
6 =head1 SYNOPSIS
7  
8 use Authen::Passphrase::EggdropBlowfish;
9  
10 $ppr = Authen::Passphrase::EggdropBlowfish->new(
11 hash_base64 => "9tpsG/61YqX/");
12  
13 $ppr = Authen::Passphrase::EggdropBlowfish->new(
14 passphrase => "passphrase");
15  
16 $hash = $ppr->hash;
17 $hash_base64 = $ppr->hash_base64;
18  
19 if($ppr->match($passphrase)) { ...
20  
21 =head1 DESCRIPTION
22  
23 An object of this class encapsulates a passphrase hashed using the
24 Blowfish-based algorithm used in Eggdrop's blowfish.mod. This is a
25 subclass of L<Authen::Passphrase>, and this document assumes that the
26 reader is familiar with the documentation for that class.
27  
28 This hash scheme uses no salt, and does not accept a zero-length
29 passphrase. It uses the passphrase as a Blowfish key to encrypt a
30 standard plaintext block. The hash is the ciphertext block. The standard
31 Blowfish key schedule only accepts keys from 8 to 56 bytes long; this
32 algorithm relaxes that requirement and accepts any non-zero length.
33 Up to 72 bytes of passphrase/key are significant; any more are ignored.
34  
35 In Eggdrop the hash is represented as a "B<+>" followed by twelve base
36 64 digits. The first six digits encode the second half of the hash,
37 and the last six encode the first half. Within each half the bytes
38 are encoded in reverse order. The base 64 digits are "B<.>", "B</>",
39 "B<0>" to "B<9>", "B<a>" to "B<z>", "B<A>" to "B<Z>" (in that order).
40  
41 I<Warning:> The hash is small by modern standards, and the lack of salt
42 is a weakness in this scheme. For a scheme that makes better use of
43 Blowfish see L<Authen::Passphrase::BlowfishCrypt>.
44  
45 =cut
46  
47 package Authen::Passphrase::EggdropBlowfish;
48  
49 2 ▶{ use 5.006; }
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 2 ▶use Authen::Passphrase 0.003;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
55 2 ▶use Crypt::Eksblowfish::Uklblowfish 0.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 2 our $VERSION = "0.009";
58  
59 2 ▶use parent "Authen::Passphrase";
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 2 my $b64_digits =
62 "./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
63  
64 9 ▶sub _en_base64($) {
Subroutine _en_base64: called CC=3 SCAR=11.0
65 9 my($bytes) = @_;
66 9 my $digits = "";
67 9 foreach my $word (reverse unpack("N*", $bytes)) {
68 18 ▶ for(my $i = 6; $i--; $word >>= 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i--) 108 18 126
69 108 $digits .= substr($b64_digits, $word & 0x3f, 1);
70 }
71 }
72 9 return $digits;
73 }
74  
75 7 ▶sub _de_base64($) {
Subroutine _de_base64: called CC=3 SCAR=11.0
76 7 my($digits) = @_;
77 7 my @words;
78 7 ▶ while($digits =~ /(......)/sg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digits =~ /(......)/gs) 14 7 21
79 14 my $wdig = $1;
80 14 my $word = 0;
81 14 ▶ for(my $i = 6; $i--; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i--) 84 14 98
82 84 $word <<= 6;
83 84 $word |= index($b64_digits, substr($wdig, $i, 1));
84 }
85 14 push @words, $word;
86 }
87 7 return pack("N*", reverse @words);
88 }
89  
90 =head1 CONSTRUCTOR
91  
92 =over
93  
94 =item Authen::Passphrase::EggdropBlowfish->new(ATTR => VALUE, ...)
95  
96 Generates a new passphrase recogniser object using the Eggdrop
97 blowfish.mod algorithm. The following attributes may be given:
98  
99 =over
100  
101 =item B<hash>
102  
103 The hash, as a string of eight bytes.
104  
105 =item B<hash_base64>
106  
107 The hash, as a string of twelve base 64 digits.
108  
109 =item B<passphrase>
110  
111 A passphrase that will be accepted.
112  
113 =back
114  
115 Either the hash or the passphrase must be given.
116  
117 =cut
118  
119 10 ▶sub new {
Subroutine new: called CC=16 SCAR=30.3
Pod documented
120 10 my $class = shift;
121 10 my $self = bless({}, $class);
122 10 my $passphrase;
123 10 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 10 9 19
124 10 my $attr = shift;
125 10 my $value = shift;
126 10 ▶ if($attr eq "hash") {
Branch: 3 branches
BranchTrueFalseTotal
if ($attr eq 'hash') { } 1 9 10
elsif ($attr eq 'hash_base64') { } 7 2 9
elsif ($attr eq 'passphrase') { } 2 0 2
127 croak "hash specified redundantly"
128 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
129 defined($passphrase);
130 1 ▶ $value =~ m#\A[\x00-\xff]{8}\z#
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /\A[\x00-\xff]{8}\z/ or croak("not a valid hash") 1 0 1
131 or croak "not a valid hash";
132 1 $self->{hash} = "$value";
133 } elsif($attr eq "hash_base64") {
134 croak "hash specified redundantly"
135 7 ▶ if exists($self->{hash}) ||
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"hash"} or defined $passphrase 0 7 7
Condition33%1/3
exists $self->{"hash"} or defined $passphrase
l!l&&r!l&&!r
007
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
136 defined($passphrase);
137 7 ▶ $value =~ m#\A(?:[./0-9a-zA-Z]{5}[./01]){2}\z#
Branch: 1 branch
BranchTrueFalseTotal
$value =~ m[\A(?:[./0-9a-zA-Z]{5}[./01]){2}\z] or croak(qq["$value" is not a valid ] . 'base 64 hash') 7 0 7
138 or croak "\"$value\" is not a valid ".
139 "base 64 hash";
140 7 $self->{hash} = _de_base64($value);
141 } elsif($attr eq "passphrase") {
142 croak "passphrase specified redundantly"
143 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
144 defined($passphrase);
145 2 ▶ $value ne "" or croak "can't accept null passphrase";
Branch: 1 branch
BranchTrueFalseTotal
$value ne "" or croak("can't accept null passphrase") 1 1 2
146 1 $passphrase = $value;
147 } else {
148 0 croak "unrecognised attribute `$attr'";
149 }
150 }
151 9 ▶ $self->{hash} = $self->_hash_of($passphrase) if defined $passphrase;
Branch: 1 branch
BranchTrueFalseTotal
if defined $passphrase 1 8 9
152 9 ▶ croak "hash not specified" unless exists $self->{hash};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"hash"} 0 9 9
153 9 return $self;
154 }
155  
156 =back
157  
158 =head1 METHODS
159  
160 =over
161  
162 =item $ppr->hash
163  
164 Returns the hash value, as a string of eight bytes.
165  
166 =cut
167  
168 3 ▶sub hash {
Subroutine hash: called CC=1 SCAR=0.0
Pod documented
169 3 my($self) = @_;
170 3 return $self->{hash};
171 }
172  
173 =item $ppr->hash_base64
174  
175 Returns the hash value, as a string of twelve base 64 digits.
176  
177 =cut
178  
179 9 ▶sub hash_base64 {
Subroutine hash_base64: called CC=1 SCAR=0.0
Pod documented
180 9 my($self) = @_;
181 9 return _en_base64($self->{hash});
182 }
183  
184 =item $ppr->match(PASSPHRASE)
185  
186 This method is part of the standard L<Authen::Passphrase> interface.
187  
188 =cut
189  
190 38 ▶sub _hash_of {
Subroutine _hash_of: called CC=1 SCAR=0.0
191 38 my($self, $passphrase) = @_;
192 38 $passphrase = substr($passphrase, 0, 72);
193 38 my $cipher = Crypt::Eksblowfish::Uklblowfish->new($passphrase);
194 38 return $cipher->encrypt("\xde\xad\xd0\x61\x23\xf6\xb0\x95");
195 }
196  
197 43 ▶sub match {
Subroutine match: called CC=2 SCAR=6.9
Pod documented
198 43 my($self, $passphrase) = @_;
199 return $passphrase ne "" &&
200 43 ▶ $self->_hash_of($passphrase) eq $self->{hash};
Condition100%3/3
$passphrase ne '' && $self->_hash_of($passphrase) eq $self->{'hash'}
!ll&&!rl&&r
6307
MC/DC100%2/2
$passphrase ne '' && $self->_hash_of($passphrase) eq $self->{'hash'}
$passphrase ne '' $self->_hash_of($passphrase) eq $self->{'hash'}
Truth table100%3/3
A && B
A= $passphrase ne ''B= $self->_hash_of($passphrase) eq $self->{'hash'}
ABresult
0X0
100
111
201 }
202  
203 =back
204  
205 =head1 SEE ALSO
206  
207 L<Authen::Passphrase>,
208 L<Crypt::Eksblowfish::Uklblowfish>
209  
210 =head1 AUTHOR
211  
212 Andrew Main (Zefram) <zefram@fysh.org>
213  
214 =head1 COPYRIGHT
215  
216 Copyright (C) 2006, 2007, 2009, 2010, 2012
217 Andrew Main (Zefram) <zefram@fysh.org>
218  
219 =head1 LICENSE
220  
221 This module is free software; you can redistribute it and/or modify it
222 under the same terms as Perl itself.
223  
224 =cut
225  
226 2 1;
« Passphrase/LANManagerHalf.pm ↑ Summary Passphrase/MD5Crypt.pm »