0

I have custom class in laravel 5.4 app which should return hashed secret. The class is

class Hash {

   // Unencrypted secret

   private $secret;

   public function generateSecret(){

       $secret = generateSecretKey();

       $secret_hash = password_hash($secret, PASSWORD_BCRYPT);

       Session::put('secret-hash', $secret_hash);
       $this->secret = $secret;        
   }
}

Then in my controller I have

use Hash;
class UsersController extends BaseController
{
    public function Auth()
    {
        $myhash = new Hash();
        $msg = '';

        $myhash->generateSecret();
        $enc = $myhash->encryptSecret($key->key);
        return View::make('users.auth', ['enc'=> $enc]);
    }
     ...
}

var_dump($myhash->generateSecret()); from the controller return NULL

var_dump($secret) in public function generateSecret() return string(15) "866094233422231" string(15) "583375207466239" which is correct.

var_dump($myhash); in UsersController also return correct data

String(15) "008844975703088" object(Hash)#329 (1) 
{ 
     ["secret":"Hash":private]=> string(15) "008844975703088" 
}

Appears that the problem is in controller and generating the hash $myhash->generateSecret();. The function must generate secret(a string) which then is hashed $enc = $myhash->encryptSecret($key->key); and displayed on page.

Any ideas why the function isn't working? I don't know what else I can try.

4
  • I think var_dump shows null because your function has no return. Commented Jul 31, 2017 at 7:38
  • Yeah, I think same but then when I var dump it directly in the function it is showing the result and it is not null which is odd for me. Commented Jul 31, 2017 at 7:39
  • return the value of your function generateSecret. Commented Jul 31, 2017 at 7:40
  • @aldrin27, this is the example return of generateSecret - string(15) "866094233422231" string(15) "583375207466239" Commented Jul 31, 2017 at 7:57

2 Answers 2

1

generateSecret() does not return anything, so $myhash->generateSecret() is NULL. generateSecret() does set $secret as a property ($this->secret = $secret), but since $secret is declared as private, you can't access it outside the class, so $myhash->secret in your controller won't work.

So you have 2 options - either return the secret so the calling code can see it, or add a getter to make $secret accessible outside the class. Here's how you could return it:

public function generateSecret(){
    $secret = generateSecretKey();
    $secret_hash = password_hash($secret, PASSWORD_BCRYPT);
    Session::put('secret-hash', $secret_hash);
    return $secret;
}

Then in your controller:

$secret = $myhash->generateSecret();
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the answer and sorry for the delay. I will try it this way and will let you know about the result.
Just a question: What can be the reason to work on my local server but not online?
Sorry but is still bool(false). I mean I don't get NULL in the controller anymore but now I get bool(false)
I am not sure what your code looks like now, but does $secret still come from generateSecretKey()? If yes, then generateSecretKey() is returning false ... try var_dump() again to confirm.
Thanks for the help. The second bool(false) was my mistake. Everything works fine!
0

This function:

public function generateSecret()
{

   $secret = generateSecretKey();

   $secret_hash = password_hash($secret, PASSWORD_BCRYPT);

   Session::put('secret-hash', $secret_hash);
   $this->secret = $secret;        
}

Doesn't return any data, so var_dump($myhash->generateSecret()); will always be null.

You should expose state using getter if you need to access it outside of class.

5 Comments

I don't understand because this function is working perfectly like this in laravel 4.2.
Sorry but doesn't I have getter in my user controller where I have $myhash = new Hash();
No, that's just instantiating your class. Either add a getter which returns $this->secret, or simply return $secret; from generateSecret().
@Don'tPanic, return $secret; doesn't change anything. Maybe I'm wrong but I just replaced $this->secret = $secret; with return $secret;, right?
I added an answer with more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.