3 * Basic encryption/decryption class.
4 * We use this class atm for encrypting & decrypting the imap passwords.
8 private $config; /**< array that should contain the enc_method & hash_method & key */
13 * loads the config array with the given argument.
14 * @param $cryptinfo an array containing the info needed to encrypt & decrypt.(enc_method & hash_method & key)
16 function __construct($cryptinfo) {
17 $this->config
= $cryptinfo;
21 * encrypts by using the given enc_method and hash_method.
22 * It will first check if the methods are supported, if not it will throw an error, if so it will encrypt the $data
23 * @param $data the string that we want to encrypt.
24 * @return the encrypted string.
26 public function encrypt($data) {
28 self
::check_methods($this->config
['enc_method'], $this->config
['hash_method']);
29 $iv = self
::hashIV($this->config
['key'], $this->config
['hash_method'], openssl_cipher_iv_length($this->config
['enc_method']));
30 $infostr = sprintf('$%s$%s$', $this->config
['enc_method'], $this->config
['hash_method']);
31 return $infostr . openssl_encrypt($data, $this->config
['enc_method'], $this->config
['key'], false, $iv);
35 * decrypts by using the given enc_method and hash_method.
36 * @param $edata the encrypted string that we want to decrypt
37 * @return the decrypted string.
39 public function decrypt($edata) {
40 $e_arr = explode('$', $edata);
41 if( count($e_arr) != 4 ) {
42 Throw new Exception('Given data is missing crucial sections.');
44 $this->config
['enc_method'] = $e_arr[1];
45 $this->config
['hash_method'] = $e_arr[2];
46 self
::check_methods($this->config
['enc_method'], $this->config
['hash_method']);
47 $iv = self
::hashIV($this->config
['key'], $this->config
['hash_method'], openssl_cipher_iv_length($this->config
['enc_method']));
48 return openssl_decrypt($e_arr[3], $this->config
['enc_method'], $this->config
['key'], false, $iv);
52 * hashes the key by using a hash method specified.
53 * @param $key the key to be hashed
54 * @param $method the metho of hashing to be used
55 * @param $iv_size the size of the initialization vector.
56 * @return return the hashed key up till the size of the iv_size param.
58 private static function hashIV($key, $method, $iv_size) {
59 $myhash = hash($method, $key, TRUE);
60 while( strlen($myhash) < $iv_size ) {
61 $myhash .= hash($method, $myhash, TRUE);
63 return substr($myhash, 0, $iv_size);
67 * checks if the encryption and hash methods are supported
68 * @param $enc the encryption method.
69 * @param $hash the hash method.
70 * @throw Exception in case a method is not supported.
72 private static function check_methods($enc, $hash) {
74 if( ! function_exists('openssl_encrypt') ) {
75 Throw new Exception('openssl_encrypt() not supported.');
76 } else if( ! in_array($enc, openssl_get_cipher_methods()) ) {
77 Throw new Exception('Encryption method ' . $enc . ' not supported.');
78 } else if( ! in_array(strtolower($hash), hash_algos()) ) {
79 Throw new Exception('Hashing method ' . $hash . ' not supported.');