Overview

Packages

  • Onion::Controllers
  • Onion::Core
  • Onion::UI
  • Onion::Utils

Classes

  • Arrays
  • Assets
  • Filesystem
  • Image
  • Languages
  • Mail
  • Secure
  • Strings
  • SWF
  • Utils
  • Validate
  • Visitor
  • Overview
  • Package
  • Class
  • Tree
 1: <?php
 2: 
 3: /**
 4:  * Onion Framework - Śifrovanie
 5:  *
 6:  * Copyright (c) 2011 Jano Gašpar (http://webstranky.net)
 7:  *
 8:  * @author    Jano Gašpar
 9:  * @copyright Copyright (c) 2011 Jano Gašpar
10:  * @package   Onion::Utils
11:  **/
12: 
13: class Secure
14: {
15:     private static $algorithm = MCRYPT_BLOWFISH;
16: 
17: 
18:     private static $mode = MCRYPT_MODE_CBC;
19: 
20: 
21:     private static $resource;
22: 
23: 
24:     public static $encode = TRUE;
25: 
26: 
27:     public static function encrypt($data, $password)
28:     {
29:         $data = base64_encode(serialize($data));
30:         $resource = self::initialize($password);
31:         $encrypted_data = mcrypt_generic(self::$resource, $data);
32: 
33:         if (self::$encode === TRUE) {
34:             $encrypted_data = base64_encode($encrypted_data);
35:         }
36: 
37:         return self::deinitialize($encrypted_data);
38:     }
39: 
40: 
41:     public static function decrypt($encrypted_data, $password)
42:     {
43:         if (self::$encode === TRUE) {
44:             $encrypted_data = base64_decode($encrypted_data);
45:         }
46: 
47:         self::initialize($password);
48:         $data = mdecrypt_generic(self::$resource, $encrypted_data);
49:         $data = unserialize(base64_decode($data));
50:         return self::deinitialize($data);
51:     }
52: 
53: 
54:     private static function initialize($password)
55:     {
56:         $resource = mcrypt_module_open(self::$algorithm, '', self::$mode, '');
57: 
58:         if ($resource === FALSE) {
59:             trigger_error('Cannot initialize mcrypt module!.', E_USER_ERROR);
60:         }
61: 
62:         $iv = md5($password);
63:         $iv_size = mcrypt_enc_get_iv_size($resource);
64:         $iv = substr($iv, 0, $iv_size);
65:         mcrypt_generic_init($resource, $password, $iv);
66: 
67:         self::$resource = $resource;
68:     }
69: 
70: 
71:     private static function deinitialize($data)
72:     {
73:         mcrypt_generic_deinit(self::$resource);
74:         mcrypt_module_close(self::$resource);
75: 
76:         return $data;
77:     }
78: }
79: 
Onion API documentation generated by ApiGen.
Generated using the TokenReflection library.