note 63484 added to function.mcrypt-module-open

From: Date: Wed, 22 Mar 2006 20:18:53 +0000
Subject: note 63484 added to function.mcrypt-module-open
Groups: php.notes 
Request: Send a blank email to php-notes+get-106350@lists.php.net to get a copy of this message
I needed to have a way of encrypting and decrypting information in a database, and the mcrypt library seemed like the best way to do that. After reading the documentation and examples I created two functions. One was to encrypt data and the other was to decrypt data. Here is basically what I wrote at first: function myencrypt($data, $key) { $td = mcrypt_module_open('tripledes', '', 'ecb', ''); $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM); $ks = mcrypt_enc_get_key_size($td); mcrypt_generic_init($td, $key, $iv); $encrypted = mcrypt_generic($td, $data); mcrypt_generic_deinit($td); mcrypt_module_close($td); return base64_encode($encrypted); } function mydecrypt($encrypted_data, $key) { $td = mcrypt_module_open('tripledes', '', 'ecb', ''); $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM); $ks = mcrypt_enc_get_key_size($td); mcrypt_generic_init($td, $key, $iv); $decrypted = mdecrypt_generic($td, base64_decode( $encrypted_data )); mcrypt_generic_deinit($td); mcrypt_module_close($td); return $decrypted; } After writing the functions I began to test them. These functions would run well for about the first 50-100 times, and then pause for a while (I imagine that the program was blocked waiting for a resource) and then run 50-100 more and pause again. I think the problem is that the function was calling mcrypt_module_open each time it ran and that must make a system call to allocate a file handle, or some kind of resource handle. To fix this I replaced the functions with the following: $__td = mcrypt_module_open('tripledes', '', 'ecb', ''); $__iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($__td), MCRYPT_DEV_RANDOM); $__ks = mcrypt_enc_get_key_size($__td); function myencrypt($data, $key) { global $__td, $__iv, $__ks; mcrypt_generic_init($__td, $key, $__iv); $encrypted = mcrypt_generic($__td, $data); mcrypt_generic_deinit($__td); return base64_encode($encrypted); } function mydecrypt($encrypted_data, $key) { global $__td, $__iv, $__ks; mcrypt_generic_init($__td, $key, $__iv); $decrypted = mdecrypt_generic($__td, base64_decode($encrypted_data)); mcrypt_generic_deinit($__td); return $decrypted; } Since it uses a global handle to the mcrypt module, it never has to wait for the system to return a handle. Benchmarked these new functions with a test script that did 10000 encrypts and decrypts and it ran perfectly. For those of you that don't like to allocate something and never de-allocate it, rest assured that PHP will clean it up. One of the beauties of PHP. ---- Server IP: 216.194.113.175 Probable Submitter: 71.36.92.167 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.mcrypt-module-open.php Edit -- http://master.php.net/note/edit/63484 Del: integrated -- http://master.php.net/note/delete/63484/integrated Del: useless -- http://master.php.net/note/delete/63484/useless Del: bad code -- http://master.php.net/note/delete/63484/bad+code Del: spam -- http://master.php.net/note/delete/63484/spam Del: non-english -- http://master.php.net/note/delete/63484/non-english Del: in docs -- http://master.php.net/note/delete/63484/in+docs Del: other reasons-- http://master.php.net/note/delete/63484 Reject -- http://master.php.net/note/reject/63484 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#106350) next »