note 45044 modified in ref.mcrypt by nlopess

From: Date: Sun, 18 Sep 2005 09:19:32 +0000
Subject: note 45044 modified in ref.mcrypt by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-95361@lists.php.net to get a copy of this message
This is a modified version of a previous script that test the algorithms and modes of mcrypt dll. This new script prints out the result in a table and avoid warnings to be printed. <?PHP /* run a self-test through every listed cipher and mode */ function mcrypt_check_sanity() { $modes = mcrypt_list_modes(); $algorithms = mcrypt_list_algorithms(); echo "<table border=1>"; echo "<tr><td align=center><strong>Algorithm</strong></td align=center><td><strong>Status</strong></td>"; foreach ($modes as $mode) echo "<td align=center><strong>".strtoupper($mode)."</strong></td>"; echo "</tr>"; foreach ($algorithms as $cipher) { echo "<tr><td bgcolor=f0f0ff align=left>".strtoupper($cipher)."</td>"; if(mcrypt_module_self_test($cipher)) { print "<td bgcolor=green align=center>OK</td>"; } else { print "<td bgcolor=red align=center>NOT OK</td>"; } foreach ($modes as $mode) { if($mode == 'stream') { $result = "<td bgcolor=gray align=center>NOT TESTED</td>"; } else if(mcrypt_test_module_mode($cipher,$mode)) { $result = "<td bgcolor=green align=center><strong>OK</strong></td>"; } else { $result = "<td bgcolor=red align=center>NOT OK</td>"; } print $result; } echo "</tr>"; } echo "</table>"; } // a variant on the example posted in mdecrypt_generic function mcrypt_test_module_mode($module,$mode) { /* Data */ $key = 'this is a very long key, even too long for the cipher'; $plain_text = 'very important data'; /* Open module, and create IV */ $td = mcrypt_module_open($module, '',$mode, ''); $key = substr($key, 0, mcrypt_enc_get_key_size($td)); $iv_size = mcrypt_enc_get_iv_size($td); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); /* Initialize encryption handle */ if (mcrypt_generic_init($td, $key, $iv) != -1) { /* Encrypt data */ $c_t = mcrypt_generic($td, $plain_text); mcrypt_generic_deinit($td); // close the module mcrypt_module_close($td); /* Reinitialize buffers for decryption */ /* Open module */ $td = mcrypt_module_open($module, '', $mode, ''); $key = substr($key, 0, mcrypt_enc_get_key_size($td)); mcrypt_generic_init($td, $key, $iv); $p_t = trim(mdecrypt_generic($td, $c_t)); //trim to remove padding /* Clean up */ mcrypt_generic_end($td); mcrypt_module_close($td); } if (strncmp($p_t, $plain_text, strlen($plain_text)) == 0) { return TRUE; } else { return FALSE; } } // function call: @mcrypt_check_sanity(); ?> --was-- This is a modified version of a previous script that test the algorithms and modes of mcrypt dll. This new script prints out the result in a table and avoid warnings to be printed. <?PHP /* run a self-test through every listed cipher and mode */ function mcrypt_check_sanity() { $modes = mcrypt_list_modes(); $algorithms = mcrypt_list_algorithms(); echo "<table border=1>"; echo "<tr><td align=center><strong>Algorithm</strong></td align=center><td><strong>Status</strong></td>"; foreach ($modes as $mode) echo "<td align=center><strong>".strtoupper($mode)."</strong></td>"; echo "</tr>"; foreach ($algorithms as $cipher) { echo "<tr><td bgcolor=f0f0ff align=left>".strtoupper($cipher)."</td>"; if(mcrypt_module_self_test($cipher)) { print "<td bgcolor=green align=center>OK</td>"; } else { print "<td bgcolor=red align=center>NOT OK</td>"; } foreach ($modes as $mode) { if($mode == 'stream') { $result = "<td bgcolor=gray align=center>NOT TESTED</td>"; } else if(mcrypt_test_module_mode($cipher,$mode)) { $result = "<td bgcolor=green align=center><strong>OK</strong></td>"; } else { $result = "<td bgcolor=red align=center>NOT OK</td>"; } print $result; } echo "</tr>"; } echo "</table>"; } // a variant on the example posted in mdecrypt_generic function mcrypt_test_module_mode($module,$mode) { /* Data */ $key = 'this is a very long key, even too long for the cipher'; $plain_text = 'very important data'; /* Open module, and create IV */ $td = mcrypt_module_open($module, '',$mode, ''); $key = substr($key, 0, mcrypt_enc_get_key_size($td)); $iv_size = mcrypt_enc_get_iv_size($td); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); /* Initialize encryption handle */ if (mcrypt_generic_init($td, $key, $iv) != -1) { /* Encrypt data */ $c_t = mcrypt_generic($td, $plain_text); mcrypt_generic_deinit($td); // close the module mcrypt_module_close($td); /* Reinitialize buffers for decryption */ /* Open module */ $td = mcrypt_module_open($module, '', $mode, ''); $key = substr($key, 0, mcrypt_enc_get_key_size($td)); mcrypt_generic_init($td, $key, $iv); $p_t = trim(mdecrypt_generic($td, $c_t)); //trim to remove padding /* Clean up */ mcrypt_generic_end($td); mcrypt_module_close($td); } if (strncmp($p_t, $plain_text, strlen($plain_text)) == 0) { return TRUE; } else { return FALSE; } } // function call: @mcrypt_check_sanity(); ?> http://php.net/manual/en/ref.mcrypt.php

« previous php.notes (#95361) next »