1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\Encrypt; |
4
|
|
|
|
5
|
|
|
use ParagonIE\CipherSweet\BlindIndex; |
6
|
|
|
use ParagonIE\CipherSweet\EncryptedField; |
7
|
|
|
use ParagonIE\CipherSweet\Transformation\LastFourDigits; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Value will be set on parent record through built in getField |
11
|
|
|
* mechanisms for composite fields |
12
|
|
|
* |
13
|
|
|
* This can be useful to store phone numbers, national numbers... |
14
|
|
|
* We keep two indexes: |
15
|
|
|
* - One with the full record |
16
|
|
|
* - One with the last 4 numbers (so if your phone number is +00 471 123 456, it will be searchable with 3456) |
17
|
|
|
*/ |
18
|
|
|
class EncryptedNumberField extends EncryptedDBField |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @param array |
22
|
|
|
*/ |
23
|
|
|
private static $composite_db = array( |
|
|
|
|
24
|
|
|
"Value" => "Varchar(191)", |
25
|
|
|
"BlindIndex" => 'Varchar(32)', |
26
|
|
|
"LastFourBlindIndex" => 'Varchar(16)', |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param int $default |
31
|
|
|
* @return int |
32
|
|
|
*/ |
33
|
|
|
public function getLastFourIndexSize($default = null) |
34
|
|
|
{ |
35
|
|
|
if (array_key_exists('last_four_index_size', $this->options)) { |
36
|
|
|
return $this->options['last_four_index_size']; |
37
|
|
|
} |
38
|
|
|
return $default; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function getLastFourBlindIndexField() |
45
|
|
|
{ |
46
|
|
|
return $this->getField('LastFourBlindIndex'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
|
public function setLastFourBlindIndexField($value, $markChanged = true) |
53
|
|
|
{ |
54
|
|
|
return $this->setField('LastFourBlindIndex', $value, $markChanged); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param CipherSweet $engine |
|
|
|
|
59
|
|
|
* @return EncryptedField |
60
|
|
|
*/ |
61
|
|
|
public function getEncryptedField($engine = null) |
62
|
|
|
{ |
63
|
|
|
if ($engine === null) { |
64
|
|
|
$engine = EncryptHelper::getCipherSweet(); |
65
|
|
|
} |
66
|
|
|
$lastFourIndexSize = $this->getLastFourIndexSize(self::SMALL_INDEX_SIZE); |
67
|
|
|
$indexSize = $this->getIndexSize(self::LARGE_INDEX_SIZE); |
68
|
|
|
// fieldName needs to match exact db name for row rotator to work properly |
69
|
|
|
$encryptedField = (new EncryptedField($engine, $this->tableName, $this->name . "Value")) |
70
|
|
|
->addBlindIndex(new BlindIndex($this->name . "LastFourBlindIndex", [new LastFourDigits()], $lastFourIndexSize)) |
71
|
|
|
->addBlindIndex(new BlindIndex($this->name . "BlindIndex", [], $indexSize)); |
72
|
|
|
return $encryptedField; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function addToQuery(&$query) |
76
|
|
|
{ |
77
|
|
|
parent::addToQuery($query); |
78
|
|
|
$query->selectField(sprintf('"%sValue"', $this->name)); |
79
|
|
|
$query->selectField(sprintf('"%sBlindIndex"', $this->name)); |
80
|
|
|
$query->selectField(sprintf('"%sLastFourBlindIndex"', $this->name)); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|