Tag: decrypt

  • Laravel Eloquent – Encrypt/Decrypt Data on call

    在開發商業網站的時候,對資料加密是必要的手段,尤其是資料庫中關於會員隱私,或是商業機密的部份都要特別處理。 在實務上通常不會用資料庫內建的加密功能,而是用後端語言來實現 encrypt 與 decrypt,資料庫只儲存加密後的訊息。 在 Laravel 裡加解密可以透過繼承 Eloquent,用 magic function 來達成自動化。 class BaseModel extends Eloquent { protected $encrypt = []; public function setAttribute($key, $value) { if (in_array($key, $this->encrypt)) { $value = Crypt::encrypt($value); } return parent::setAttribute($key, $value); } public function getAttribute($key) { if (in_array($key, $this->encrypt)) { return Crypt::decrypt($this->attributes[$key]); } return parent::getAttribute($key); } public function attributesToArray()…