<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Tommy Hendrawan</title>
    <description>The latest articles on Forem by Tommy Hendrawan (@tommyhendrawan).</description>
    <link>https://forem.com/tommyhendrawan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F523684%2Fb9bf827d-a2a4-4427-9c97-d0c57462d76d.png</url>
      <title>Forem: Tommy Hendrawan</title>
      <link>https://forem.com/tommyhendrawan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/tommyhendrawan"/>
    <language>en</language>
    <item>
      <title>Automatic Laravel Data Encryption with Eloquent</title>
      <dc:creator>Tommy Hendrawan</dc:creator>
      <pubDate>Sat, 19 Dec 2020 03:20:13 +0000</pubDate>
      <link>https://forem.com/tommyhendrawan/automatic-laravel-data-encryption-with-eloquent-4137</link>
      <guid>https://forem.com/tommyhendrawan/automatic-laravel-data-encryption-with-eloquent-4137</guid>
      <description>&lt;p&gt;Few months ago i got laravel project requirement that need me to encrypt all user information that stored at database and the encrypted information also &lt;code&gt;must be searchable&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I try to google search and found some laravel package that enable eloquent to &lt;code&gt;automatically encrypt data&lt;/code&gt; when do save and &lt;code&gt;automatically decrypt data&lt;/code&gt; when load. Sadly most of the package not support search at the encrypted information.&lt;/p&gt;

&lt;p&gt;Start from package that i found, i try to modify and create new package that suit my project requirement like below&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1: Composer
&lt;/h3&gt;

&lt;p&gt;Via Composer command line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;composer require elgibor-solution/laravel-database-encryption
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Add ServiceProvider to your app/config.php file (Laravel 5.4 or below)
&lt;/h3&gt;

&lt;p&gt;Add the service provider to the providers array in the config/app.php config file as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;    &lt;span class="s1"&gt;'providers'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="mf"&gt;...&lt;/span&gt;
        &lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nc"&gt;ESolution\DBEncryption\Providers\DBEncryptionServiceProvider&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;Use the &lt;code&gt;EncryptedAttribute&lt;/code&gt; trait in any Eloquent model that you wish to apply encryption&lt;br&gt;
to and define a &lt;code&gt;protected $encrypted&lt;/code&gt; array containing a list of the attributes to encrypt.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;
    &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;ESolution\DBEncryption\Traits\EncryptedAttribute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Eloquent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;EncryptedAttribute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="cd"&gt;/**
         * The attributes that should be encrypted on save.
         *
         * @var array
         */&lt;/span&gt;
        &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nv"&gt;$encryptable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="s1"&gt;'first_name'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'last_name'&lt;/span&gt;
        &lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By including the &lt;code&gt;EncryptedAttribute&lt;/code&gt; trait, the &lt;code&gt;setAttribute()&lt;/code&gt;, &lt;code&gt;getAttribute()&lt;/code&gt; and &lt;code&gt;getAttributeFromArray()&lt;/code&gt;&lt;br&gt;
methods provided by Eloquent are overridden to include an additional step.&lt;/p&gt;
&lt;h3&gt;
  
  
  Searching Encrypted Fields Example:
&lt;/h3&gt;

&lt;p&gt;Searching encrypted field can be done by calling the &lt;code&gt;whereEncrypted&lt;/code&gt; and &lt;code&gt;orWhereEncrypted&lt;/code&gt; functions&lt;br&gt;
similar to laravel eloquent &lt;code&gt;where&lt;/code&gt; and &lt;code&gt;orWhere&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;    &lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App\Http\Controllers&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;App\User&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UsersController&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Controller&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Request&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nv"&gt;$user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;whereEncrypted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'first_name'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'john'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;orWhereEncrypted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'last_name'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'!='&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Doe'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;firstOrFail&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Encrypt your current data
&lt;/h3&gt;

&lt;p&gt;If you have current data in your database you can encrypt it with the command below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;    &lt;span class="n"&gt;php&lt;/span&gt; &lt;span class="n"&gt;artisan&lt;/span&gt; &lt;span class="n"&gt;encryptable&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;encryptModel&lt;/span&gt; &lt;span class="s1"&gt;'App\User'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Additionally you can decrypt it using the command below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;    &lt;span class="n"&gt;php&lt;/span&gt; &lt;span class="n"&gt;artisan&lt;/span&gt; &lt;span class="n"&gt;encryptable&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;decryptModel&lt;/span&gt; &lt;span class="s1"&gt;'App\User'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: You must implement first the &lt;code&gt;Encryptable&lt;/code&gt; trait and set &lt;code&gt;$encryptable&lt;/code&gt; attributes&lt;/p&gt;

&lt;h3&gt;
  
  
  Exists and Unique Validation Rules
&lt;/h3&gt;

&lt;p&gt;If you are using exists and unique rules with encrypted values replace it with exists_encrypted and unique_encrypted&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;      &lt;span class="nv"&gt;$validator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;validator&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;'email'&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s1"&gt;'foo@bar.com'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'email'&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s1"&gt;'exists_encrypted:users,email'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
      &lt;span class="nv"&gt;$validator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;validator&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;'email'&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s1"&gt;'foo@bar.com'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'email'&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s1"&gt;'unique_encrypted:users,email'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hope this package help other people that got same project requirement&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>encryption</category>
      <category>eloquent</category>
    </item>
  </channel>
</rss>
