Merge pull request #663 from samsonasik/add-crypto-random-string-helper

add random_bytes() into random_string() helper
This commit is contained in:
Lonnie Ezell 2017-08-07 22:36:38 -05:00 committed by GitHub
commit 457c87fa3c
3 changed files with 6 additions and 1 deletions

View File

@ -709,7 +709,7 @@ if ( ! function_exists('random_string'))
*
* Useful for generating passwords or hashes.
*
* @param string $type Type of random string. basic, alpha, alnum, numeric, nozero, unique, md5, encrypt and sha1
* @param string $type Type of random string. basic, alpha, alnum, numeric, nozero, unique, md5, sha1, and crypto
* @param int $len Number of characters
*
* @return string
@ -745,6 +745,8 @@ if ( ! function_exists('random_string'))
return md5(uniqid(mt_rand(), true));
case 'sha1':
return sha1(uniqid(mt_rand(), true));
case 'crypto':
return bin2hex(random_bytes($len/2));
}
}

View File

@ -85,6 +85,8 @@ class TextHelperTest extends \CIUnitTestCase
{
$this->assertEquals(16, strlen(random_string('alnum', 16)));
$this->assertInternalType('string', random_string('numeric', 16));
$this->assertEquals(16, strlen($random = random_string('crypto', 16)));
$this->assertInternalType('string', $random);
}
// --------------------------------------------------------------------
public function test_increment_string()

View File

@ -43,6 +43,7 @@ The following functions are available:
- **nozero**: Numeric string with no zeros.
- **md5**: An encrypted random number based on ``md5()`` (fixed length of 32).
- **sha1**: An encrypted random number based on ``sha1()`` (fixed length of 40).
- **crypto**: A random string based on ``random_bytes()``.
Usage example::