crypter.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. $iv = mcrypt_create_iv(static::iv_size(), static::randomizer());
  36. $value = mcrypt_encrypt(static::$cipher, static::key(), $value, static::$mode, $iv);
  37. return base64_encode($iv.$value);
  38. }
  39. /**
  40. * Get the random number generator appropriate for the server.
  41. *
  42. * There are a variety of sources to get a random number; however, not all
  43. * of them will be available on every server. We will attempt to use the
  44. * most secure random number generator available.
  45. *
  46. * @return int
  47. */
  48. protected static function randomizer()
  49. {
  50. foreach (array('MCRYPT_DEV_URANDOM', 'MCRYPT_DEV_RANDOM', 'MCRYPT_RAND') as $generator)
  51. {
  52. if (defined($generator)) return constant($generator);
  53. }
  54. }
  55. /**
  56. * Decrypt a string using Mcrypt.
  57. *
  58. * @param string $value
  59. * @return string
  60. */
  61. public static function decrypt($value)
  62. {
  63. list($iv, $value) = static::parse(base64_decode($value, true));
  64. $value = mcrypt_decrypt(static::$cipher, static::key(), $value, static::$mode, $iv);
  65. return rtrim($value, "\0");
  66. }
  67. /**
  68. * Parse an encrypted value into the input vector and the actual value.
  69. *
  70. * @param string $value
  71. * @return array
  72. */
  73. protected static function parse($value)
  74. {
  75. if ($value === false)
  76. {
  77. throw new \Exception('Decryption error. Input value is not valid base64 data.');
  78. }
  79. return array(substr($value, 0, static::iv_size()), substr($value, static::iv_size()));
  80. }
  81. /**
  82. * Get the input vector size for the cipher and mode.
  83. *
  84. * @return int
  85. */
  86. protected static function iv_size()
  87. {
  88. return mcrypt_get_iv_size(static::$cipher, static::$mode);
  89. }
  90. /**
  91. * Get the encryption key from the application configuration.
  92. *
  93. * @return string
  94. */
  95. protected static function key()
  96. {
  97. return Config::$items['application']['key'];
  98. }
  99. }