Skip to content

Commit

Permalink
BackwardsCompatibilityBreak - fCryptography::generateRandomString() w…
Browse files Browse the repository at this point in the history
…as renamed to fCryptography::randomString()
  • Loading branch information
wbond committed Apr 12, 2012
1 parent f58475f commit a4c6fc1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 61 deletions.
118 changes: 59 additions & 59 deletions classes/fCryptography.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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);
}
Expand Down Expand Up @@ -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
*
Expand Down
4 changes: 2 additions & 2 deletions classes/fORMColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit a4c6fc1

Please sign in to comment.