4 // vim: expandtab sw=4 ts=4 sts=4:
10 // Store the initialization vector because it will be needed for
11 // further decryption. I don't think necessary to have one iv
12 // per server so I don't put the server number in the cookie name.
14 if (!isset($_COOKIE['pma_mcrypt_iv'])) {
15 srand((double) microtime() * 1000000);
16 $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_BLOWFISH
, MCRYPT_MODE_CBC
), MCRYPT_RAND
);
17 setcookie('pma_mcrypt_iv',
19 time() +
(60 * 60 * 24 * 30),
20 $GLOBALS['cookie_path'], '',
21 $GLOBALS['is_https']);
23 $iv = base64_decode($_COOKIE['pma_mcrypt_iv']);
29 * @param string input string
30 * @param integer length of the result
31 * @param string the filling string
32 * @param integer padding mode
34 * @return string the padded string
38 function full_str_pad($input, $pad_length, $pad_string = '', $pad_type = 0) {
40 $length = $pad_length - strlen($input);
41 if ($length > 0) { // str_repeat doesn't like negatives
42 if ($pad_type == STR_PAD_RIGHT
) { // STR_PAD_RIGHT == 1
43 $str = $input.str_repeat($pad_string, $length);
44 } elseif ($pad_type == STR_PAD_BOTH
) { // STR_PAD_BOTH == 2
45 $str = str_repeat($pad_string, floor($length/2));
47 $str .= str_repeat($pad_string, ceil($length/2));
48 } else { // defaults to STR_PAD_LEFT == 0
49 $str = str_repeat($pad_string, $length).$input;
51 } else { // if $length is negative or zero we don't need to do anything
57 * Encryption using blowfish algorithm (mcrypt)
59 * @param string original data
60 * @param string the secret
62 * @return string the encrypted result
68 function PMA_blowfish_encrypt($data, $secret) {
70 // Seems we don't need the padding. Anyway if we need it,
71 // we would have to replace 8 by the next 8-byte boundary.
72 //$data = full_str_pad($data, 8, "\0", STR_PAD_RIGHT);
73 return base64_encode(mcrypt_encrypt(MCRYPT_BLOWFISH
, $secret, $data, MCRYPT_MODE_CBC
, $iv));
77 * Decryption using blowfish algorithm (mcrypt)
79 * @param string encrypted data
80 * @param string the secret
82 * @return string original data
88 function PMA_blowfish_decrypt($encdata, $secret) {
90 return trim(mcrypt_decrypt(MCRYPT_BLOWFISH
, $secret, base64_decode($encdata), MCRYPT_MODE_CBC
, $iv));