Skip to content

Commit

Permalink
Update stable branch. Prepare for 1.4.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-scott committed Feb 17, 2016
1 parent 79a20ec commit 34d00a3
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 118 deletions.
49 changes: 46 additions & 3 deletions src/Asymmetric/Crypto.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@ abstract class Crypto implements Contract\AsymmetricKeyCryptoInterface
* @return string
*
* @throws CryptoException\InvalidKey
* @throws CryptoException\InvalidType
*/
public static function encrypt(
$source,
Contract\KeyInterface $ourPrivateKey,
Contract\KeyInterface $theirPublicKey,
$raw = false
) {
if (!\is_string($source)) {
throw new CryptoException\InvalidType(
'Argument 1: Expected the plaintext as a string'
);
}
if (!$ourPrivateKey instanceof EncryptionSecretKey) {
throw new CryptoException\InvalidKey(
'Argument 2: Expected an instance of EncryptionSecretKey'
Expand Down Expand Up @@ -58,13 +64,19 @@ public static function encrypt(
* @return string
*
* @throws CryptoException\InvalidKey
* @throws CryptoException\InvalidType
*/
public static function decrypt(
$source,
Contract\KeyInterface $ourPrivateKey,
Contract\KeyInterface $theirPublicKey,
$raw = false
) {
if (!\is_string($source)) {
throw new CryptoException\InvalidType(
'Argument 1: Expected the ciphertext as a string'
);
}
if (!$ourPrivateKey instanceof EncryptionSecretKey) {
throw new CryptoException\InvalidKey(
'Argument 2: Expected an instance of EncryptionSecretKey'
Expand Down Expand Up @@ -134,14 +146,21 @@ public static function getSharedSecret(
* @param boolean $raw Don't hex encode the output?
*
* @return string
*
*
* @throws CryptoException\InvalidKey
* @throws CryptoException\InvalidType
* @throws CryptoException\CannotPerformOperation
*/
public static function seal(
$source,
Contract\KeyInterface $publicKey,
$raw = false
) {
if (!\is_string($source)) {
throw new CryptoException\InvalidType(
'Argument 1: Expected the plaintext as a string'
);
}
if (!$publicKey instanceof EncryptionPublicKey) {
throw new CryptoException\InvalidKey(
'Argument 2: Expected an instance of EncryptionPublicKey'
Expand All @@ -168,14 +187,20 @@ public static function seal(
* @param boolean $raw Don't hex encode the output?
*
* @return string Signature (detached)
*
*
* @throws CryptoException\InvalidKey
* @throws CryptoException\InvalidType
*/
public static function sign(
$message,
Contract\KeyInterface $privateKey,
$raw = false
) {
if (!\is_string($message)) {
throw new CryptoException\InvalidType(
'Argument 1: Expected the message as a string'
);
}
if (!$privateKey instanceof SignatureSecretKey) {
throw new CryptoException\InvalidKey(
'Argument 2: Expected an instance of SignatureSecretKey'
Expand All @@ -201,13 +226,19 @@ public static function sign(
* @return string
*
* @throws CryptoException\InvalidKey
* @throws CryptoException\InvalidType
* @throws CryptoException\CannotPerformOperation
*/
public static function unseal(
$source,
Contract\KeyInterface $privateKey,
$raw = false
) {
if (!\is_string($source)) {
throw new CryptoException\InvalidType(
'Argument 1: Expected the ciphertext as a string'
);
}
if (!$privateKey instanceof EncryptionSecretKey) {
throw new CryptoException\InvalidKey(
'Argument 2: Expected an instance of EncryptionSecretKey'
Expand Down Expand Up @@ -258,8 +289,10 @@ public static function unseal(
* @param boolean $raw Don't hex decode the input?
*
* @return boolean
*
*
* @throws CryptoException\InvalidKey
* @throws CryptoException\InvalidType
* @throws CryptoException\InvalidSignature
* @throws CryptoException\CannotPerformOperation
*/
public static function verify(
Expand All @@ -268,11 +301,21 @@ public static function verify(
$signature,
$raw = false
) {
if (!\is_string($message)) {
throw new CryptoException\InvalidType(
'Argument 1: Expected the message as a string'
);
}
if (!$publicKey instanceof SignaturePublicKey) {
throw new CryptoException\InvalidKey(
'Argument 2: Expected an instance of SignaturePublicKey'
);
}
if (!\is_string($signature)) {
throw new CryptoException\InvalidType(
'Argument 3: Expected the signature as a string'
);
}
if (!$raw) {
$signature = \Sodium\hex2bin($signature);
}
Expand Down
13 changes: 13 additions & 0 deletions src/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use \ParagonIE\Halite\Contract\KeyInterface;
use \ParagonIE\Halite\Symmetric\EncryptionKey;
use \ParagonIE\Halite\Symmetric\Crypto;
use \ParagonIE\Halite\Alerts\InvalidType;
use \ParagonIE\Halite\Alerts\InvalidMessage;

final class Cookie
Expand Down Expand Up @@ -36,9 +37,15 @@ public function __debugInfo()
*
* @param string $name
* @return mixed (typically an array)
* @throws InvalidType
*/
public function fetch($name)
{
if (!\is_string($name)) {
throw new InvalidType(
'Argument 1: Expected a string'
);
}
if (!isset($_COOKIE[$name])) {
return null;
}
Expand All @@ -64,6 +71,7 @@ public function fetch($name)
* @param bool $secure (defaults to TRUE)
* @param bool $httponly (defaults to TRUE)
* @return bool
* @throws InvalidType
*/
public function store(
$name,
Expand All @@ -74,6 +82,11 @@ public function store(
$secure = true,
$httponly = true
) {
if (!\is_string($name)) {
throw new InvalidType(
'Argument 1: Expected a string'
);
}
return \setcookie(
$name,
Crypto::encrypt(
Expand Down
Loading

0 comments on commit 34d00a3

Please sign in to comment.