improve crypt class comments.

This commit is contained in:
Taylor Otwell 2011-06-16 20:33:15 -05:00
parent fb66bf532d
commit 825ac35a47
1 changed files with 12 additions and 7 deletions

View File

@ -25,7 +25,8 @@ class Crypt {
public static function encrypt($value)
{
// -----------------------------------------------------
// Determine the input vector source.
// Determine the input vector source. Different servers
// and operating systems will have varying options.
// -----------------------------------------------------
if (defined('MCRYPT_DEV_URANDOM'))
{
@ -41,21 +42,19 @@ public static function encrypt($value)
}
// -----------------------------------------------------
// The system random number generator must be seeded.
// The system random number generator must be seeded
// to produce adequately random results.
// -----------------------------------------------------
if ($random === MCRYPT_RAND)
{
mt_srand();
}
// -----------------------------------------------------
// Create the Mcrypt input vector and encrypt the value.
// -----------------------------------------------------
$iv = mcrypt_create_iv(static::iv_size(), $random);
$value = mcrypt_encrypt(static::$cipher, static::key(), $value, static::$mode, $iv);
// -----------------------------------------------------
// Use base64 encoding to get a string value.
// We use base64 encoding to get a nice string value.
// -----------------------------------------------------
return base64_encode($iv.$value);
}
@ -68,6 +67,10 @@ public static function encrypt($value)
*/
public static function decrypt($value)
{
// -----------------------------------------------------
// Since all of our encrypted values are base64 encoded,
// we will decode the value here and verify it.
// -----------------------------------------------------
$value = base64_decode($value, true);
if ( ! $value)
@ -81,7 +84,7 @@ public static function decrypt($value)
$iv = substr($value, 0, static::iv_size());
// -----------------------------------------------------
// Remove the input vector from the value.
// Remove the input vector from the encrypted value.
// -----------------------------------------------------
$value = substr($value, static::iv_size());
@ -106,6 +109,8 @@ private static function key()
/**
* Get the input vector size for the cipher and mode.
*
* Different ciphers and modes use varying lengths of input vectors.
*
* @return int
*/
private static function iv_size()