PHP CodeIgniter扩展加密类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP CodeIgniter扩展加密类相关的知识,希望对你有一定的参考价值。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

/**
 * CodeIgniter Extended Encryption Class
 *
 * An extension to CI's encryption class to provide a lot more hash algorithms. PHP
 * 5.1.2+ is highly recommended to take full advantage of this class, however extra
 * user made hash functions can be used as if they are added to the
 * 'MY_Encrypt::_custom_hashes' array.
 *
 * @package		CodeIgniter
 * @subpackage	Extended libraries
 * @category	Extended libraries
 * @author		Jeffy
 * @link		
 */

    class MY_Encrypt extends CI_Encrypt
    {
        // Built-in CodeIgniter hash algorithms
        var $_builtin_hashes = array(
            'sha1',
            'md5',
            );
        // Custom hash algorithms
        var $_custom_hashes = array(
            );
        
        function MY_Encrypt()
        {
            parent::CI_Encrypt();
        }
        
        function set_hash($type = 'sha1')
        {
            if ($this->hash_exists($type))
            {
                $this->_hash_type = $type;
            }
        }
        
        function hash($str)
        {
            if ($this->hash_exists($this->_hash_type))
            {
                // CodeIgniter built-in hashes
                if (in_array($this->_hash_type, $this->_builtin_hashes))
                {
                    return parent::hash($str);
                // Hash algorithms available using the PHP5 'hash' function
                } elseif (function_exists('hash') AND in_array($this->_hash_type, @hash_algos())) {
                    return hash($this->_hash_type, $str);
                // Hash algorithms available using the PHP5 'openssl_digest' function	
                } elseif (function_exists('openssl_digest') AND in_array($this->_hash_type, @openssl_get_md_methods())) {
                    return openssl_digest($str, $this->_hash_type);
                // Custom hash functions/algorithms referenced by the $_custom_hashes array
                } elseif (@in_array($this->_hash_type, $this->_custom_hashes)) {
                    if (method_exists($this, $this->_hash_type))
                    {
                        return call_user_func(array($this, $this->_hash_type), $str);
                    }	
                }
            } else {
                // Default to SHA1 instead of MD5.
                $this->_hash_type = 'sha1';
                
                return parent::hash($str);
            }
        }
        
        function hash_exists($type)
        {
            return (
                in_array($type, $this->_builtin_hashes) OR 
                in_array($type, $this->_custom_hashes) OR 
                function_exists('hash') AND in_array($type, @hash_algos()) OR 
                function_exists('openssl_digest') AND in_array($type, @openssl_get_md_methods())
                );
        }
    }
?>

以上是关于PHP CodeIgniter扩展加密类的主要内容,如果未能解决你的问题,请参考以下文章

CodeIgniter扩展加密类

CodeIgniter 加密类到 java

CodeIgniter 2:如何多次扩展 CI_Controller?

在 Codeigniter 中扩展表单验证

如何使 CodeIgniter 文件上传类接受所有扩展?

如何扩展 CodeIgniter 数据库实用程序类