-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
152 additions
and
5 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 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,32 @@ | ||
<?php | ||
|
||
namespace AndroidSmsGateway; | ||
|
||
use AndroidSmsGateway\Domain\Message; | ||
use AndroidSmsGateway\Domain\MessageState; | ||
use Http\Client\HttpClient; | ||
|
||
class EncryptedClient extends Client { | ||
protected Encryptor $encryptor; | ||
|
||
public function __construct( | ||
string $passphrase, | ||
string $login, | ||
string $password, | ||
string $serverUrl = self::DEFAULT_URL, | ||
?HttpClient $client = null | ||
) { | ||
parent::__construct($login, $password, $serverUrl, $client); | ||
|
||
$this->encryptor = new Encryptor($passphrase); | ||
} | ||
|
||
public function Send(Message $message): MessageState { | ||
$message = $message->Encrypt($this->encryptor); | ||
return parent::Send($message)->Decrypt($this->encryptor); | ||
} | ||
|
||
public function GetState(string $id): MessageState { | ||
return parent::GetState($id)->Decrypt($this->encryptor); | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
namespace AndroidSmsGateway; | ||
|
||
class Encryptor { | ||
protected string $passphrase; | ||
|
||
public function __construct( | ||
string $passphrase | ||
) { | ||
$this->passphrase = $passphrase; | ||
} | ||
|
||
public function Encrypt(string $data): string { | ||
$salt = $this->generateSalt(); | ||
$secretKey = $this->generateSecretKeyFromPassphrase($this->passphrase, $salt); | ||
|
||
return base64_encode($salt) . '.' . openssl_encrypt($data, 'aes-256-cbc', $secretKey, 0, $salt); | ||
} | ||
|
||
public function Decrypt(string $data): string { | ||
list($saltBase64, $encryptedBase64) = explode('.', $data, 2); | ||
|
||
$salt = base64_decode($saltBase64); | ||
$secretKey = $this->generateSecretKeyFromPassphrase($this->passphrase, $salt); | ||
|
||
return openssl_decrypt($encryptedBase64, 'aes-256-cbc', $secretKey, 0, $salt); | ||
} | ||
|
||
protected function generateSalt(int $size = 16): string { | ||
return random_bytes($size); | ||
} | ||
|
||
protected function generateSecretKeyFromPassphrase( | ||
string $passphrase, | ||
string $salt, | ||
int $keyLength = 32, | ||
int $iterationCount = 10000 | ||
): string { | ||
return hash_pbkdf2('sha1', $passphrase, $salt, $iterationCount, $keyLength, true); | ||
} | ||
} |
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