Add link to wiki explaining configuration options.
[phpmyadmin/crack.git] / libraries / mcrypt.lib.php
blobc4ed43972472146287abc8b3fd1fda84ee3e5d05
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 setcookie('pma_mcrypt_iv',
18 base64_encode($iv),
19 time() + (60 * 60 * 24 * 30),
20 $GLOBALS['cookie_path'], '',
21 $GLOBALS['is_https']);
22 } else {
23 $iv = base64_decode($_COOKIE['pma_mcrypt_iv']);
26 /**
27 * String padding
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
36 * @access public
38 function full_str_pad($input, $pad_length, $pad_string = '', $pad_type = 0) {
39 $str = '';
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));
46 $str .= $input;
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
52 $str = $input;
54 return $str;
56 /**
57 * Encryption using blowfish algorithm (mcrypt)
59 * @param string original data
60 * @param string the secret
62 * @return string the encrypted result
64 * @access public
66 * @author lem9
68 function PMA_blowfish_encrypt($data, $secret) {
69 global $iv;
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));
76 /**
77 * Decryption using blowfish algorithm (mcrypt)
79 * @param string encrypted data
80 * @param string the secret
82 * @return string original data
84 * @access public
86 * @author lem9
88 function PMA_blowfish_decrypt($encdata, $secret) {
89 global $iv;
90 return trim(mcrypt_decrypt(MCRYPT_BLOWFISH, $secret, base64_decode($encdata), MCRYPT_MODE_CBC, $iv));