Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | class CRM_Utils_Crypt { | |
36 | ||
37 | static function encrypt($string) { | |
38 | if (empty($string)) { | |
39 | return $string; | |
40 | } | |
41 | ||
42 | if (function_exists('mcrypt_module_open') && | |
43 | defined('CIVICRM_SITE_KEY') | |
44 | ) { | |
45 | $td = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_ECB, ''); | |
46 | // ECB mode - iv not needed - CRM-8198 | |
47 | $iv = '00000000000000000000000000000000'; | |
48 | $ks = mcrypt_enc_get_key_size($td); | |
49 | $key = substr(sha1(CIVICRM_SITE_KEY), 0, $ks); | |
50 | ||
51 | mcrypt_generic_init($td, $key, $iv); | |
52 | $string = mcrypt_generic($td, $string); | |
53 | mcrypt_generic_deinit($td); | |
54 | mcrypt_module_close($td); | |
55 | } | |
56 | return base64_encode($string); | |
57 | } | |
58 | ||
59 | static function decrypt($string) { | |
60 | if (empty($string)) { | |
61 | return $string; | |
62 | } | |
63 | ||
64 | $string = base64_decode($string); | |
65 | if (empty($string)) { | |
66 | return $string; | |
67 | } | |
68 | ||
69 | if (function_exists('mcrypt_module_open') && | |
70 | defined('CIVICRM_SITE_KEY') | |
71 | ) { | |
72 | $td = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_ECB, ''); | |
73 | // ECB mode - iv not needed - CRM-8198 | |
74 | $iv = '00000000000000000000000000000000'; | |
75 | $ks = mcrypt_enc_get_key_size($td); | |
76 | $key = substr(sha1(CIVICRM_SITE_KEY), 0, $ks); | |
77 | ||
78 | mcrypt_generic_init($td, $key, $iv); | |
79 | $string = rtrim(mdecrypt_generic($td, $string)); | |
80 | mcrypt_generic_deinit($td); | |
81 | mcrypt_module_close($td); | |
82 | } | |
83 | ||
84 | return $string; | |
85 | } | |
86 | } | |
87 |