TYPO3 API  SVNRelease
class.tx_saltedpasswords_abstract_salts.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2009-2011 Marcus Krause <marcus#exp2009@t3sec.info>
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *  A copy is found in the textfile GPL.txt and important notices to the license
00017 *  from the author is found in LICENSE.txt distributed with these scripts.
00018 *
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 /**
00028  * Contains abstract class "tx_saltedpasswords_abstract_salts"
00029  * to be used in classes that provide salted hashing.
00030  *
00031  * $Id: class.tx_saltedpasswords_abstract_salts.php 10120 2011-01-18 20:03:36Z ohader $
00032  */
00033 
00034 
00035 /**
00036  * Abtract class with methods needed to be extended
00037  * in a salted hashing class.
00038  *
00039  * @author      Marcus Krause <marcus#exp2009@t3sec.info>
00040  *
00041  * @abstract
00042  * @since       2009-09-06
00043  * @package     TYPO3
00044  * @subpackage  tx_saltedpasswords
00045  */
00046 abstract class tx_saltedpasswords_abstract_salts {
00047     /**
00048      * Method applies settings (prefix, optional hash count, optional suffix)
00049      * to a salt.
00050      *
00051      * @param   string      $salt:  a salt to apply setting to
00052      * @return  string      salt with setting
00053      */
00054     abstract protected function applySettingsToSalt($salt);
00055 
00056     /**
00057      * Generates a random base salt settings for the hash.
00058      *
00059      * @return  string      a string containing settings and a random salt
00060      */
00061     abstract protected function getGeneratedSalt();
00062 
00063     /**
00064      * Returns a string for mapping an int to the corresponding base 64 character.
00065      *
00066      * @return  string      string for mapping an int to the corresponding base 64 character
00067      */
00068     abstract protected function getItoa64();
00069 
00070     /**
00071      * Returns setting string to indicate type of hashing method.
00072      *
00073      * @return  string      setting string of hashing method
00074      */
00075     abstract protected function getSetting();
00076 
00077     /**
00078      * Encodes bytes into printable base 64 using the *nix standard from crypt().
00079      *
00080      * @param   string      $input: the string containing bytes to encode.
00081      * @param   integer     $count: the number of characters (bytes) to encode.
00082      * @return  string      encoded string
00083      */
00084     public function base64Encode($input, $count) {
00085         $output = '';
00086         $i = 0;
00087         $itoa64 = $this->getItoa64();
00088         do {
00089             $value = ord($input[$i++]);
00090             $output .= $itoa64[$value & 0x3f];
00091             if ($i < $count) {
00092                 $value |= ord($input[$i]) << 8;
00093             }
00094             $output .= $itoa64[($value >> 6) & 0x3f];
00095             if ($i++ >= $count) {
00096                 break;
00097             }
00098             if ($i < $count) {
00099                 $value |= ord($input[$i]) << 16;
00100             }
00101             $output .= $itoa64[($value >> 12) & 0x3f];
00102             if ($i++ >= $count) {
00103                 break;
00104             }
00105             $output .= $itoa64[($value >> 18) & 0x3f];
00106         } while ($i < $count);
00107         return $output;
00108     }
00109 
00110     /**
00111      * Method determines required length of base64 characters for a given
00112      * length of a byte string.
00113      *
00114      * @param   integer     $byteLength: length of bytes to calculate in base64 chars
00115      * @return  integer     required length of base64 characters
00116      */
00117     protected function getLengthBase64FromBytes($byteLength) {
00118             // calculates bytes in bits in base64
00119         return intval(ceil(($byteLength * 8) / 6));
00120     }
00121 }
00122 
00123 
00124 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/saltedpasswords/classes/salts/class.tx_saltedpasswords_abstract_salts.php'])) {
00125     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/saltedpasswords/classes/salts/class.tx_saltedpasswords_abstract_salts.php']);
00126 }
00127 ?>