Php 使用where子句获取加密数据
除了主键(id),我的整个数据库都是加密的。我需要使用$email变量获取电子邮件Php 使用where子句获取加密数据,php,laravel,eloquent,php-7,Php,Laravel,Eloquent,Php 7,除了主键(id),我的整个数据库都是加密的。我需要使用$email变量获取电子邮件 $encrypted=registermodel::select('email')->where('email','=',$email)->get(); 你不可能很容易-它是加密的!您必须获取每个记录并对其进行解密,然后比较明文 这是解决问题的正确方法 下面是我使用基于CRC的索引列的尝试 <?php namespace App\ModelTraits; use Illuminate\Su
$encrypted=registermodel::select('email')->where('email','=',$email)->get();
你不可能很容易-它是加密的!您必须获取每个记录并对其进行解密,然后比较明文 这是解决问题的正确方法 下面是我使用基于CRC的索引列的尝试
<?php
namespace App\ModelTraits;
use Illuminate\Support\Facades\Crypt;
/**
*
*/
trait EmailSigTrait
{
public function setEmailAttribute($value)
{
$this->attributes['email'] = Crypt::encryptString($value);
$this->attributes['emailsig'] = Self::crcemail($value);
}
public function getEmailAttribute()
{
if(!isset($this->attributes['email'])){
return;
}
$value = $this->attributes['email'];
if (empty($value)) {
return;
}
return strtolower(Crypt::decryptString($value));
}
static function crcemail($email)
{
$email = strtolower($email);
// anonymise the email
$name = str_before($email,'@');
$anon = substr($name, 0, 1) .
substr($name, strlen($name)/2,1) .
substr($name, -1) .
'@' . str_after($email, '@');
return sprintf(crc32(strToLower($anon)));
}
protected function findByEmailSig($email, $model)
{
$email = strtolower($email);
$candidates = $model::where('emailsig', $model::crcemail($email))->get();
foreach ($candidates as $candidate) {
if (strtolower($candidate->email) == $email) {
return $candidate;
}
}
return false;
}
}
那么你的问题是什么?$email是加密的吗?需要吗?具体点。