Home > Back-end >  How to add encryption key in SHA256 algorithm in php?
How to add encryption key in SHA256 algorithm in php?

Time:04-20

I want to encrypt some data uisng SHA256 hashing algorithm

<?php
 $encryption_key = "aldskfje49345rgaefa";

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

?>

CodePudding user response:

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

Read the Manual Here

Just a suggestion...

CodePudding user response:

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.

  • Related