Skip to content

Commit

Permalink
v1.0.1 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Castelnuovo committed Apr 8, 2021
1 parent 26b70b9 commit 0f591c2
Show file tree
Hide file tree
Showing 16 changed files with 318 additions and 288 deletions.
57 changes: 30 additions & 27 deletions examples/Asymmetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,51 @@
declare(strict_types=1);

use CQ\Crypto\Asymmetric;
use CQ\Crypto\Models\Keypair;
use CQ\Crypto\Models\AsymmetricKey;

try {
$string = 'Hello World!';

$assymetricKeypair = Asymmetric::genKey();
$encodedKeypair = $assymetricKeypair->toString();
// If no encodedKey is provided a new one is generated
$keypair = new AsymmetricKey();
$exportKeypair = $keypair->export();

$newAssymetricKeypair = new Keypair();
$newAssymetricKeypair->loadFromString(
encodedKeypair: $encodedKeypair
// By providing an exported key you can import it
$keypair2 = new AsymmetricKey(encodedKey: $exportKeypair);
$exportKeypair2 = $keypair2->export();

$client = new Asymmetric(
keypair: $keypair
);
$client2 = new Asymmetric(
keypair: $keypair2
);

$assymetricEncryptedString = Asymmetric::encrypt(
string: $string,
recieverEncryptionPublicKey: $assymetricKeypair->getEncryption()->getPublicKey()
$encryptedString = $client->encrypt(
string: $string
);
$assymetricDecryptedString = Asymmetric::decrypt(
encryptedString: $assymetricEncryptedString,
recieverEncryptionPrivateKey: $newAssymetricKeypair->getEncryption()->getSecretKey()
$decryptedString = $client2->decrypt(
encryptedString: $encryptedString
);
$assymetricSignature = Asymmetric::sign(
string: $string,
authenticationSecretKey: $assymetricKeypair->getAuthentication()->getSecretKey()

$signature = $client->sign(
string: $string
);
$assymetricVerify = Asymmetric::verify(
$verify = $client2->verify(
string: $string,
signature: $assymetricSignature,
authenticationPublicKey: $newAssymetricKeypair->getAuthentication()->getPublicKey()
signature: $signature
);
} catch (\Throwable $th) {
echo $th->getMessage();
} catch (\Throwable $error) {
echo $error->getMessage();
exit;
}

echo json_encode([
'string' => $string,
'assymetric' => [
'keypair' => $encodedKeypair,
'encrypted' => $assymetricEncryptedString,
'decrypted' => $assymetricDecryptedString,
'signature' => $assymetricSignature,
'verify' => $assymetricVerify,
],
'export' => $exportKey,
'export2' => $exportKey2,
'encrypted' => $encryptedString,
'decrypted' => $decryptedString,
'signature' => $signature,
'verify' => $verify,
]);
4 changes: 2 additions & 2 deletions examples/Hash.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
string: $string,
hash: $hash
);
} catch (\Throwable $th) {
echo $th->getMessage();
} catch (\Throwable $error) {
echo $error->getMessage();
exit;
}

Expand Down
29 changes: 14 additions & 15 deletions examples/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,33 @@

declare(strict_types=1);

use CQ\Crypto\Models\SymmetricKey;
use CQ\Crypto\Password;
use CQ\Crypto\Symmetric;

try {
$encryptionKey = Symmetric::genKey();
$context = null; // Optional variable
$string = 'Hello World!';
$context = null;

$password = new Password(
keystring: $encryptionKey
);
$key = new SymmetricKey();
$password = new Password(key: $key);

$plaintextPassword = 'Hello World!';
$encryptedHashedPassword = $password->hash(
plaintextPassword: $plaintextPassword,
plaintextPassword: $string,
context: $context
);
$passwordVerify = $password->verify(
plaintextPassword: $plaintextPassword,
$verify = $password->verify(
plaintextPassword: $string,
encryptedHashedPassword: $encryptedHashedPassword,
context: $context
);
} catch (\Throwable $th) {
echo $th->getMessage();
} catch (\Throwable $error) {
echo $error->getMessage();
exit;
}

echo json_encode([
'plaintextPassword' => $plaintextPassword,
'hashedPassword' => $encryptedHashedPassword,
'verifyPassword' => $passwordVerify,
'string' => $string,
'context' => $context,
'encryptedHashedPassword' => $encryptedHashedPassword,
'verify' => $verify,
]);
4 changes: 2 additions & 2 deletions examples/Random.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
$random = Random::string(
length: 32
);
} catch (\Throwable $th) {
echo $th->getMessage();
} catch (\Throwable $error) {
echo $error->getMessage();
exit;
}

Expand Down
48 changes: 30 additions & 18 deletions examples/Symmetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,52 @@

declare(strict_types=1);

use CQ\Crypto\Models\SymmetricKey;
use CQ\Crypto\Symmetric;

try {
$string = 'Hello World!';

$symmetricKey = Symmetric::genKey();
$symmetricClient = new Symmetric(
keystring: $symmetricKey
// If no encodedKey is provided a new one is generated
$key = new SymmetricKey();
$exportKey = $key->export();

// By providing an exported key you can import it
$key2 = new SymmetricKey(encodedKey: $exportKey);
$exportKey2 = $key2->export();

$client = new Symmetric(
key: $key
);
$client2 = new Symmetric(
key: $key2
);
$symmetricEncryptedString = $symmetricClient->encrypt(

$encryptedString = $client->encrypt(
string: $string
);
$symmetricDecryptedString = $symmetricClient->decrypt(
encryptedString: $symmetricEncryptedString
$decryptedString = $client2->decrypt(
encryptedString: $encryptedString
);
$symmetricSignature = $symmetricClient->sign(

$signature = $client->sign(
string: $string
);
$symmetricVerify = $symmetricClient->verify(
$verify = $client2->verify(
string: $string,
signature: $symmetricSignature
signature: $signature
);
} catch (\Throwable $th) {
echo $th->getMessage();
} catch (\Throwable $error) {
echo $error->getMessage();
exit;
}

echo json_encode([
'string' => $string,
'symmetric' => [
'key' => $symmetricKey,
'encrypted' => $symmetricEncryptedString,
'decrypted' => $symmetricDecryptedString,
'signature' => $symmetricSignature,
'verify' => $symmetricVerify,
],
'export' => $exportKey,
'export2' => $exportKey2,
'encrypted' => $encryptedString,
'decrypted' => $decryptedString,
'signature' => $signature,
'verify' => $verify,
]);
82 changes: 18 additions & 64 deletions src/Crypto/Asymmetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,107 +4,61 @@

namespace CQ\Crypto;

use CQ\Crypto\Models\AsymmetricKey;
use CQ\Crypto\Providers\CryptoProvider;
use ParagonIE\Halite\Asymmetric\Crypto;
use ParagonIE\Halite\Asymmetric\EncryptionPublicKey;
use ParagonIE\Halite\Asymmetric\EncryptionSecretKey;
use ParagonIE\Halite\Asymmetric\SignaturePublicKey;
use ParagonIE\Halite\Asymmetric\SignatureSecretKey;
use ParagonIE\Halite\KeyFactory;
use ParagonIE\HiddenString\HiddenString;
use CQ\Crypto\Models\Keypair;

class Asymmetric
final class Asymmetric extends CryptoProvider
{
public function __construct(
private Keypair $keypair
private AsymmetricKey $keypair
) {
}

/**
* Generate encryption keypair
*/
public static function genKey(): Keypair
{
$keypair = new Keypair();

$keypair->setAuthentication(
authentication: KeyFactory::generateSignatureKeyPair()
);
$keypair->setEncryption(
encryption: KeyFactory::generateEncryptionKeyPair()
);

return $keypair;
}

/**
* Encrypt string
*/
public static function encrypt(
string $string,
EncryptionPublicKey $recieverEncryptionPublicKey,
?EncryptionSecretKey $senderEncryptionSecretKey = null
): string {
if ($senderEncryptionSecretKey) {
return Crypto::encrypt(
plaintext: new HiddenString($string),
ourPrivateKey: $senderEncryptionSecretKey,
theirPublicKey: $recieverEncryptionPublicKey
);
}

public function encrypt(string $string): string
{
return Crypto::seal(
plaintext: new HiddenString(value: $string),
publicKey: $recieverEncryptionPublicKey
publicKey: $this->keypair->getEncryption()->getPublicKey()
);
}

/**
* Decrypt string
* Decrypt encryptedString
*/
public static function decrypt(
string $encryptedString,
EncryptionSecretKey $recieverEncryptionPrivateKey,
?EncryptionPublicKey $senderEncryptionPublicKey = null
): string {
if ($senderEncryptionPublicKey) {
return Crypto::decrypt(
ciphertext: $encryptedString,
ourPrivateKey: $recieverEncryptionPrivateKey,
theirPublicKey: $senderEncryptionPublicKey
)->getString();
}

public function decrypt(string $encryptedString): string
{
return Crypto::unseal(
ciphertext: $encryptedString,
privateKey: $recieverEncryptionPrivateKey
privateKey: $this->keypair->getEncryption()->getSecretKey()
)->getString();
}

/**
* Sign string
*/
public static function sign(
string $string,
SignatureSecretKey $authenticationSecretKey
): string {
public function sign(string $string): string
{
return Crypto::sign(
message: $string,
privateKey: $authenticationSecretKey
privateKey: $this->keypair->getAuthentication()->getSecretKey()
);
}

/**
* Verify string
* Verify string with signature
*/
public static function verify(
public function verify(
string $string,
string $signature,
SignaturePublicKey $authenticationPublicKey
string $signature
): bool {
return Crypto::verify(
message: $string,
publicKey: $authenticationPublicKey,
publicKey: $this->keypair->getAuthentication()->getPublicKey(),
signature: $signature
);
}
Expand Down
11 changes: 0 additions & 11 deletions src/Crypto/Exceptions/KeyException.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Crypto/Hash.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace CQ\Crypto;

class Hash
final class Hash
{
private static int $hash_cost = 2;

Expand Down

0 comments on commit 0f591c2

Please sign in to comment.