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