-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from paragonie/fieldsets
Implement EncryptedRow.
- Loading branch information
Showing
7 changed files
with
1,164 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
<?php | ||
namespace ParagonIE\CipherSweet; | ||
|
||
use ParagonIE\CipherSweet\Contract\TransformationInterface; | ||
use ParagonIE\CipherSweet\Transformation\Compound; | ||
|
||
/** | ||
* Class CompoundIndex | ||
* @package ParagonIE\CipherSweet | ||
*/ | ||
class CompoundIndex | ||
{ | ||
/** | ||
* @var array<int, string> $columns | ||
*/ | ||
protected $columns; | ||
|
||
/** | ||
* @var bool $fastHash | ||
*/ | ||
protected $fastHash; | ||
|
||
/** | ||
* @var array $hashConfig | ||
*/ | ||
protected $hashConfig; | ||
|
||
/** | ||
* @var string $name | ||
*/ | ||
protected $name; | ||
|
||
/** | ||
* @var int $outputLength | ||
*/ | ||
protected $filterBits = 256; | ||
|
||
/** | ||
* @var array<string, array<int, TransformationInterface>> | ||
*/ | ||
protected $columnTransforms = []; | ||
|
||
/** | ||
* @var Compound | ||
*/ | ||
private static $compounder; | ||
|
||
/** | ||
* CompoundIndex constructor. | ||
* | ||
* @param string $name | ||
* @param array<int, string> $columns | ||
* @param int $filterBits | ||
* @param bool $fastHash | ||
* @param array $hashConfig | ||
*/ | ||
public function __construct( | ||
$name, | ||
array $columns = [], | ||
$filterBits = 256, | ||
$fastHash = false, | ||
array $hashConfig = [] | ||
) { | ||
$this->name = $name; | ||
$this->columns = $columns; | ||
$this->filterBits = $filterBits; | ||
$this->fastHash = $fastHash; | ||
$this->hashConfig = $hashConfig; | ||
} | ||
|
||
/** | ||
* @return Compound | ||
*/ | ||
public static function getCompounder() | ||
{ | ||
if (!self::$compounder) { | ||
self::$compounder = new Compound(); | ||
} | ||
return self::$compounder; | ||
} | ||
|
||
/** | ||
* @param string $column | ||
* @param TransformationInterface $tf | ||
* @return self | ||
*/ | ||
public function addTransform($column, TransformationInterface $tf) | ||
{ | ||
$this->columnTransforms[$column][] = $tf; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return array<int, string> | ||
*/ | ||
public function getColumns() | ||
{ | ||
return $this->columns; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function getFastHash() | ||
{ | ||
return $this->fastHash; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getFilterBitLength() | ||
{ | ||
return $this->filterBits; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getHashConfig() | ||
{ | ||
return $this->hashConfig; | ||
} | ||
|
||
/** | ||
* @param string $column | ||
* @return array<int, TransformationInterface> | ||
*/ | ||
public function getTransforms($column) | ||
{ | ||
if (!\array_key_exists($column, $this->columns)) { | ||
return []; | ||
} | ||
return $this->columnTransforms[$column]; | ||
} | ||
|
||
/** | ||
* Get a packed plaintext for use in creating a compound blind index | ||
* This is a one-way transformation meant to be distinct from other inputs | ||
* Not all elements of the row will be used. | ||
* | ||
* @param array $row | ||
* | ||
* @return string | ||
* @throws \Exception | ||
*/ | ||
public function getPacked(array $row) | ||
{ | ||
/** @var array<int, string> $pieces */ | ||
$pieces = []; | ||
/** @var string $col */ | ||
foreach ($this->columns as $col) { | ||
if (!\array_key_exists($col, $row)) { | ||
continue; | ||
} | ||
/** @var string $piece */ | ||
$piece = $row[$col]; | ||
if (!empty($this->columnTransforms[$col])) { | ||
foreach ($this->columnTransforms[$col] as $tf) { | ||
if ($tf instanceof TransformationInterface) { | ||
/** @var string $piece */ | ||
$piece = $tf($piece); | ||
} | ||
} | ||
} | ||
$pieces[$col] = $piece; | ||
} | ||
$compounder = self::getCompounder(); | ||
return (string) $compounder($pieces); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.