| 1 | =head1 NAME | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | Authen::Passphrase::MySQL41 - passphrases using the MySQL v4.1 algorithm | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | use Authen::Passphrase::MySQL41; | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | $ppr = Authen::Passphrase::MySQL41->new( | ||||||||||||||||||||||||||||
| 10 | hash_hex => "9CD12C48C4C5DD62914B". | ||||||||||||||||||||||||||||
| 11 | "3FABB93131746E9E9115"); | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | $ppr = Authen::Passphrase::MySQL41->new( | ||||||||||||||||||||||||||||
| 14 | passphrase => "passphrase"); | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | $hash = $ppr->hash; | ||||||||||||||||||||||||||||
| 17 | $hash_hex = $ppr->hash_hex; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | if($ppr->match($passphrase)) { ... | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | An object of this class encapsulates a passphrase hashed using the | ||||||||||||||||||||||||||||
| 24 | algorithm used by MySQL from version 4.1. This is a subclass of | ||||||||||||||||||||||||||||
| 25 | L<Authen::Passphrase>, and this document assumes that the reader is | ||||||||||||||||||||||||||||
| 26 | familiar with the documentation for that class. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | The MySQL v4.1 hash scheme is based on the SHA-1 digest algorithm. | ||||||||||||||||||||||||||||
| 29 | The passphrase is first hashed using SHA-1, then the output of that | ||||||||||||||||||||||||||||
| 30 | stage is hashed using SHA-1 again. The final hash is the output of the | ||||||||||||||||||||||||||||
| 31 | second SHA-1. No salt is used. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | In MySQL the hash is represented as a "B<*>" followed by 40 uppercase | ||||||||||||||||||||||||||||
| 34 | hexadecimal digits. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | The lack of salt is a weakness in this scheme. Salted SHA-1 is a better | ||||||||||||||||||||||||||||
| 37 | scheme; see L<Authen::Passphrase::SaltedDigest>. | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =cut | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | package Authen::Passphrase::MySQL41; | ||||||||||||||||||||||||||||
| 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 Digest::SHA qw(sha1); | ||||||||||||||||||||||||||
|
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::MySQL41->new(ATTR => VALUE, ...) | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | Generates a new passphrase recogniser object using the MySQL v4.1 | ||||||||||||||||||||||||||||
| 62 | algorithm. The following attributes may be given: | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =over | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =item B<hash> | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | The hash, as a string of 20 bytes. | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =item B<hash_hex> | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | The hash, as a string of 40 hexadecimal digits. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =item B<passphrase> | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | A passphrase that will be accepted. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =back | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | Either the hash or the passphrase must be given. | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =cut | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 6 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=15 SCAR=29.9
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 6 | my $class = shift; | |||||||||||||||||||||||||||
| 86 | 6 | my $self = bless({}, $class); | |||||||||||||||||||||||||||
| 87 | 6 | my $passphrase; | |||||||||||||||||||||||||||
| 88 | 6 | ▶ | while(@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 6 | my $attr = shift; | |||||||||||||||||||||||||||
| 90 | 6 | my $value = shift; | |||||||||||||||||||||||||||
| 91 | 6 | ▶ | if($attr eq "hash") { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 92 | croak "hash specified redundantly" | ||||||||||||||||||||||||||||
| 93 | 2 | ▶ | if exists($self->{hash}) || | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $self->{"hash"} or defined $passphrase
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
| |||||||||||||||||||||||||||||
| 94 | defined($passphrase); | ||||||||||||||||||||||||||||
| 95 | 2 | ▶ | $value =~ m#\A[\x00-\xff]{20}\z# | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | or croak "not a valid MySQL v4.1 hash"; | ||||||||||||||||||||||||||||
| 97 | 2 | $self->{hash} = "$value"; | |||||||||||||||||||||||||||
| 98 | } elsif($attr eq "hash_hex") { | ||||||||||||||||||||||||||||
| 99 | croak "hash specified redundantly" | ||||||||||||||||||||||||||||
| 100 | 3 | ▶ | if exists($self->{hash}) || | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $self->{"hash"} or defined $passphrase
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
| |||||||||||||||||||||||||||||
| 101 | defined($passphrase); | ||||||||||||||||||||||||||||
| 102 | 3 | ▶ | $value =~ m#\A[0-9A-Fa-f]{40}\z# | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | or croak "\"$value\" is not a valid ". | ||||||||||||||||||||||||||||
| 104 | "hex MySQL v4.1 hash"; | ||||||||||||||||||||||||||||
| 105 | 3 | $self->{hash} = pack("H*", $value); | |||||||||||||||||||||||||||
| 106 | } elsif($attr eq "passphrase") { | ||||||||||||||||||||||||||||
| 107 | croak "passphrase specified redundantly" | ||||||||||||||||||||||||||||
| 108 | 1 | ▶ | if exists($self->{hash}) || | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $self->{"hash"} or defined $passphrase
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
| |||||||||||||||||||||||||||||
| 109 | defined($passphrase); | ||||||||||||||||||||||||||||
| 110 | 1 | $passphrase = $value; | |||||||||||||||||||||||||||
| 111 | } else { | ||||||||||||||||||||||||||||
| 112 | 0 | croak "unrecognised attribute `$attr'"; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | 6 | ▶ | $self->{hash} = $self->_hash_of($passphrase) if defined $passphrase; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 6 | ▶ | croak "hash not specified" unless exists $self->{hash}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 6 | return $self; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =back | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head1 METHODS | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =over | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =item $ppr->hash | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | Returns the hash value, as a string of 20 bytes. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =cut | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 6 | ▶ | sub hash { | ||||||||||||||||||||||||||
|
Subroutine
hash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 133 | 6 | my($self) = @_; | |||||||||||||||||||||||||||
| 134 | 6 | return $self->{hash}; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =item $ppr->hash_hex | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | Returns the hash value, as a string of 40 uppercase hexadecimal digits. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =cut | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 6 | ▶ | sub hash_hex { | ||||||||||||||||||||||||||
|
Subroutine
hash_hex: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 144 | 6 | my($self) = @_; | |||||||||||||||||||||||||||
| 145 | 6 | return uc(unpack("H*", $self->{hash})); | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =item $ppr->match(PASSPHRASE) | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | This method is part of the standard L<Authen::Passphrase> interface. | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =cut | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 26 | ▶ | sub _hash_of { | ||||||||||||||||||||||||||
|
Subroutine
_hash_of: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 155 | 26 | my($self, $passphrase) = @_; | |||||||||||||||||||||||||||
| 156 | 26 | return sha1(sha1($passphrase)); | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 25 | ▶ | sub match { | ||||||||||||||||||||||||||
|
Subroutine
match: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 160 | 25 | my($self, $passphrase) = @_; | |||||||||||||||||||||||||||
| 161 | 25 | return $self->_hash_of($passphrase) eq $self->{hash}; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =back | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | L<Authen::Passphrase>, | ||||||||||||||||||||||||||||
| 169 | L<Digest::SHA> | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | Andrew Main (Zefram) <zefram@fysh.org> | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | Copyright (C) 2006, 2007, 2009, 2010, 2012 | ||||||||||||||||||||||||||||
| 178 | Andrew Main (Zefram) <zefram@fysh.org> | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | This module is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 183 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =cut | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 2 | 1; | |||||||||||||||||||||||||||