PageRenderTime 57ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Crypt.php

https://github.com/spekkionu/spekkionu-php-class-collection
PHP | 107 lines | 39 code | 10 blank | 58 comment | 2 complexity | b96f13835dfe71a6e5462b67bada1a3d MD5 | raw file
  1. <?php
  2. /**
  3. * Encryption Class
  4. * @author Jonathan Bernardi
  5. * @copyright 2009 spekkionu <spekkionu@spekkionu.com>
  6. * @uses mcrypt extension
  7. * @package Crypt
  8. * @license MIT License
  9. */
  10. class Crypt {
  11. /**
  12. * Encryption key
  13. * @var string $_key
  14. */
  15. private $_key;
  16. /**
  17. * Encryption algorithm, defaults to MCRYPT_BLOWFISH
  18. * @var string $_algorithm
  19. */
  20. private $_algorithm;
  21. /**
  22. * Class constructor, sets encryption key and algorithm
  23. * @param string $key The Encryption key to use.
  24. * @param string $algorithm The encryption algorithm. Defaults to MCRYPT_BLOWFISH. A good algorithm is MCRYPT_RIJNDAEL_256
  25. */
  26. public function __construct($key, $algorithm = MCRYPT_BLOWFISH){
  27. // Make sure mcrypt library is available.
  28. if (!extension_loaded('mcrypt')) throw new Extension('MCRYPT extension is not loaded.');
  29. $this->setAlgorithm($algorithm);
  30. $this->setKey($key);
  31. }
  32. /**
  33. * Sets the encryption key.
  34. * @param $key
  35. * @return Crypt
  36. */
  37. public function setKey($key){
  38. $this->_key = $key;
  39. return $this;
  40. }
  41. /**
  42. * Returns the correct size key for the algorithm
  43. * @return string
  44. */
  45. private function _getKey(){
  46. $key_size = mcrypt_get_key_size($this->_algorithm, MCRYPT_MODE_ECB);
  47. $key = str_pad($this->_key, $key_size, '0');
  48. $key = substr($key, 0, $key_size);
  49. return $key;
  50. }
  51. /**
  52. * Generates a random IV
  53. * @return string
  54. */
  55. private function _generateIV(){
  56. // Generate an IV of the correct size.
  57. $iv_size = mcrypt_get_iv_size($this->_algorithm, MCRYPT_MODE_ECB);
  58. return mcrypt_create_iv($iv_size, MCRYPT_RAND);
  59. }
  60. /**
  61. *
  62. * @param $algorithm
  63. * @return Crypt
  64. */
  65. public function setAlgorithm($algorithm){
  66. // Make sure algorythm is available
  67. if(!in_array($algorithm, mcrypt_list_algorithms())) throw new Exception("MCRYPT Algorithm {$algorithm} is not available.");
  68. $this->_algorithm = $algorithm;
  69. return $this;
  70. }
  71. /**
  72. * Encrypts data
  73. * @param mixed $data
  74. * @return string
  75. */
  76. public function encrypt($data){
  77. // Serialize the data into a string
  78. $data = serialize($data);
  79. // Encrypt the data
  80. $data = mcrypt_encrypt($this->_algorithm, $this->_getKey(), $data, MCRYPT_MODE_ECB, $this->_generateIV());
  81. // Encode and trim it
  82. return trim(base64_encode($data));
  83. }
  84. /**
  85. * Decrypts data
  86. * @param string $data
  87. * @return string
  88. */
  89. public function decrypt($data){
  90. // Decode the data
  91. $data = base64_decode(trim($data));
  92. // Decrypt the data
  93. $data = mcrypt_decrypt($this->_algorithm, $this->_getKey(), $data, MCRYPT_MODE_ECB, $this->_generateIV());
  94. // Unserialize the data
  95. return unserialize(trim($data));
  96. }
  97. }