CakePHP Hash Console

A cakephp shell for generating a hash using your applications salt. Just drop it in and output and/or save a new user from the commandline.

1. Create the file /app/vendors/shells/hash.php in your app and paste the below in:

Show Plain Text
  1.  
  2. <?php
  3. /**
  4.  * @desc simple shell for generating and storing a user's md5 credentials
  5.  * @tutorial add hash.php to app/vendors/shells and run /cake/console/cake hash
  6.  * @author joshskeen josh@joshskeen.com
  7.  *
  8.  */
  9. class HashShell extends Shell{
  10.     var $username_field = 'username';
  11.     var $password_field = 'password';
  12.     var $usermodel = 'User';
  13.  
  14.     function main(){
  15.         $salt = Configure::read('Security.salt');
  16.         App::import('Security');
  17.         $pass_to_hash = $this->in("enter password to hash:");
  18.         $hash = Security::hash($pass_to_hash, null, $salt);
  19.         $this->out('HASH -> ' . $hash, "\n");
  20.         $write_to_db = $this->in("write to database? [y/n  default = n]");
  21.         if($this->filterInput($write_to_db, false))
  22.         {
  23.             $this->write_to_db($hash);
  24.         }
  25.     }
  26.  
  27.     function write_to_db($hash){
  28.         $username = $this->in("enter username for new user [default = 'admin']");
  29.         if($username == ''){
  30.             $username = 'admin';
  31.         }
  32.         $user_model_name = $this->in('User model name? [default = User]');
  33.         if($user_model_name != ''){
  34.             $this->usermodel = $user_model_name;
  35.         }
  36.         App::import('Model', $this->usermodel);
  37.         $usermodel = new $this->usermodel;
  38.         $assume_fieldnames = $this->in('assume username and password for name and hashed pwd fields? [y/n default = y]');
  39.         if(!$this->filterInput($assume_fieldnames, true)){
  40.             $this->username_field = $this->in('user name field:');
  41.             $this->password_field = $this->in('password field:');
  42.         }
  43.         $this->data[$this->usermodel][$this->username_field] = $username;
  44.         $this->data[$this->usermodel][$this->password_field] = $hash;
  45.         if($usermodel->save($this->data))
  46.         {
  47.             $this->out('save completed. Exiting...');
  48.             exit();
  49.         }else
  50.         {
  51.             $retry = $this->in('Unexpected error. Retry? [y/n default = n]');
  52.             if($this->filterInput($retry, false)){
  53.                 $this->main();
  54.             }
  55.         }
  56.     }
  57.  
  58.     function filterInput($input, $default = false){
  59.         if($input != ''){
  60.             if($input == 'y' || $input == 'yes'){
  61.                 return true;
  62.             }else{
  63.                 return false;
  64.             }
  65.         }else
  66.         {
  67.             return $default;
  68.         }
  69.     }
  70.  
  71. }
  72.  

2. running /cake/console/cake hash will generate the hash based on your app's salt, and store a user with the hash in the db optionally.