Romanian update
[phpmyadmin/arisferyanto.git] / libraries / mcrypt.lib.php
blob7d6d21cc3760163af0e6024932072954f5640102
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @version $Id$
6 */
8 /**
9 * Initialization
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 (empty($_COOKIE['pma_mcrypt_iv'])
15 || false === ($iv = base64_decode($_COOKIE['pma_mcrypt_iv']))) {
16 srand((double) microtime() * 1000000);
17 $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC), MCRYPT_RAND);
18 PMA_setCookie('pma_mcrypt_iv', base64_encode($iv));
21 /**
22 * String padding
24 * @param string input string
25 * @param integer length of the result
26 * @param string the filling string
27 * @param integer padding mode
29 * @return string the padded string
31 * @access public
33 function full_str_pad($input, $pad_length, $pad_string = '', $pad_type = 0) {
34 $str = '';
35 $length = $pad_length - strlen($input);
36 if ($length > 0) { // str_repeat doesn't like negatives
37 if ($pad_type == STR_PAD_RIGHT) { // STR_PAD_RIGHT == 1
38 $str = $input.str_repeat($pad_string, $length);
39 } elseif ($pad_type == STR_PAD_BOTH) { // STR_PAD_BOTH == 2
40 $str = str_repeat($pad_string, floor($length/2));
41 $str .= $input;
42 $str .= str_repeat($pad_string, ceil($length/2));
43 } else { // defaults to STR_PAD_LEFT == 0
44 $str = str_repeat($pad_string, $length).$input;
46 } else { // if $length is negative or zero we don't need to do anything
47 $str = $input;
49 return $str;
51 /**
52 * Encryption using blowfish algorithm (mcrypt)
54 * @param string original data
55 * @param string the secret
57 * @return string the encrypted result
59 * @access public
61 * @author lem9
63 function PMA_blowfish_encrypt($data, $secret) {
64 global $iv;
65 // Seems we don't need the padding. Anyway if we need it,
66 // we would have to replace 8 by the next 8-byte boundary.
67 //$data = full_str_pad($data, 8, "\0", STR_PAD_RIGHT);
68 return base64_encode(mcrypt_encrypt(MCRYPT_BLOWFISH, $secret, $data, MCRYPT_MODE_CBC, $iv));
71 /**
72 * Decryption using blowfish algorithm (mcrypt)
74 * @param string encrypted data
75 * @param string the secret
77 * @return string original data
79 * @access public
81 * @author lem9
83 function PMA_blowfish_decrypt($encdata, $secret) {
84 global $iv;
85 return trim(mcrypt_decrypt(MCRYPT_BLOWFISH, $secret, base64_decode($encdata), MCRYPT_MODE_CBC, $iv));