Skip to content
Snippets Groups Projects
class.mcrypt.php 1.63 KiB
Newer Older
  • Learn to ignore specific revisions
  • Jared Hancock's avatar
    Jared Hancock committed
    <?php
    /*********************************************************************
        class.mcrypt.php
    
        Mcrypt wrapper.... nothing special at all.
    
        Peter Rotich <peter@osticket.com>
        Copyright (c)  2006-2012 osTicket
        http://www.osticket.com
    
        Released under the GNU General Public License WITHOUT ANY WARRANTY.
        See LICENSE.TXT for details.
    
        vim: expandtab sw=4 ts=4 sts=4:
    **********************************************************************/
    class Mcrypt {
        
        function encrypt($text, $salt){
    
            //if mcrypt extension is not installed--simply return unencryted text and log a warning.
            if(!function_exists('mcrypt_encrypt') || !function_exists('mcrypt_decrypt')){
                $msg='Cryptography extension mcrypt is not enabled or installed. Important text/data is being stored as plain text in database.';
                Sys::log(LOG_WARN,'mcrypt module missing',$msg);
                return $text;
            }
    
            return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256,$salt, $text, MCRYPT_MODE_ECB,
                             mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
        }
    
        function decrypt($text, $salt){
            if(!function_exists('mcrypt_encrypt') || !function_exists('mcrypt_decrypt'))
                return $text;
    
            return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $salt, base64_decode($text), MCRYPT_MODE_ECB,
                            mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
        }
    
        function exists(){
            return (function_exists('mcrypt_encrypt') && function_exists('mcrypt_decrypt'));
        }
    }
    ?>