项目作者: Crudly

项目描述 :
Encryption cast for Eloquent
高级语言: PHP
项目地址: git://github.com/Crudly/Encrypted.git
创建时间: 2020-04-24T21:53:16Z
项目社区:https://github.com/Crudly/Encrypted

开源协议:MIT License

下载


Encrypted

Build Status
Release
License

Note This package is no longer needed for new projects as hashing is now
among the native casts and encryption was there for a while already.
We will probably keep it up to date for a few more years because it usually only takes bumping a version tag.

A custom cast class for Laravel Eloquent that encrypts or hashes your values. Package is small and provides just a few, simple, well tested features.

  1. protected $casts = [
  2. // hashes the value when assigning to $model->password
  3. 'password' => Password::class,
  4. // encrypts on write, decrypts on read
  5. 'classified' => Encrypted::class,
  6. // encrypts on write, decrypts & typecasts on read
  7. 'secret' => Encrypted::class.':integer',
  8. ];

Installation

Use composer.

  1. $ composer require crudly/encrypted

Usage

Mark any column in your model as encrypted.

  1. <?php
  2. namespace App;
  3. use Crudly\Encrypted\Encrypted;
  4. use Illuminate\Database\Eloquent\Model;
  5. class MyModel extends Model
  6. {
  7. protected $casts = [
  8. 'something_secret' => Encrypted::class,
  9. ];
  10. }

You can work with the attribute as you normally would, but it will be encrypted on the database.

  1. $mm = new MyModel;
  2. $mm->someting_secret = 'classified_info';
  3. $mm->save();

Type casting

Encryption serializes the variable and decryption unserializes it, so you get out exactly what you put in. This usually means that no type casting is needed.

But sometimes you want everything casted to some type even if you put something else in. In those cases you can specify types (all of Eloquent’s default casts are supported):

  1. <?php
  2. namespace App;
  3. use Crudly\Encrypted\Encrypted;
  4. use Illuminate\Database\Eloquent\Model;
  5. class MyModel extends Model
  6. {
  7. protected $casts = [
  8. 'encrypted_column' => Encrypted::class,
  9. 'an_integer' => Encrypted::class.':integer',
  10. 'a_string' => Encrypted::class.':string',
  11. 'decimal_with_two_places' => Encrypted::class.':decimal:2',
  12. ];
  13. }

Password hashing

This can also be used to hash a password upon write.

  1. <?php
  2. namespace App;
  3. use Crudly\Encrypted\Password;
  4. use Illuminate\Database\Eloquent\Model;
  5. class MyUser extends Model
  6. {
  7. protected $casts = [
  8. 'password' => Password::class,
  9. ];
  10. }

This hashes the password using bcrypt. You can check a string against the hashed password using Hash facade.

  1. $mu = new MyUser;
  2. $mu->password = 'secret';
  3. $mu->password; // returns a hash
  4. Hash::check('secret', $mu->password); //returns true
  5. Hash::check('hunter2', $mu->password); //returns false

TODO

Maybe add key and cipher customization via options, i.e. Encrypted::class.':string,AckfSECXIvnK5r28GVIWUAxmbBSjTsmF' and Encrypted::class.':string,AckfSECXIvnK5r28GVIWUAxmbBSjTsmF,AES-128-CBC'. And password hashing options.