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 PMA_setCookie('pma_mcrypt_iv', base64_encode($iv));
19 $iv = base64_decode($_COOKIE['pma_mcrypt_iv']);
25 * @param string input string
26 * @param integer length of the result
27 * @param string the filling string
28 * @param integer padding mode
30 * @return string the padded string
34 function full_str_pad($input, $pad_length, $pad_string = '', $pad_type = 0) {
36 $length = $pad_length - strlen($input);
37 if ($length > 0) { // str_repeat doesn't like negatives
38 if ($pad_type == STR_PAD_RIGHT
) { // STR_PAD_RIGHT == 1
39 $str = $input.str_repeat($pad_string, $length);
40 } elseif ($pad_type == STR_PAD_BOTH
) { // STR_PAD_BOTH == 2
41 $str = str_repeat($pad_string, floor($length/2));
43 $str .= str_repeat($pad_string, ceil($length/2));
44 } else { // defaults to STR_PAD_LEFT == 0
45 $str = str_repeat($pad_string, $length).$input;
47 } else { // if $length is negative or zero we don't need to do anything
53 * Encryption using blowfish algorithm (mcrypt)
55 * @param string original data
56 * @param string the secret
58 * @return string the encrypted result
64 function PMA_blowfish_encrypt($data, $secret) {
66 // Seems we don't need the padding. Anyway if we need it,
67 // we would have to replace 8 by the next 8-byte boundary.
68 //$data = full_str_pad($data, 8, "\0", STR_PAD_RIGHT);
69 return base64_encode(mcrypt_encrypt(MCRYPT_BLOWFISH
, $secret, $data, MCRYPT_MODE_CBC
, $iv));
73 * Decryption using blowfish algorithm (mcrypt)
75 * @param string encrypted data
76 * @param string the secret
78 * @return string original data
84 function PMA_blowfish_decrypt($encdata, $secret) {
86 return trim(mcrypt_decrypt(MCRYPT_BLOWFISH
, $secret, base64_decode($encdata), MCRYPT_MODE_CBC
, $iv));