mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
Merge pull request #663 from samsonasik/add-crypto-random-string-helper
add random_bytes() into random_string() helper
This commit is contained in:
commit
457c87fa3c
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
|
@ -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::
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user