home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / phpMyAdmin / libraries / mcrypt.lib.php < prev    next >
Encoding:
PHP Script  |  2008-06-23  |  2.6 KB  |  92 lines

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4.  *
  5.  * @version $Id: mcrypt.lib.php 11326 2008-06-17 21:32:48Z lem9 $
  6.  */
  7. if (! defined('PHPMYADMIN')) {
  8.     exit;
  9. }
  10.  
  11. /**
  12.  * Initialization
  13.  * Store the initialization vector because it will be needed for
  14.  * further decryption. I don't think necessary to have one iv
  15.  * per server so I don't put the server number in the cookie name.
  16.  */
  17. if (empty($_COOKIE['pma_mcrypt_iv'])
  18.  || false === ($iv = base64_decode($_COOKIE['pma_mcrypt_iv']))) {
  19.     srand((double) microtime() * 1000000);
  20.     $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC), MCRYPT_RAND);
  21.     PMA_setCookie('pma_mcrypt_iv', base64_encode($iv));
  22. }
  23.  
  24. /**
  25.  * String padding
  26.  *
  27.  * @param   string  input string
  28.  * @param   integer length of the result
  29.  * @param   string  the filling string
  30.  * @param   integer padding mode
  31.  *
  32.  * @return  string  the padded string
  33.  *
  34.  * @access  public
  35.  */
  36. function full_str_pad($input, $pad_length, $pad_string = '', $pad_type = 0) {
  37.     $str = '';
  38.     $length = $pad_length - strlen($input);
  39.     if ($length > 0) { // str_repeat doesn't like negatives
  40.         if ($pad_type == STR_PAD_RIGHT) { // STR_PAD_RIGHT == 1
  41.             $str = $input.str_repeat($pad_string, $length);
  42.         } elseif ($pad_type == STR_PAD_BOTH) { // STR_PAD_BOTH == 2
  43.             $str = str_repeat($pad_string, floor($length/2));
  44.             $str .= $input;
  45.             $str .= str_repeat($pad_string, ceil($length/2));
  46.         } else { // defaults to STR_PAD_LEFT == 0
  47.             $str = str_repeat($pad_string, $length).$input;
  48.         }
  49.     } else { // if $length is negative or zero we don't need to do anything
  50.         $str = $input;
  51.     }
  52.     return $str;
  53. }
  54. /**
  55.  * Encryption using blowfish algorithm (mcrypt)
  56.  *
  57.  * @param   string  original data
  58.  * @param   string  the secret
  59.  *
  60.  * @return  string  the encrypted result
  61.  *
  62.  * @access  public
  63.  *
  64.  * @author  lem9
  65.  */
  66. function PMA_blowfish_encrypt($data, $secret) {
  67.     global $iv;
  68.     // Seems we don't need the padding. Anyway if we need it,
  69.     // we would have to replace 8 by the next 8-byte boundary.
  70.     //$data = full_str_pad($data, 8, "\0", STR_PAD_RIGHT);
  71.     return base64_encode(mcrypt_encrypt(MCRYPT_BLOWFISH, $secret, $data, MCRYPT_MODE_CBC, $iv));
  72. }
  73.  
  74. /**
  75.  * Decryption using blowfish algorithm (mcrypt)
  76.  *
  77.  * @param   string  encrypted data
  78.  * @param   string  the secret
  79.  *
  80.  * @return  string  original data
  81.  *
  82.  * @access  public
  83.  *
  84.  * @author  lem9
  85.  */
  86. function PMA_blowfish_decrypt($encdata, $secret) {
  87.     global $iv;
  88.     return trim(mcrypt_decrypt(MCRYPT_BLOWFISH, $secret, base64_decode($encdata), MCRYPT_MODE_CBC, $iv));
  89. }
  90.  
  91. ?>
  92.