Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gouseferoz's avatar

Hashing and storing large data in MySQL database,

Hi Guys,

I am trying to hash my data before saving it to the database mainly to: 1) Save the size of the data being saved - If you are going to save large lines of data 2) Security.

I looked up online and found http://www.unit-conversion.info/texttools/sha/#data which does the same.

I was able to hash the data but not sure how to unhash the hashed data.

Can you help me achieve my use case?

Regards, Feroz.

0 likes
7 replies
hjortur17's avatar

You could use Laravel Hash, something like this:

use Illuminate\Support\Facades\Hash;

Hash::make(yourData)
Xsecrets's avatar

I believe what you are looking for is encryption. A standard Hash is one way you cannot get the data back out. Using encryption you can go back and forth.

2 likes
gouseferoz's avatar

@xsecrets

Yes, i think i am looking for the encryption.

encrypt and decrypt doesn't save memory.

Can you suggest any other encryption mechanisms.

Regards

Tray2's avatar

There is no way to securely encrypt something and still save space, To achieve that you need to first encrypt then compress it.

mstrauss's avatar

@gouseferoz

You said:

I am trying to hash my data before saving it to the database mainly to: 1) Save the size of the data being saved - If you are going to save large lines of data 2) Security.

Only point two (2) really makes sense. As @tray2 said, encrypting is not a space saving mechanism, it is for security purposes only. If you are trying to be storage-conscious, which is always a good idea, maybe you can only encrypt the sensitive data, as opposed to encrypting the whole DB. To do so, consider using an Encyptable Trait. It's not too difficult to set up on your own but if you prefer a package, here is one that looks pretty straightforward:

https://github.com/gregoryduckworth/Encryptable

fylzero's avatar

@gouseferoz Encryption does not equate with compression. To encrypt in and out of the database, the methods I suggested above are the right way to go about it. Those encryption methods are salted by your APP_KEY in your env file, it isn't one way... fairly secure for what you are trying to do.

Compressing in and out of a database... you would literally lose speed doing the compression and decompression waaaaay more than having it compressed would save you in the first place. There is a reason that isn't common practice.

24 likes

Please or to participate in this conversation.