swapnil mane
swapnil mane

Reputation: 257

How to add encryption key in SHA256 algorithm in php?

I want to encrypt some data uisng SHA256 hashing algorithm

<?php
 $encryption_key = "aldskfje49345rgaefa";

 echo hash('sha256', "hello world");

?>

Upvotes: 0

Views: 2302

Answers (2)

Th&#233;o Champion
Th&#233;o Champion

Reputation: 307

When you do this

echo hash('sha256', "hello world");

You are not encrypting your data but you are hashing them.

The difference between both ?

Hashing is one way. You generate an unique and irreversible hash.
Encrypting is two way. You can encrypt your data with a key and decrypt with the same key.

So in your situation, you should avoid using SHA256 algorithm. Instead, I recommand you to use Crypt facade provided by Laravel. The key used for encrypting your data is your APP_KEY environnement variable (which is generated when you install your project). If you have not this, you can simply generate it by execute this command :

php artisan key:generate

So, you will need to share the key with your friend, and use the same algorithm. I let you read the documentation about Crypt Facade.

Upvotes: 0

Sublan Mustafa
Sublan Mustafa

Reputation: 379

Try Using hash_hmac() Like this hash_hmac('sha256',);

Read the Manual Here

Just a suggestion...

Upvotes: 1

Related Questions