Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD RSA parameters WIP #1

Merged
merged 6 commits into from
Sep 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: PHP Composer

on:
push:
branches: [ main ]
branches: [ main, development/* ]
pull_request:
branches: [ main ]

Expand Down
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.php @MayMeow
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
"php": "7.4.*"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.6"
},
"scripts": {
"test": "phpunit tests"
"test": "phpunit tests",
"codesniffer": "phpcs --standard=PSR2 src"
}
}
58 changes: 57 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 19 additions & 3 deletions src/AESCryptoServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,15 @@ public function generateIV()
*/
public function encrypt(string $plainText): string
{
$encryptedBytes = openssl_encrypt($plainText, $this->cipher, $this->key, OPENSSL_RAW_DATA, $this->iv, $this->tag, $this->aad);
$encryptedBytes = openssl_encrypt(
$plainText,
$this->cipher,
$this->key,
OPENSSL_RAW_DATA,
$this->iv,
$this->tag,
$this->aad
);

return base64_encode($this->iv . $this->tag . $encryptedBytes);
}
Expand All @@ -103,6 +111,14 @@ public function decrypt(string $encryptedData): string
$this->tag = substr($c, $iv_len, static::DEFAULT_GCM_TAG_LENGTH);
$encryptedBytes = substr($c, $iv_len + static::DEFAULT_GCM_TAG_LENGTH);

return openssl_decrypt($encryptedBytes, $this->cipher, $this->key, OPENSSL_RAW_DATA, $this->iv, $this->tag, $this->aad);
return openssl_decrypt(
$encryptedBytes,
$this->cipher,
$this->key,
OPENSSL_RAW_DATA,
$this->iv,
$this->tag,
$this->aad
);
}
}
}
12 changes: 8 additions & 4 deletions src/CryptoKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class CryptoKey
{
public function HelloWorld() : string
public function helloWorld() : string
{
return "Hello World";
}
Expand All @@ -17,8 +17,12 @@ public function HelloWorld() : string
* @param int $length
* @return string
*/
public function getCryptographicKey(string $password, ?string $salt = null, int $iterations = 1024, int $length = 48) : string
{
public function getCryptographicKey(
string $password,
?string $salt = null,
int $iterations = 1024,
int $length = 48
) : string {
return hash_pbkdf2("sha256", $password, $salt, $iterations, $length);
}
}
}
109 changes: 109 additions & 0 deletions src/RSACryptoServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace MayMeow\Cryptography;

class RSACryptoServiceProvider
{
protected RSAParameters $parameters;

/**
* @param RSAParameters $parameters
*/
public function setParameters(RSAParameters $parameters): void
{
$this->parameters = $parameters;
}

/**
* encrypt file with public key
*/
public function encrypt($plainText) : string
{
$encrypted = '';

openssl_public_encrypt($plainText, $encrypted, $this->parameters->getPublicKey());

return base64_encode($encrypted);
}

/**
* decrypt with private key
*/
public function decrypt($encryptedText) : string
{
$plainText = '';
$privKey = $this->parameters->getPrivateKey();

openssl_private_decrypt(base64_decode($encryptedText), $plainText, $privKey);

return $plainText;
}

public function private_encrypt($plainText) : string
{
$encrypted = '';
$privKey = $this->parameters->getPrivateKey();

openssl_private_encrypt($plainText, $encrypted, $privKey);

return base64_encode($encrypted);
}

public function public_decrypt($encryptedText) : string
{
$plainText = '';
openssl_public_decrypt(base64_decode($encryptedText), $plainText, $this->parameters->getPublicKey());

return $plainText;
}

protected function seal(string $plain_text) : string
{
//openssl_open($plain_text, $sealed_data, $ekeys, [$this->parameters->getPrivateKey()])
}

protected function open()
{

}

/**
* @param $data
* @return string
*/
public function sign($data) : string
{
$privKey = $this->_getPrivateKey();

$result = openssl_sign($data, $signature, $privKey, OPENSSL_ALGO_SHA512);

return base64_encode($signature);
}

/**
* @param $data
* @param $signature
* @return bool
*/
public function verify($data, $signature) : bool
{
$verification = openssl_verify($data, base64_decode($signature), $this->parameters->getPublicKey(), OPENSSL_ALGO_SHA512);

return (bool)$verification;
}

/**
* @return string
*/
public function getFingerPrint() : string
{
$fingerprint = join(':', str_split(md5(base64_decode($this->parameters->getPublicKey())), 2));

return $fingerprint;
}

protected function _getPrivateKey()
{
return $this->parameters->getPrivateKey();
}
}
103 changes: 103 additions & 0 deletions src/RSAParameters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace MayMeow\Cryptography;

class RSAParameters
{
private string $privateKey;
private string $publicKey;
private string $passphrase;

protected array $config = [
'digest_alg' => 'sha512',
'private_key_bits' => 4096,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
];

public function __construct()
{
}

public function generateKeys(?string $passphrase = null, ?array $configArgs = null) : void
{
$keys = openssl_pkey_new($this->config);

if ($passphrase != null) {
$this->passphrase = $passphrase;
}

openssl_pkey_export($keys, $private, $passphrase, $configArgs);
$this->privateKey = $private;

$pub = openssl_pkey_get_details($keys);
$this->publicKey = $pub['key'];
}

/**
* @return string
*/
public function getPrivateKey()
{
if ($this->passphrase != null && $this->privateKey != null) {
return openssl_pkey_get_private($this->privateKey, $this->passphrase);
}

return $this->publicKey;
}

/**
* @param string $privateKey
*/
public function setPrivateKey(string $privateKey): void
{
$this->privateKey = $privateKey;
}

/**
* @return string
*/
public function getPublicKey(): string
{
return $this->publicKey;
}

/**
* @param string $publicKey
*/
public function setPublicKey(string $publicKey): void
{
$this->publicKey = $publicKey;
}

/**
* @return string
*/
public function getPassphrase(): string
{
return $this->passphrase;
}

/**
* @param string $passphrase
*/
public function setPassphrase(string $passphrase): void
{
$this->passphrase = $passphrase;
}

/**
* @return array
*/
public function getConfig(): array
{
return $this->config;
}

/**
* @param array $config
*/
public function setConfig(array $config): void
{
$this->config = $config;
}
}
2 changes: 1 addition & 1 deletion tests/PBKDF2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function TestHelloWorld() :void
{
$p = new CryptoKey();

$this->assertEquals("Hello World", $p->HelloWorld());
$this->assertEquals("Hello World", $p->helloWorld());
}

/** @test */
Expand Down
Loading