MediaWiki  REL1_24
LayeredParameterizedPassword.php
Go to the documentation of this file.
00001 <?php
00033 class LayeredParameterizedPassword extends ParameterizedPassword {
00034     protected function getDelimiter() {
00035         return '!';
00036     }
00037 
00038     protected function getDefaultParams() {
00039         $params = array();
00040 
00041         foreach ( $this->config['types'] as $type ) {
00042             $passObj = $this->factory->newFromType( $type );
00043 
00044             if ( !$passObj instanceof ParameterizedPassword ) {
00045                 throw new MWException( 'Underlying type must be a parameterized password.' );
00046             } elseif ( $passObj->getDelimiter() === $this->getDelimiter() ) {
00047                 throw new MWException( 'Underlying type cannot use same delimiter as encapsulating type.' );
00048             }
00049 
00050             $params[] = implode( $passObj->getDelimiter(), $passObj->getDefaultParams() );
00051         }
00052 
00053         return $params;
00054     }
00055 
00056     public function crypt( $password ) {
00057         $lastHash = $password;
00058         foreach ( $this->config['types'] as $i => $type ) {
00059             // Construct pseudo-hash based on params and arguments
00061             $passObj = $this->factory->newFromType( $type );
00062 
00063             $params = '';
00064             $args = '';
00065             if ( $this->params[$i] !== '' ) {
00066                 $params = $this->params[$i] . $passObj->getDelimiter();
00067             }
00068             if ( isset( $this->args[$i] ) && $this->args[$i] !== '' ) {
00069                 $args = $this->args[$i] . $passObj->getDelimiter();
00070             }
00071             $existingHash = ":$type:" . $params . $args . $this->hash;
00072 
00073             // Hash the last hash with the next type in the layer
00074             $passObj = $this->factory->newFromCiphertext( $existingHash );
00075             $passObj->crypt( $lastHash );
00076 
00077             // Move over the params and args
00078             $this->params[$i] = implode( $passObj->getDelimiter(), $passObj->params );
00079             $this->args[$i] = implode( $passObj->getDelimiter(), $passObj->args );
00080             $lastHash = $passObj->hash;
00081         }
00082 
00083         $this->hash = $lastHash;
00084     }
00085 
00097     public function partialCrypt( ParameterizedPassword $passObj ) {
00098         $type = $passObj->config['type'];
00099         if ( $type !== $this->config['types'][0] ) {
00100             throw new MWException( 'Only a hash in the first layer can be finished.' );
00101         }
00102 
00103         // Gather info from the existing hash
00104         $this->params[0] = implode( $passObj->getDelimiter(), $passObj->params );
00105         $this->args[0] = implode( $passObj->getDelimiter(), $passObj->args );
00106         $lastHash = $passObj->hash;
00107 
00108         // Layer the remaining types
00109         foreach ( $this->config['types'] as $i => $type ) {
00110             if ( $i == 0 ) {
00111                 continue;
00112             };
00113 
00114             // Construct pseudo-hash based on params and arguments
00116             $passObj = $this->factory->newFromType( $type );
00117 
00118             $params = '';
00119             $args = '';
00120             if ( $this->params[$i] !== '' ) {
00121                 $params = $this->params[$i] . $passObj->getDelimiter();
00122             }
00123             if ( isset( $this->args[$i] ) && $this->args[$i] !== '' ) {
00124                 $args = $this->args[$i] . $passObj->getDelimiter();
00125             }
00126             $existingHash = ":$type:" . $params . $args . $this->hash;
00127 
00128             // Hash the last hash with the next type in the layer
00129             $passObj = $this->factory->newFromCiphertext( $existingHash );
00130             $passObj->crypt( $lastHash );
00131 
00132             // Move over the params and args
00133             $this->params[$i] = implode( $passObj->getDelimiter(), $passObj->params );
00134             $this->args[$i] = implode( $passObj->getDelimiter(), $passObj->args );
00135             $lastHash = $passObj->hash;
00136         }
00137 
00138         $this->hash = $lastHash;
00139     }
00140 }