Saturday, January 8, 2022

[FIXED] hash function on laravel cant decrypt string given from form

Issue

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 class="modal-body">

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

<div class="form-group">
                                                        <b>Key</b>
    <input type="text" class="form-control" name="key" >
                                                    </div>

                                            </div>
         <div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel <i class="ni ni-fat-remove"></i></button>
<button type="submit" class="btn btn-primary">Decrypt<i class="ni ni-check-bold"></i></button>
                                            </div>

</form>

Solution

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...
}


Answered By - Engr Talha

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.