Browse Source

refactoring the crypter class.

Taylor Otwell 13 years ago
parent
commit
0be75fc2d9
1 changed files with 21 additions and 13 deletions
  1. 21 13
      laravel/security/crypter.php

+ 21 - 13
laravel/security/crypter.php

@@ -37,26 +37,34 @@ class Crypter {
 	 */
 	public static function encrypt($value)
 	{
-		// Determine the most appropriate random number generator for the
-		// OS and system and environment the application is running on.
+		$iv = mcrypt_create_iv(static::iv_size(), static::randomizer());
+
+		$value = mcrypt_encrypt(static::$cipher, static::key(), $value, static::$mode, $iv);
+
+		return base64_encode($iv.$value);
+	}
+
+	/**
+	 * Get the random number generator appropriate for the server.
+	 *
+	 * There are a variety of sources to get a random number; however, not all
+	 * of them will be available on every server. We will attempt to use the
+	 * most secure random number generator available.
+	 *
+	 * @return int
+	 */
+	protected static function randomizer()
+	{
 		if (defined('MCRYPT_DEV_URANDOM'))
 		{
-			$randomizer = MCRYPT_DEV_URANDOM;
+			return MCRYPT_DEV_URANDOM;
 		}
 		elseif (defined('MCRYPT_DEV_RANDOM'))
 		{
-			$randomizer = MCRYPT_DEV_RANDOM;
-		}
-		else
-		{
-			$randomizer = MCRYPT_RAND;			
+			return MCRYPT_DEV_RANDOM;
 		}
 
-		$iv = mcrypt_create_iv(static::iv_size(), $randomizer);
-
-		$value = mcrypt_encrypt(static::$cipher, static::key(), $value, static::$mode, $iv);
-
-		return base64_encode($iv.$value);
+		return MCRYPT_RAND;			
 	}
 
 	/**