This repository has been archived by the owner on Jan 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
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 #12 from salesupply/feature/add-hashing-support
Feature/add hashing support
- Loading branch information
Showing
13 changed files
with
724 additions
and
150 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
This file was deleted.
Oops, something went wrong.
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,99 @@ | ||
<?php | ||
|
||
namespace ZfDoctrineEncryptModule\Adapter; | ||
|
||
use ParagonIE\ConstantTime\Binary; | ||
use ParagonIE\Halite\Alerts\InvalidKey; | ||
use ParagonIE\Halite\HiddenString; | ||
use ParagonIE\Halite\Password; | ||
use ParagonIE\Halite\Symmetric\EncryptionKey; | ||
use ZfDoctrineEncryptModule\Interfaces\HashInterface; | ||
|
||
class HaliteHashingAdapter implements HashInterface | ||
{ | ||
/** | ||
* @var EncryptionKey | ||
*/ | ||
private $key; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $pepper; | ||
|
||
/** | ||
* HaliteAdapter constructor. | ||
* @param $key | ||
* @throws InvalidKey | ||
* @throws \TypeError | ||
*/ | ||
public function __construct($key, $pepper) | ||
{ | ||
if (Binary::safeStrlen($key) !== \Sodium\CRYPTO_STREAM_KEYBYTES) { | ||
|
||
throw new InvalidKey( | ||
'Encryption key used for ' . __CLASS__ . '::' . __FUNCTION__ . ' must be exactly ' . \Sodium\CRYPTO_STREAM_KEYBYTES . ' characters long' | ||
); | ||
} | ||
|
||
if (Binary::safeStrlen($pepper) !== \Sodium\CRYPTO_STREAM_KEYBYTES) { | ||
|
||
throw new InvalidKey( | ||
'Encryption pepper used for ' . __CLASS__ . '::' . __FUNCTION__ . ' must be exactly ' . \Sodium\CRYPTO_STREAM_KEYBYTES . ' characters long' | ||
); | ||
} | ||
|
||
$this->setKey((new EncryptionKey((new HiddenString($key))))); | ||
$this->setPepper($pepper); | ||
} | ||
|
||
/** | ||
* @param string $data | ||
* @return string | ||
* @throws \ParagonIE\Halite\Alerts\CannotPerformOperation | ||
* @throws \ParagonIE\Halite\Alerts\InvalidDigestLength | ||
* @throws \ParagonIE\Halite\Alerts\InvalidMessage | ||
* @throws \ParagonIE\Halite\Alerts\InvalidType | ||
*/ | ||
public function hash(string $data): string | ||
{ | ||
return Password::hash(new HiddenString($data . $this->getPepper()), $this->getKey()); | ||
} | ||
|
||
/** | ||
* @return EncryptionKey | ||
*/ | ||
public function getKey(): EncryptionKey | ||
{ | ||
return $this->key; | ||
} | ||
|
||
/** | ||
* @param EncryptionKey $key | ||
* @return HaliteHashingAdapter | ||
*/ | ||
public function setKey(EncryptionKey $key): HaliteHashingAdapter | ||
{ | ||
$this->key = $key; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getPepper(): string | ||
{ | ||
return $this->pepper; | ||
} | ||
|
||
/** | ||
* @param string $pepper | ||
* @return HaliteHashingAdapter | ||
*/ | ||
public function setPepper(string $pepper): HaliteHashingAdapter | ||
{ | ||
$this->pepper = $pepper; | ||
return $this; | ||
} | ||
|
||
} |
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,37 @@ | ||
<?php | ||
|
||
namespace ZfDoctrineEncryptModule\Annotation; | ||
|
||
use Doctrine\Common\Annotations\Annotation\Target; | ||
|
||
/** | ||
* The below register the class as to be used as Doctrine's Annotation and only on properties. | ||
* | ||
* @Annotation | ||
* @Target("PROPERTY") | ||
*/ | ||
class Hashed | ||
{ | ||
/** | ||
* @var string linked property which implements \ZfDoctrineEncryptModule\Interfaces\SaltInterface | ||
*/ | ||
public $salt; | ||
|
||
/** | ||
* @return null|string | ||
*/ | ||
public function getSalt(): ?string | ||
{ | ||
return $this->salt; | ||
} | ||
|
||
/** | ||
* @param null|string $salt | ||
* @return Hashed | ||
*/ | ||
public function setSalt(?string $salt): Hashed | ||
{ | ||
$this->salt = $salt; | ||
return $this; | ||
} | ||
} |
Oops, something went wrong.