2.11.11.3 release
[phpmyadmin/arisferyanto.git] / libraries / mcrypt.lib.php
blobe6b7277dc96a2dea4aa443bad64b9698163fee75
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @version $Id$
6 */
7 if (! defined('PHPMYADMIN')) {
8 exit;
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.
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));
24 /**
25 * String padding
27 * @param string input string
28 * @param integer length of the result
29 * @param string the filling string
30 * @param integer padding mode
32 * @return string the padded string
34 * @access public
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;
49 } else { // if $length is negative or zero we don't need to do anything
50 $str = $input;
52 return $str;
54 /**
55 * Encryption using blowfish algorithm (mcrypt)
57 * @param string original data
58 * @param string the secret
60 * @return string the encrypted result
62 * @access public
64 * @author lem9
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));
74 /**
75 * Decryption using blowfish algorithm (mcrypt)
77 * @param string encrypted data
78 * @param string the secret
80 * @return string original data
82 * @access public
84 * @author lem9
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));