Skip to content

Commit 8f17148

Browse files
committed
Update random_string helper
1 parent 28bdb19 commit 8f17148

File tree

1 file changed

+49
-10
lines changed

1 file changed

+49
-10
lines changed

system/helpers/string_helper.php

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,16 @@ function reduce_multiples($str, $character = ',', $trim = FALSE)
193193

194194
// ------------------------------------------------------------------------
195195

196-
if ( ! function_exists('random_string'))
197-
{
196+
if (!function_exists('random_string')) {
198197
/**
199-
* Create a "Random" String
198+
* Creates a random string of characters
199+
*
200+
* From FuelPHP
200201
*
201-
* Function update by FuelPHP
202+
* @param string $type the type of string
203+
* @param int $length the number of characters
202204
*
203-
* @param string $type type of random string. basic, alpha, alnum, numeric, nozero, unique, md5, encrypt and sha1
204-
* @param int $length number of characters
205-
* @return string the random string
205+
* @return string the random string
206206
*/
207207
function random_string($type = 'alnum', $length = 16)
208208
{
@@ -257,11 +257,21 @@ function random_string($type = 'alnum', $length = 16)
257257
case 'unique':
258258
return md5(uniqid(mt_rand(), true));
259259
break;
260-
261260
case 'sha1' :
262261
return sha1(uniqid(mt_rand(), true));
263262
break;
264-
263+
case 'sha256' :
264+
return hash('sha256', uniqid(mt_rand(), true));
265+
break;
266+
case 'sha384' :
267+
return hash('sha384', uniqid(mt_rand(), true));
268+
break;
269+
case 'sha512' :
270+
return hash('sha512', uniqid(mt_rand(), true));
271+
break;
272+
case 'whirlpool' :
273+
return hash('whirlpool', uniqid(mt_rand(), true));
274+
break;
265275
case 'uuid':
266276
$pool = array('8', '9', 'a', 'b');
267277

@@ -271,7 +281,36 @@ function random_string($type = 'alnum', $length = 16)
271281
random_string('hexdec', 3),
272282
$pool[array_rand($pool)],
273283
random_string('hexdec', 3),
274-
random_string('hexdec', 12));
284+
random_string('hexdec', 12)
285+
);
286+
break;
287+
case 'binary':
288+
if (function_exists('random_bytes')) {
289+
try {
290+
return random_bytes($length);
291+
}catch (\Exception $e){
292+
log_message('debug', 'Error Code: '.$e->getCode().' - File: '.$e->getFile().' - Line: '.$e->getLine().' - Message: '.$e->getMessage());
293+
return null;
294+
}
295+
} else {
296+
return null;
297+
}
298+
break;
299+
case 'hex':
300+
case 'crypto':
301+
if ($length % 2 !== 0) {
302+
throw new InvalidArgumentException('You must set an even number to the second parameter when you use `crypto`.');
303+
}
304+
if (function_exists('random_bytes')) {
305+
try {
306+
return bin2hex(random_bytes($length / 2));
307+
}catch (\Exception $e){
308+
log_message('debug', 'Error Code: '.$e->getCode().' - File: '.$e->getFile().' - Line: '.$e->getLine().' - Message: '.$e->getMessage());
309+
return null;
310+
}
311+
} else {
312+
return null;
313+
}
275314
break;
276315
}
277316
}

0 commit comments

Comments
 (0)