crypter.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php namespace Laravel\Security; use Laravel\Config;
  2. if (trim(Config::$items['application']['key']) === '')
  3. {
  4. throw new \Exception('The encryption class may not be used without an application key.');
  5. }
  6. class Crypter {
  7. /**
  8. * The encryption cipher.
  9. *
  10. * @var string
  11. */
  12. protected static $cipher = MCRYPT_RIJNDAEL_256;
  13. /**
  14. * The encryption mode.
  15. *
  16. * @var string
  17. */
  18. protected static $mode = 'cbc';
  19. /**
  20. * Encrypt a string using Mcrypt.
  21. *
  22. * The string will be encrypted using the cipher and mode specified when the
  23. * crypter instance was created, and the final result will be base64 encoded.
  24. *
  25. * <code>
  26. * // Encrypt a string using the Mcrypt PHP extension
  27. * $encrypted = Crypter::encrpt('secret');
  28. * </code>
  29. *
  30. * @param string $value
  31. * @return string
  32. */
  33. public static function encrypt($value)
  34. {
  35. // Determine the most appropriate random number generator for the
  36. // OS and system and environment the application is running on.
  37. if (defined('MCRYPT_DEV_URANDOM'))
  38. {
  39. $randomizer = MCRYPT_DEV_URANDOM;
  40. }
  41. elseif (defined('MCRYPT_DEV_RANDOM'))
  42. {
  43. $randomizer = MCRYPT_DEV_RANDOM;
  44. }
  45. else
  46. {
  47. $randomizer = MCRYPT_RAND;
  48. }
  49. $iv = mcrypt_create_iv(static::iv_size(), $randomizer);
  50. $value = mcrypt_encrypt(static::$cipher, static::key(), $value, static::$mode, $iv);
  51. return base64_encode($iv.$value);
  52. }
  53. /**
  54. * Decrypt a string using Mcrypt.
  55. *
  56. * @param string $value
  57. * @return string
  58. */
  59. public static function decrypt($value)
  60. {
  61. list($iv, $value) = static::parse(base64_decode($value, true));
  62. $value = mcrypt_decrypt(static::$cipher, static::key(), $value, static::$mode, $iv);
  63. return rtrim($value, "\0");
  64. }
  65. /**
  66. * Parse an encrypted value into the input vector and the actual value.
  67. *
  68. * @param string $value
  69. * @return array
  70. */
  71. protected static function parse($value)
  72. {
  73. if ($value === false)
  74. {
  75. throw new \Exception('Decryption error. Input value is not valid base64 data.');
  76. }
  77. return array(substr($value, 0, static::iv_size()), substr($value, static::iv_size()));
  78. }
  79. /**
  80. * Get the input vector size for the cipher and mode.
  81. *
  82. * @return int
  83. */
  84. protected static function iv_size()
  85. {
  86. return mcrypt_get_iv_size(static::$cipher, static::$mode);
  87. }
  88. /**
  89. * Get the encryption key from the application configuration.
  90. *
  91. * @return string
  92. */
  93. protected static function key()
  94. {
  95. return Config::$items['application']['key'];
  96. }
  97. }