Fix typos (bug #1597137).
[phpmyadmin/last10db.git] / libraries / mcrypt.lib.php
blob3954f67bbf7b070269d6f21e287702f08df64ca6
1 <?php
3 /* $Id$ */
4 // vim: expandtab sw=4 ts=4 sts=4:
6 /**
7 * Initialization
8 */
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));
18 } else {
19 $iv = base64_decode($_COOKIE['pma_mcrypt_iv']);
22 /**
23 * String padding
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
32 * @access public
34 function full_str_pad($input, $pad_length, $pad_string = '', $pad_type = 0) {
35 $str = '';
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));
42 $str .= $input;
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
48 $str = $input;
50 return $str;
52 /**
53 * Encryption using blowfish algorithm (mcrypt)
55 * @param string original data
56 * @param string the secret
58 * @return string the encrypted result
60 * @access public
62 * @author lem9
64 function PMA_blowfish_encrypt($data, $secret) {
65 global $iv;
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));
72 /**
73 * Decryption using blowfish algorithm (mcrypt)
75 * @param string encrypted data
76 * @param string the secret
78 * @return string original data
80 * @access public
82 * @author lem9
84 function PMA_blowfish_decrypt($encdata, $secret) {
85 global $iv;
86 return trim(mcrypt_decrypt(MCRYPT_BLOWFISH, $secret, base64_decode($encdata), MCRYPT_MODE_CBC, $iv));