Ali
Ali

Reputation: 49

How can i hash username in Laravel

I use Laravel Authentication and want to hash all user data . How can i hash username in Laravel and decode in login page like password ?

I edited Register controller like this

 protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'username' => bcrypt($data['username']),
            'password' => bcrypt($data['password']),
        ]);
    }

Upvotes: 0

Views: 945

Answers (2)

M.Idrish
M.Idrish

Reputation: 437

yes hash is 1 way process but you can use encryption for same type of process.

use Illuminate\Support\Facades\Crypt;

$encrypted = Crypt::encryptString('Hello world.');

$decrypted = Crypt::decryptString($encrypted);

non-PHP clients receiving encrypted values will need to unserialize the data. If you would like to encrypt and decrypt values without serialization, you may use the encryptString and decryptString methods of the Crypt facade.

so always use Crypt Facade because all thing work in background automatically.

Upvotes: 1

Andy Song
Andy Song

Reputation: 4684

Hash is different from encoding/decoding. Hash is a one-way process, whereas decoding/encoding is a two-way process.

For Hash, once something is hashed you can not get the original any more, however, if you use the same value and hash again you will get the same hash result.

For decode/encode, you can get the original value back.

In Laravel by default, the password is hashed not encrypted or encoded.

So, hash a username is a bad idea. You will never know what's the username once it's been hashed.

Upvotes: 2

Related Questions