new approach to logging database access and upgraded adodb
[openemr.git] / library / adodb / session / adodb-encrypt-mcrypt.php
blob3c1ddf2779784afa76d5778e02f7d295cce2a5c9
1 <?php
4 /*
5 V5.14 8 Sept 2011 (c) 2000-2011 John Lim (jlim#natsoft.com). All rights reserved.
6 Contributed by Ross Smith (adodb@netebb.com).
7 Released under both BSD license and Lesser GPL library license.
8 Whenever there is any discrepancy between the two licenses,
9 the BSD license will take precedence.
10 Set tabs to 4 for best viewing.
14 if (!function_exists('mcrypt_encrypt')) {
15 trigger_error('Mcrypt functions are not available', E_USER_ERROR);
16 return 0;
19 /**
21 class ADODB_Encrypt_MCrypt {
22 /**
24 var $_cipher;
26 /**
28 var $_mode;
30 /**
32 var $_source;
34 /**
36 function getCipher() {
37 return $this->_cipher;
40 /**
42 function setCipher($cipher) {
43 $this->_cipher = $cipher;
46 /**
48 function getMode() {
49 return $this->_mode;
52 /**
54 function setMode($mode) {
55 $this->_mode = $mode;
58 /**
60 function getSource() {
61 return $this->_source;
64 /**
66 function setSource($source) {
67 $this->_source = $source;
70 /**
72 function ADODB_Encrypt_MCrypt($cipher = null, $mode = null, $source = null) {
73 if (!$cipher) {
74 $cipher = MCRYPT_RIJNDAEL_256;
76 if (!$mode) {
77 $mode = MCRYPT_MODE_ECB;
79 if (!$source) {
80 $source = MCRYPT_RAND;
83 $this->_cipher = $cipher;
84 $this->_mode = $mode;
85 $this->_source = $source;
88 /**
90 function write($data, $key) {
91 $iv_size = mcrypt_get_iv_size($this->_cipher, $this->_mode);
92 $iv = mcrypt_create_iv($iv_size, $this->_source);
93 return mcrypt_encrypt($this->_cipher, $key, $data, $this->_mode, $iv);
96 /**
98 function read($data, $key) {
99 $iv_size = mcrypt_get_iv_size($this->_cipher, $this->_mode);
100 $iv = mcrypt_create_iv($iv_size, $this->_source);
101 $rv = mcrypt_decrypt($this->_cipher, $key, $data, $this->_mode, $iv);
102 return rtrim($rv, "\0");
107 return 1;