本篇文章給大家分享一個Laravel擴(kuò)展:Ciphersweet包,介紹一下怎么利用Laravel Ciphersweet 來加密/解密 Laravel 中的 Eloquent 模型字段,希望對大家有所幫助!
Laravel Ciphersweet 是由 Spatie 提供的一個包,用于在Laravel應(yīng)用程序中集成可搜索的字段級加密。該包的 readme 解釋了密碼甜蜜可以幫助解決的問題,如下所示:
在你的項(xiàng)目中,你可能會將敏感的個人數(shù)據(jù)存儲在數(shù)據(jù)庫中。如果未經(jīng)授權(quán)的人訪問你的數(shù)據(jù)庫,則所有敏感數(shù)據(jù)都可以讀取,這顯然是不好的。
要解決此問題,你可以對個人數(shù)據(jù)進(jìn)行加密。這樣,未經(jīng)授權(quán)的人無法讀取它,但當(dāng)你需要顯示或使用數(shù)據(jù)時,你的應(yīng)用程序仍然可以解密它。
這個包是 Ciphersweet 的包裝器,可以輕松地將其功能集成到 Laravel 模型中。以下是自述文 setup instructions 中的模型示例,該示例說明了使用 Ciphersweet 模型的外
use SpatieLaravelCipherSweetContractsCipherSweetEncrypted; use SpatieLaravelCipherSweetConcernsUsesCipherSweet; use ParagonIECipherSweetEncryptedRow; use IlluminateDatabaseEloquentModel; class User extends Model implements CipherSweetEncrypted { use UsesCipherSweet; public static function configureCipherSweet(EncryptedRow $encryptedRow): void { $encryptedRow ->addField('email') ->addBlindIndex('email', new BlindIndex('email_index')); } }
這允許你加密用戶的電子郵件,以防止未經(jīng)授權(quán)的人讀取數(shù)據(jù),但允許你解密數(shù)據(jù)以顯示或使用它。
一旦你配置了此包并設(shè)置了模型,你就可以使用盲索引在數(shù)據(jù)庫中搜索加密數(shù)據(jù):
$user = User::whereBlind('email', 'email_index', 'rias@spatie.be');
此包還有助于生成加密密鑰和加密模型屬性,以加快與密碼的集成。
我想指出的是,你不應(yīng)該在不了解你試圖解決的用例的來龍去脈的情況下盲目地使用這個包。你可以在 此頁面 上了解有關(guān) CipherSweet 的