Home > Software engineering >  hash function on laravel cant decrypt string given from form
hash function on laravel cant decrypt string given from form

Time:12-26

Im currently creating an application that requiring me to use the Bcrypt Hash function on laravel, but for some reason it always says that the string given to decrypt is 'false' when i do dd, but if i use plain-text, it says 'true' with dd.

for more context i will give the code related

Encryption:

  $encryptionKey = "PNJiX9RhLmnzJw2vRT1kolbYd3iwj96z";
        // $new_hash=Hash::make($encryptionKey);
        $new_hash = Hash::make($encryptionKey, [
            'rounds' => 12,
        ]);

Decryption with plain text that gives 'true' result(I check with online decrypting tools too):

dd(Hash::check( 'PNJiX9RhLmnzJw2vRT1kolbYd3iwj96z', '$2y$12$1tdSV0MQFuXSTisGoSPnw.efdnl9.Hk8x907U5TcgJ78mobozhUoK'));

Decryption with plain text that gives 'false' result:

$this->validate($request, [
            'key' => 'required',
        ]);
        $hashedPassword = 'PNJiX9RhLmnzJw2vRT1kolbYd3iwj96z';
        $encryptionOldKey = $request->key;
dd(Hash::check( $encryptionOldKey, $hashedPassword));

Blade file related to form decryption:

<div >

<form action="{{ url('/home/decrypt', $file->id ) }} " method="POST" enctype="multipart/form-data">
                                                    {{ csrf_field() }}

<div >
                                                        <b>Key</b>
    <input type="text"  name="key" >
                                                    </div>

                                            </div>
         <div >
<button type="button"  data-dismiss="modal">Cancel <i ></i></button>
<button type="submit" >Decrypt<i ></i></button>
                                            </div>

</form>

CodePudding user response:

solution

Always (Hash::make) accept plain-text(original password) not encrypted/hased one.

For reference:

use Illuminate\Support\Facades\Hash;

$newPassword='12345678'; // original/plain-text
$hashedPassword=Hash::make($newPassword);

if (Hash::check($newPassword, $hashedPassword)) {
    // The passwords match...
}

CodePudding user response:

I feel like pepega right now, I placed my variable wrong , thanks for everyone that helping me :) .

  • Related