Permalink
Browse files

BackwardsCompatibilityBreak - fCryptography::generateRandomString() w…

…as renamed to fCryptography::randomString()
  • Loading branch information...
wbond committed Sep 13, 2008
1 parent f58475f commit a4c6fc121fbf12568d5a1873a1e308190ea9e80f
Showing with 61 additions and 61 deletions.
  1. +59 −59 classes/fCryptography.php
  2. +2 −2 classes/fORMColumn.php
View
@@ -33,64 +33,6 @@ static public function checkPasswordHash($password, $hash)
}
/**
* Returns a random string of the type and length specified
*
* @param integer $length The length of string to return
* @param string $type The type of string to return, can be 'alphanumeric', 'alpha', 'numeric', or 'hexadecimal'
* @return string A random string of the type and length specified
*/
static public function generateRandomString($length, $type='alphanumeric')
{
if ($length < 1) {
fCore::toss(
'fProgrammerException',
fGrammar::compose(
'The length specified, %1$s, is less than the minimum of %2$s',
$length,
1
)
);
}
switch ($type) {
case 'alphanumeric':
$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
break;
case 'alpha':
$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case 'numeric':
$alphabet = '0123456789';
break;
case 'hexadecimal':
$alphabet = 'abcdef0123456789';
break;
default:
fCore::toss(
'fProgrammerException',
fGrammar::compose(
'The type specified, %1$s, is invalid. Must be one of: %2$s.',
fCore::dump($type)
)
);
}
$alphabet_length = strlen($alphabet);
$output = '';
for ($i = 0; $i < $length; $i++) {
$output .= $alphabet[self::random(0, $alphabet_length-1)];
}
return $output;
}
/**
* Hashes a password using a loop of sha1 hashes and a salt, making rainbow table attacks infeasible
*
@@ -99,7 +41,7 @@ static public function generateRandomString($length, $type='alphanumeric')
*/
static public function hashPassword($password)
{
$salt = self::generateRandomString(10);
$salt = self::randomString(10);
return self::hashWithSalt($password, $salt);
}
@@ -273,6 +215,64 @@ static public function random($min=NULL, $max=NULL)
}
/**
* Returns a random string of the type and length specified
*
* @param integer $length The length of string to return
* @param string $type The type of string to return, can be 'alphanumeric', 'alpha', 'numeric', or 'hexadecimal'
* @return string A random string of the type and length specified
*/
static public function randomString($length, $type='alphanumeric')
{
if ($length < 1) {
fCore::toss(
'fProgrammerException',
fGrammar::compose(
'The length specified, %1$s, is less than the minimum of %2$s',
$length,
1
)
);
}
switch ($type) {
case 'alphanumeric':
$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
break;
case 'alpha':
$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case 'numeric':
$alphabet = '0123456789';
break;
case 'hexadecimal':
$alphabet = 'abcdef0123456789';
break;
default:
fCore::toss(
'fProgrammerException',
fGrammar::compose(
'The type specified, %1$s, is invalid. Must be one of: %2$s.',
fCore::dump($type)
)
);
}
$alphabet_length = strlen($alphabet);
$output = '';
for ($i = 0; $i < $length; $i++) {
$output .= $alphabet[self::random(0, $alphabet_length-1)];
}
return $output;
}
/**
* Makes sure that the PRNG has been seeded with a fairly secure value
*
View
@@ -616,7 +616,7 @@ static public function setRandomStrings($object, &$values, &$old_values, &$relat
if ($unique_key == array($column)) {
$is_unique_column = TRUE;
do {
$value = fCryptography::generateRandomString($settings['length'], $settings['type']);
$value = fCryptography::randomString($settings['length'], $settings['type']);
// See if this is unique
$sql = "SELECT " . $column . " FROM " . $table . " WHERE " . $column . " = " . fORMDatabase::escapeByType($value);
@@ -627,7 +627,7 @@ static public function setRandomStrings($object, &$values, &$old_values, &$relat
// If is is not a unique column, just generate a value
if (!$is_unique_column) {
$value = fCryptography::generateRandomString($settings['length'], $settings['type']);
$value = fCryptography::randomString($settings['length'], $settings['type']);
}
fActiveRecord::assign($values, $old_values, $column, $value);

0 comments on commit a4c6fc1

Please sign in to comment.