source: trunk/workflow/inc/local/classes/class.wf_crypt.php @ 7655

Revision 7655, 4.0 KB checked in by douglasz, 11 years ago (diff)

Ticket #3236 - Melhorias de performance no codigo do Expresso.

  • Property svn:executable set to *
Line 
1<?php
2/**
3* Criptografia simples e segura baseada em funções hash   
4* @author       Marc Wöhlken, woehlken@quadracom.de
5* @author       Carlos Eduardo Nogueira Gonçalves
6* @version 1.0
7* @license http://www.gnu.org/copyleft/gpl.html GPL
8* @package Workflow
9* @subpackage local
10**/     
11class wf_crypt {
12        /**
13         * @var string $hash_key Versão embaralhada da chave de criptografia fornecida pelo usuário
14         * @access public
15         **/
16        var $hash_key;
17        /**
18         * @var int $hash_length Comprimento da string dos valores criptografados usando o algoritmo atual
19         * @access public
20         **/   
21        var $hash_length;
22        /**
23         * @var boolean $base64 Usar codificação base64
24         * @access public
25         **/   
26        var $base64;
27        /**
28         * @var string $salt Valor secreto que randomiza a saída e protege a chave fornecida pelo usuário       
29         * @access public
30         **/   
31        var $salt = 'd41d8cd98f00b204e9800998ecf8427e';
32       
33        /**
34         * Construtor
35         * @return object
36         * @access public
37         */
38        function wf_crypt() {
39                $moduleConf = parse_ini_file(PHPGW_SERVER_ROOT . SEP . 'workflow' . SEP . 'inc' . SEP . 'config' . SEP . 'module.ini', true);
40                $this->setKey($moduleConf['classes']['wf_crypt'], true);
41        }
42       
43        /**
44         * Usado para definir a chave de criptografia e descriptografia
45         * @param       string  $key    Chave secreta usada para criptografia e descriptografia
46         * @param       boolean $base64 Usar codificação base64
47         * @return void
48         * @access public
49         */
50        function setKey($key, $base64 = true) {
51                $this->base64 = $base64;               
52                $this->hash_key = $this->_hash($key);           
53                $this->hash_length = strlen($this->hash_key);
54        }
55               
56        /**
57         * Criptografa dados
58         * @param       string  $string Informação a ser criptografada
59         * @return string       Informação criptografada
60         * @access public
61         */
62        function encrypt($string) {
63                $iv = $this->_generate_iv();           
64                $out = '';             
65                for($c=0;$c < $this->hash_length;++$c) {
66                        $out .= chr(ord($iv[$c]) ^ ord($this->hash_key[$c]));
67                }
68                $key = $iv;
69                $c = 0;
70                while($c < strlen($string)) {
71                        if(($c != 0) and ($c % $this->hash_length == 0)) {
72                                $key = $this->_hash($key . substr($string,$c - $this->hash_length,$this->hash_length));
73                        }
74                        $out .= chr(ord($key[$c % $this->hash_length]) ^ ord($string[$c]));
75                        ++$c;
76                }
77                if($this->base64) $out = base64_encode($out);
78                return $out;
79        }
80       
81        /**
82         * Descriptografa
83         * @param       string  $string Informação a ser descriptografada       
84         * @return string       Informação descriptografada
85         * @access public
86         */
87        function decrypt($string) {
88                if($this->base64) $string = base64_decode($string);
89                $tmp_iv = substr($string,0,$this->hash_length);
90                $string = substr($string,$this->hash_length,strlen($string) - $this->hash_length);
91                $iv = $out = '';
92                for($c=0;$c < $this->hash_length;++$c) {
93                        $iv .= chr(ord($tmp_iv[$c]) ^ ord($this->hash_key[$c]));
94                }
95                $key = $iv;
96                $c = 0;
97                while($c < strlen($string)) {
98                        if(($c != 0) and ($c % $this->hash_length == 0)) {
99                                $key = $this->_hash($key . substr($out,$c - $this->hash_length,$this->hash_length));
100                        }
101                        $out .= chr(ord($key[$c % $this->hash_length]) ^ ord($string[$c]));
102                        ++$c;
103                }
104                return $out;
105        }
106
107        /**
108         * Função de hash usada para criptografar
109         * @access private
110         * @param       string  $string Informação a ser criptografada
111         * @return string       Valor criptografado dos dados de entrada
112         */
113        function _hash($string) {
114                if(function_exists('sha1')) {
115                        $hash = sha1($string);
116                } else {
117                        $hash = md5($string);
118                }
119                $out ='';
120                for($c=0;$c<strlen($hash);$c+=2) {
121                        $out .= $this->_hex2chr($hash[$c] . $hash[$c+1]);
122                }
123                return $out;
124        }
125       
126        /**
127         * Gera uma string aleatória para inicializar criptografia
128         * @access private
129         * @return string       String pseudo-aleatória
130         **/
131        function _generate_iv() {
132                srand ((double)microtime()*1000000);
133                $iv  = $this->salt;
134                $iv .= rand(0,getrandmax());
135                $iv .= serialize($GLOBALS);
136                return $this->_hash($iv);
137        }
138       
139        /**
140         * Converte valor hexadecimal para string binária
141         * @access private
142         * @param       string  Número hexadecimal entre 00 e ff
143         * @return      string  Caracter representando o valor de entrada
144         **/
145        function _hex2chr($num) {
146                return chr(hexdec($num));
147        }
148}
149?>
Note: See TracBrowser for help on using the repository browser.