diff --git a/system/Helpers/text_helper.php b/system/Helpers/text_helper.php index 22f8d1924d..7d41bcea36 100755 --- a/system/Helpers/text_helper.php +++ b/system/Helpers/text_helper.php @@ -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)); } } diff --git a/tests/system/Helpers/TextHelperTest.php b/tests/system/Helpers/TextHelperTest.php index c68ff34a32..813f6ba999 100755 --- a/tests/system/Helpers/TextHelperTest.php +++ b/tests/system/Helpers/TextHelperTest.php @@ -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() diff --git a/user_guide_src/source/helpers/text_helper.rst b/user_guide_src/source/helpers/text_helper.rst index fc3af2e4fa..473ab85a87 100755 --- a/user_guide_src/source/helpers/text_helper.rst +++ b/user_guide_src/source/helpers/text_helper.rst @@ -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::