1 | <?php |
||
15 | class ar_security_crypt extends arBase { |
||
16 | private $secret; |
||
17 | private $method; |
||
18 | const USEKEY = 1; |
||
19 | const USEPASS = 2; |
||
20 | |||
21 | private function keyRawToInternal($rawkey) { |
||
22 | static $key; |
||
23 | static $func; |
||
24 | if (is_null($key)) { |
||
25 | $key = DC\Key::createNewRandomKey(); |
||
26 | $func = function($rawkey) { |
||
27 | return new self($rawkey); |
||
28 | }; |
||
29 | $func = $func->bindto($key,$key); |
||
30 | } |
||
31 | return $func($rawkey); |
||
32 | } |
||
33 | |||
34 | public function __construct() { |
||
36 | |||
37 | public function key($key) { |
||
54 | |||
55 | public function passphrase($pass) |
||
63 | |||
64 | /* |
||
65 | * keybytes are hardcoded to 32 bytes |
||
66 | * 3 options: |
||
67 | * A) base64 encoded key |
||
68 | * B) key in the format for defuse |
||
69 | * C) not a key |
||
70 | * Only A and C are 'implemented' |
||
71 | */ |
||
72 | |||
73 | public function encrypt($data) { |
||
96 | |||
97 | public function decrypt($data){ |
||
98 | if (!isset($this->method)) { |
||
99 | return ar('error')->raiseError('use key or passphrase to init crypt engine',1000); |
||
100 | } |
||
101 | $data = base64_decode($data,true); |
||
102 | |||
103 | if ($data === false ) { |
||
104 | return ar('error')->raiseError('Invalid data, not properly base64 encoded',1000); |
||
105 | } |
||
106 | |||
107 | try { |
||
108 | $secret = $this->secret; |
||
109 | if ($this->method === self::USEKEY ) { |
||
110 | $decrypted = DC\Crypto::decrypt($data, $secret(), true); |
||
111 | } else { |
||
112 | $decrypted = DC\Crypto::decryptWithPassword($data, $secret(), true); |
||
113 | } |
||
114 | } catch (DC\Exception\EnvironmentIsBrokenException $ex) { |
||
115 | return ar('error')->raiseError('enviroment is broken', 1000); |
||
116 | } catch (DC\Exception\WrongKeyOrModifiedCiphertextException $ex) { |
||
117 | return ar('error')->raiseError('wrong key of data has been tampered with', 1000); |
||
118 | } |
||
119 | return $decrypted; |
||
120 | } |
||
121 | |||
122 | public function generateKey(){ |
||
127 | |||
128 | } |
||
129 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.