简体   繁体   中英

Laravel, storing encrypted secrets in a MySQL indexed column

I kind of know the answer to this already but I wanted more of a general opinion of which way to go next.

I want to have users' emails secured by encryption in my application's database, but emails being emails I also want them as an index as well on the user table.

I was doing my seeding of the database and kept getting the error:

[Illuminate\Encryption\DecryptException]  
Invalid data.

I've found that some of the secrets being generated by Crypt::encrypt() seem to exceed the 255 byte limit of MySQL on unique indexes. I guess ultimately I'll have to extend the Encrypter class of the Illuminate package and override the core encryption methods to use something which won't expand the string.

Is there something I'm missing that would do all of this better? it's probably not very query effective to store them as their encrypted format anyways but I couldn't think of anything particularly better?

Your approach is pointless because Crypt::encrypt() automatically "salts" the values. The same email will generate a different encrypted value each time. Check it by your self:

for($i = 1; $i <= 10; $i++)
{
    echo Crypt::encrypt('secret');
}

Even if you manage to overcome the index column size limit you will still be unable to check for duplicates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM