CodeIgniter Extended Encryption Class


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. /**
  4.  * CodeIgniter Extended Encryption Class
  5.  *
  6.  * An extension to CI's encryption class to provide a lot more hash algorithms. PHP
  7.  * 5.1.2+ is highly recommended to take full advantage of this class, however extra
  8.  * user made hash functions can be used as if they are added to the
  9.  * 'MY_Encrypt::_custom_hashes' array.
  10.  *
  11.  * @package CodeIgniter
  12.  * @subpackage Extended libraries
  13.  * @category Extended libraries
  14.  * @author Jeffy
  15.  * @link
  16.  */
  17.  
  18. class MY_Encrypt extends CI_Encrypt
  19. {
  20. // Built-in CodeIgniter hash algorithms
  21. var $_builtin_hashes = array(
  22. 'sha1',
  23. 'md5',
  24. );
  25. // Custom hash algorithms
  26. var $_custom_hashes = array(
  27. );
  28.  
  29. function MY_Encrypt()
  30. {
  31. parent::CI_Encrypt();
  32. }
  33.  
  34. function set_hash($type = 'sha1')
  35. {
  36. if ($this->hash_exists($type))
  37. {
  38. $this->_hash_type = $type;
  39. }
  40. }
  41.  
  42. function hash($str)
  43. {
  44. if ($this->hash_exists($this->_hash_type))
  45. {
  46. // CodeIgniter built-in hashes
  47. if (in_array($this->_hash_type, $this->_builtin_hashes))
  48. {
  49. return parent::hash($str);
  50. // Hash algorithms available using the PHP5 'hash' function
  51. } elseif (function_exists('hash') AND in_array($this->_hash_type, @hash_algos())) {
  52. return hash($this->_hash_type, $str);
  53. // Hash algorithms available using the PHP5 'openssl_digest' function
  54. } elseif (function_exists('openssl_digest') AND in_array($this->_hash_type, @openssl_get_md_methods())) {
  55. return openssl_digest($str, $this->_hash_type);
  56. // Custom hash functions/algorithms referenced by the $_custom_hashes array
  57. } elseif (@in_array($this->_hash_type, $this->_custom_hashes)) {
  58. if (method_exists($this, $this->_hash_type))
  59. {
  60. return call_user_func(array($this, $this->_hash_type), $str);
  61. }
  62. }
  63. } else {
  64. // Default to SHA1 instead of MD5.
  65. $this->_hash_type = 'sha1';
  66.  
  67. return parent::hash($str);
  68. }
  69. }
  70.  
  71. function hash_exists($type)
  72. {
  73. return (
  74. in_array($type, $this->_builtin_hashes) OR
  75. in_array($type, $this->_custom_hashes) OR
  76. function_exists('hash') AND in_array($type, @hash_algos()) OR
  77. function_exists('openssl_digest') AND in_array($type, @openssl_get_md_methods())
  78. );
  79. }
  80. }
  81. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.