PHP code example of lekoala / silverstripe-encrypt
1. Go to this page and download the library: Download lekoala/silverstripe-encrypt library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lekoala / silverstripe-encrypt example snippets
class MySecureObject extends DataObject
{
use HasEncryptedFields;
private static $db = [
"Name" => 'Varchar',
"MyText" => EncryptedDBText::class,
"MyHTMLText" => EncryptedDBHTMLText::class,
"MyVarchar" => EncryptedDBVarchar::class,
"MyNumber" => EncryptedNumberField::class,
"MyIndexedVarchar" => EncryptedDBField::class,
];
private static $indexes = [
'MyIndexedVarcharBlindIndex' => true,
'MyNumberBlindIndex' => true,
'MyNumberLastFourBlindIndex' => true,
];
public function getField($field)
{
return $this->getEncryptedField($field);
}
public function setField($fieldName, $val)
{
return $this->setEncryptedField($fieldName, $val);
}
}
Array
(
[min] => 2
[max] => 32
[indexes] => 2
[coincidence_count] => 8589934592
[coincidence_ratio] => 9.3132257461548E-8
[estimated_population] => 9223372036854775807
)
private static $db = [
"MyNumber" => EncryptedNumberField::class . '(["output_size" => 4, "domain_size" => 10, "index_size" => 32])',
];
// create definition somewhere...
$map = (new JsonFieldMap())
->addTextField('name')
->addBooleanField('active')
->addIntegerField('age');
$definition = EncryptHelper::convertJsonMapToDefinition($map);
// in your models...
private static $db = [
"MyEncryptedJson" => EncryptedDBJson::class . "(['map' => '7551830f{\"fields\":{\"$6e616d65\":\"string\",\"$616374697665\":\"bool\",\"$616765\":\"int\"}}'])",
];
$singl = singleton(MyModel::class);
$obj = $singl->dbObject('MyEncryptedField');
$searchValue = $obj->getSearchValue($value);
$query = MyModel::get()->where(array('MyEncryptedFieldBlindIndex = ?' => $searchValue));
$singl = singleton(MyModel::class);
$obj = $singl->dbObject('MyEncryptedField');
$record = $obj->fetchRecord($value);
$record = MyModel::getByBlindIndex("MyEncryptedField", $value);
$list = MyModel::getAllByBlindIndex("MyEncryptedField", $value);
$someText = 'some text';
$encrypt = EncryptHelper::encrypt($someText);
$decryptedValue = EncryptHelper::decrypt($encrypt);
$myFile->encryptFileIfNeeded();
$file = $this->File();
$file->encryptFileIfNeeded();
// fine for one record, not fine in a loop! Use EncryptHelper::checkIfFileIsEncrypted with ID
$oldKey = EncryptHelper::getOldKey();
$old = EncryptHelper::getEngineForEncryption("nacl", $oldKey);
$result = $model->needsToRotateEncryption($old);
if($result) {
$result = $model->rotateEncryption($old);
}
protected function writeBaseRecord($baseTable, $now)
{
parent::writeBaseRecord($baseTable, $now);
// After base record is written, we have an ID and therefore AAD has changed
$this->resetFieldValues();
}