[ Index ]
 

Code source de Dotclear 2.0-beta6

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/inc/clearbricks/common/ -> lib.crypt.php (source)

   1  <?php
   2  # ***** BEGIN LICENSE BLOCK *****
   3  # This file is part of Clearbricks.
   4  # Copyright (c) 2006 Olivier Meunier and contributors. All rights
   5  # reserved.
   6  #
   7  # Clearbricks is free software; you can redistribute it and/or modify
   8  # it under the terms of the GNU General Public License as published by
   9  # the Free Software Foundation; either version 2 of the License, or
  10  # (at your option) any later version.
  11  # 
  12  # Clearbricks is distributed in the hope that it will be useful,
  13  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15  # GNU General Public License for more details.
  16  # 
  17  # You should have received a copy of the GNU General Public License
  18  # along with Clearbricks; if not, write to the Free Software
  19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20  #
  21  # ***** END LICENSE BLOCK *****
  22  
  23  /**
  24  @ingroup CLEARBRICKS
  25  @brief Crypt and password helper functions.
  26  */
  27  class crypt
  28  {
  29      /**
  30      Returns an HMAC encoded value of <var>$data</var>, using the said <var>$key</var>
  31      and <var>$hashfunc</var> as hash method (sha1 or md5 are accepted.)
  32      
  33      @param    key        <b>string</b>        Hash key
  34      @param    data        <b>string</b>        Data
  35      @param    hashfunc    <b>string</b>        Hash function (md5 or sha1)
  36      @return    <b>string</b>
  37      */
  38  	public static function hmac($key,$data,$hashfunc='sha1')
  39      {
  40          $blocksize=64;
  41          if ($hashfunc != 'sha1') {
  42              $hashfunc = 'md5';
  43          }
  44          
  45          if (strlen($key)>$blocksize) {
  46              $key=pack('H*', $hashfunc($key));
  47          }
  48          
  49          $key=str_pad($key,$blocksize,chr(0x00));
  50          $ipad=str_repeat(chr(0x36),$blocksize);
  51          $opad=str_repeat(chr(0x5c),$blocksize);
  52          $hmac = pack('H*',$hashfunc(($key^$opad).pack('H*',$hashfunc(($key^$ipad).$data))));
  53          return bin2hex($hmac);
  54      }
  55      
  56      /**
  57      Returns an 8 characters random password.
  58      
  59      @return    <b>string</b>
  60      */
  61  	public static function createPassword()
  62      {
  63          $pwd = array();
  64          $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
  65          $chars2 = '$!@';
  66          
  67          foreach (range(0,8) as $i) {
  68              $pwd[] = $chars[rand(0,strlen($chars)-1)];
  69          }
  70          
  71          $pos1 = array_rand(array(0,1,2,3));
  72          $pos2 = array_rand(array(4,5,6,7));
  73          $pwd[$pos1] = $chars2[rand(0,strlen($chars2)-1)];
  74          $pwd[$pos2] = $chars2[rand(0,strlen($chars2)-1)];
  75          
  76          return implode('',$pwd);
  77      }
  78  }


Généré le : Fri Feb 23 22:16:06 2007 par Balluche grâce à PHPXref 0.7