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