MediaWiki  REL1_24
Pbkdf2Password.php
Go to the documentation of this file.
00001 <?php
00031 class Pbkdf2Password extends ParameterizedPassword {
00032     protected function getDefaultParams() {
00033         return array(
00034             'algo' => $this->config['algo'],
00035             'rounds' => $this->config['cost'],
00036             'length' => $this->config['length']
00037         );
00038     }
00039 
00040     protected function getDelimiter() {
00041         return ':';
00042     }
00043 
00044     public function crypt( $password ) {
00045         if ( count( $this->args ) == 0 ) {
00046             $this->args[] = base64_encode( MWCryptRand::generate( 16, true ) );
00047         }
00048 
00049         if ( function_exists( 'hash_pbkdf2' ) ) {
00050             $hash = hash_pbkdf2(
00051                 $this->params['algo'],
00052                 $password,
00053                 base64_decode( $this->args[0] ),
00054                 (int)$this->params['rounds'],
00055                 (int)$this->params['length'],
00056                 true
00057             );
00058         } else {
00059             $hashLen = strlen( hash( $this->params['algo'], '', true ) );
00060             $blockCount = ceil( $this->params['length'] / $hashLen );
00061 
00062             $hash = '';
00063             $salt = base64_decode( $this->args[0] );
00064             for ( $i = 1; $i <= $blockCount; ++$i ) {
00065                 $roundTotal = $lastRound = hash_hmac(
00066                     $this->params['algo'],
00067                     $salt . pack( 'N', $i ),
00068                     $password,
00069                     true
00070                 );
00071 
00072                 for ( $j = 1; $j < $this->params['rounds']; ++$j ) {
00073                     $lastRound = hash_hmac( $this->params['algo'], $lastRound, $password, true );
00074                     $roundTotal ^= $lastRound;
00075                 }
00076 
00077                 $hash .= $roundTotal;
00078             }
00079 
00080             $hash = substr( $hash, 0, $this->params['length'] );
00081         }
00082 
00083         $this->hash = base64_encode( $hash );
00084     }
00085 }