crypter.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php namespace Laravel;
  2. class Crypter {
  3. /**
  4. * The encryption cipher.
  5. *
  6. * @var string
  7. */
  8. public $cipher;
  9. /**
  10. * The encryption mode.
  11. *
  12. * @var string
  13. */
  14. public $mode;
  15. /**
  16. * The encryption key.
  17. *
  18. * @var string
  19. */
  20. public $key;
  21. /**
  22. * Create a new Crypter instance.
  23. *
  24. * @param string $cipher
  25. * @param string $mode
  26. * @param string $key
  27. * @return void
  28. */
  29. public function __construct($cipher, $mode, $key)
  30. {
  31. $this->cipher = $cipher;
  32. $this->mode = $mode;
  33. $this->key = $key;
  34. if (trim((string) $this->key) === '')
  35. {
  36. throw new \Exception('The encryption class can not be used without an encryption key.');
  37. }
  38. }
  39. /**
  40. * Create a new Crypter instance.
  41. *
  42. * @param string $cipher
  43. * @param string $mode
  44. * @param string $key
  45. * @return Crypt
  46. */
  47. public static function make($cipher = MCRYPT_RIJNDAEL_256, $mode = 'cbc', $key = null)
  48. {
  49. return new static($cipher, $mode, (is_null($key)) ? Config::get('application.key') : $key);
  50. }
  51. /**
  52. * Encrypt a value using the MCrypt library.
  53. *
  54. * @param string $value
  55. * @return string
  56. */
  57. public function encrypt($value)
  58. {
  59. $iv = mcrypt_create_iv($this->iv_size(), $this->randomizer());
  60. return base64_encode($iv.mcrypt_encrypt($this->cipher, $this->key, $value, $this->mode, $iv));
  61. }
  62. /**
  63. * Get the random number source available to the OS.
  64. *
  65. * @return int
  66. */
  67. protected function randomizer()
  68. {
  69. if (defined('MCRYPT_DEV_URANDOM'))
  70. {
  71. return MCRYPT_DEV_URANDOM;
  72. }
  73. elseif (defined('MCRYPT_DEV_RANDOM'))
  74. {
  75. return MCRYPT_DEV_RANDOM;
  76. }
  77. return MCRYPT_RAND;
  78. }
  79. /**
  80. * Decrypt a value using the MCrypt library.
  81. *
  82. * @param string $value
  83. * @return string
  84. */
  85. public function decrypt($value)
  86. {
  87. if ( ! is_string($value = base64_decode($value, true)))
  88. {
  89. throw new \Exception('Decryption error. Input value is not valid base64 data.');
  90. }
  91. list($iv, $value) = array(substr($value, 0, $this->iv_size()), substr($value, $this->iv_size()));
  92. return rtrim(mcrypt_decrypt($this->cipher, $this->key, $value, $this->mode, $iv), "\0");
  93. }
  94. /**
  95. * Get the input vector size for the cipher and mode.
  96. *
  97. * Different ciphers and modes use varying lengths of input vectors.
  98. *
  99. * @return int
  100. */
  101. private function iv_size()
  102. {
  103. return mcrypt_get_iv_size($this->cipher, $this->mode);
  104. }
  105. }