From 472dd21a7bc60147afff7bf100bbb6c9f339d0fc Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Sat, 23 Jan 2010 20:03:27 +0000 Subject: Added alpha, and sha1 string types to random_string() in the String Helper. --- system/helpers/string_helper.php | 25 ++++++++++++++++++++----- user_guide/changelog.html | 1 + user_guide/helpers/string_helper.html | 14 ++++++++------ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/system/helpers/string_helper.php b/system/helpers/string_helper.php index 67cbd0eb6..4767f0b5f 100644 --- a/system/helpers/string_helper.php +++ b/system/helpers/string_helper.php @@ -186,22 +186,27 @@ if ( ! function_exists('reduce_multiples')) * Useful for generating passwords or hashes. * * @access public - * @param string type of random string. Options: alunum, numeric, nozero, unique + * @param string type of random string. basic, alpha, alunum, numeric, nozero, unique, md5, encrypt and sha1 * @param integer number of characters * @return string - */ + */ if ( ! function_exists('random_string')) -{ +{ function random_string($type = 'alnum', $len = 8) { switch($type) { + case 'basic' : return mt_rand(); + break; case 'alnum' : case 'numeric' : case 'nozero' : + case 'alpha' : switch ($type) { + case 'alpha' : $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + break; case 'alnum' : $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; break; case 'numeric' : $pool = '0123456789'; @@ -217,12 +222,22 @@ if ( ! function_exists('random_string')) } return $str; break; - case 'unique' : return md5(uniqid(mt_rand())); + case 'unique' : + case 'md5' : + + return md5(uniqid(mt_rand())); + break; + case 'encrypt' : + case 'sha1' : + + $CI =& get_instance(); + $CI->load->helper('security'); + + return do_hash(uniqid(mt_rand(), TRUE), 'sha1'); break; } } } - // ------------------------------------------------------------------------ /** diff --git a/user_guide/changelog.html b/user_guide/changelog.html index b8601ab3a..4214a0de5 100644 --- a/user_guide/changelog.html +++ b/user_guide/changelog.html @@ -86,6 +86,7 @@ SVN Revision:

  • Modified the second parameter of directory_map() in the Directory Helper to accept an integer to specify recursion depth.
  • Modified delete_files() in the File Helper to return FALSE on failure.
  • Added an optional second parameter to byte_format() in the Number Helper to allow for decimal precision.
  • +
  • Added alpha, and sha1 string types to random_string() in the String Helper.
  • Other Changes diff --git a/user_guide/helpers/string_helper.html b/user_guide/helpers/string_helper.html index 36fa40f98..b799390c3 100644 --- a/user_guide/helpers/string_helper.html +++ b/user_guide/helpers/string_helper.html @@ -74,13 +74,15 @@ String Helper

    The first parameter specifies the type of string, the second parameter specifies the length. The following choices are available:

    - + alpha, alunum, numeric, nozero, unique, md5, encrypt and sha1

    Usage example:

    -- cgit v1.2.3-24-g4f1b