Skip to content

Commit

Permalink
rename CryptTool to Encryptor
Browse files Browse the repository at this point in the history
  • Loading branch information
pavarnos committed Aug 12, 2018
1 parent 260b12f commit 05b7830
Show file tree
Hide file tree
Showing 20 changed files with 138 additions and 143 deletions.
4 changes: 2 additions & 2 deletions bin/threema-gateway
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
*/

use Symfony\Component\Console\Application;
use Threema\MsgApi\Tools\CryptTool;
use Threema\MsgApi\Encryptor\AbstractEncryptor;

$autoloaderInWorkingDirectory = getcwd() . '/vendor/autoload.php';
if (is_file($autoloaderInWorkingDirectory)) {
require_once $autoloaderInWorkingDirectory;
}

if (!class_exists(CryptTool::class, true)) {
if (!class_exists(AbstractEncryptor::class, true)) {
$composerAutoloadFile = __DIR__ . '/../vendor/autoload.php';
if (!is_file($composerAutoloadFile)) {
$composerAutoloadFile = __DIR__ . '/../../../autoload.php';
Expand Down
6 changes: 3 additions & 3 deletions src/Threema/Console/Symfony/AbstractLocalCommand.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php /** @noinspection HtmlUnknownTag */
/**
* @author Threema GmbH
* @copyright Copyright (c) 2015-2016 Threema GmbH
Expand All @@ -15,7 +15,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Threema\MsgApi\Constants;
use Threema\MsgApi\Exceptions\InvalidArgumentException;
use Threema\MsgApi\Tools\CryptTool;
use Threema\MsgApi\Encryptor\AbstractEncryptor;

abstract class AbstractLocalCommand extends Command
{
Expand Down Expand Up @@ -118,6 +118,6 @@ private function getKey(string $key, string $optionName, string $keyPrefix): str
if (empty($key)) {
throw new InvalidArgumentException(ucfirst($optionName) . ' key invalid or missing');
}
return CryptTool::getInstance()->hex2bin(str_replace($keyPrefix, '', $key));
return AbstractEncryptor::getInstance()->hex2bin(str_replace($keyPrefix, '', $key));
}
}
8 changes: 4 additions & 4 deletions src/Threema/Console/Symfony/DecryptCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Threema\MsgApi\Tools\CryptTool;
use Threema\MsgApi\Encryptor\AbstractEncryptor;

class DecryptCommand extends AbstractLocalCommand
{
Expand All @@ -30,9 +30,9 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->loadDefaults($input, $output);
$cryptTool = CryptTool::getInstance();
$message = $cryptTool->decryptMessage($cryptTool->hex2bin($this->getMessage($input)),
$this->getPrivateKey($input, $output), $this->getPublicKey($input), $cryptTool->hex2bin($input->getArgument('nonce')));
$encryptor = AbstractEncryptor::getInstance();
$message = $encryptor->decryptMessage($encryptor->hex2bin($this->getMessage($input)),
$this->getPrivateKey($input, $output), $this->getPublicKey($input), $encryptor->hex2bin($input->getArgument('nonce')));
$output->writeln($message->__toString());
return 0;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Threema/Console/Symfony/DerivePublicKeyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Threema\MsgApi\Tools\CryptTool;
use Threema\MsgApi\Encryptor\AbstractEncryptor;

class DerivePublicKeyCommand extends AbstractLocalCommand
{
Expand All @@ -26,9 +26,9 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->loadDefaults($input, $output);
$cryptTool = CryptTool::getInstance();
$publicKey = $cryptTool->derivePublicKey($this->getPrivateKey($input, $output));
$output->writeln($cryptTool->bin2hex($publicKey));
$encryptor = AbstractEncryptor::getInstance();
$publicKey = $encryptor->derivePublicKey($this->getPrivateKey($input, $output));
$output->writeln($encryptor->bin2hex($publicKey));
return 0;
}
}
12 changes: 6 additions & 6 deletions src/Threema/Console/Symfony/EncryptCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Threema\MsgApi\Tools\CryptTool;
use Threema\MsgApi\Encryptor\AbstractEncryptor;

class EncryptCommand extends AbstractLocalCommand
{
Expand All @@ -28,16 +28,16 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->loadDefaults($input, $output);
$cryptTool = CryptTool::getInstance();
$nonce = $cryptTool->randomNonce();
$text = $cryptTool->encryptMessageText($this->getMessage($input), $this->getPrivateKey($input, $output),
$encryptor = AbstractEncryptor::getInstance();
$nonce = $encryptor->randomNonce();
$text = $encryptor->encryptMessageText($this->getMessage($input), $this->getPrivateKey($input, $output),
$this->getPublicKey($input), $nonce);

$output->writeln('Nonce:', OutputInterface::VERBOSITY_VERBOSE);
$output->writeln($cryptTool->bin2hex($nonce));
$output->writeln($encryptor->bin2hex($nonce));

$output->writeln('Encrypted Text:', OutputInterface::VERBOSITY_VERBOSE);
$output->writeln($cryptTool->bin2hex($text));
$output->writeln($encryptor->bin2hex($text));
return 0;
}
}
10 changes: 5 additions & 5 deletions src/Threema/Console/Symfony/GenerateKeyPairCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Threema\MsgApi\Helpers\KeyPrefix;
use Threema\MsgApi\Tools\CryptTool;
use Threema\MsgApi\Encryptor\AbstractEncryptor;

class GenerateKeyPairCommand extends AbstractLocalCommand
{
Expand All @@ -30,12 +30,12 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$cryptTool = CryptTool::getInstance();
$keyPair = $cryptTool->generateKeyPair();
$encryptor = AbstractEncryptor::getInstance();
$keyPair = $encryptor->generateKeyPair();
$this->writeKey($input->getArgument('private-key-file'),
KeyPrefix::addPrivate($cryptTool->bin2hex($keyPair->getPrivateKey())), $output);
KeyPrefix::addPrivate($encryptor->bin2hex($keyPair->getPrivateKey())), $output);
$this->writeKey($input->getArgument('public-key-file'),
KeyPrefix::addPublic($cryptTool->bin2hex($keyPair->getPublicKey())), $output);
KeyPrefix::addPublic($encryptor->bin2hex($keyPair->getPublicKey())), $output);
$output->writeln('Key pair generated', OutputInterface::VERBOSITY_VERBOSE);
return 0;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Threema/Console/Symfony/HashEmailCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Threema\MsgApi\Tools\CryptTool;
use Threema\MsgApi\Encryptor\AbstractEncryptor;

class HashEmailCommand extends AbstractLocalCommand
{
Expand All @@ -26,8 +26,8 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$cryptTool = CryptTool::getInstance();
$output->writeln($cryptTool->hashEmail($input->getArgument('email')));
$encryptor = AbstractEncryptor::getInstance();
$output->writeln($encryptor->hashEmail($input->getArgument('email')));
return 0;
}
}
6 changes: 3 additions & 3 deletions src/Threema/Console/Symfony/HashPhoneCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Threema\MsgApi\Tools\CryptTool;
use Threema\MsgApi\Encryptor\AbstractEncryptor;

class HashPhoneCommand extends AbstractLocalCommand
{
Expand All @@ -26,8 +26,8 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$cryptTool = CryptTool::getInstance();
$output->writeln($cryptTool->hashPhoneNo($input->getArgument('phone')));
$encryptor = AbstractEncryptor::getInstance();
$output->writeln($encryptor->hashPhoneNo($input->getArgument('phone')));
return 0;
}
}
10 changes: 5 additions & 5 deletions src/Threema/Console/Symfony/MessageReceive.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Threema\MsgApi\Exceptions\BadMessageException;
use Threema\MsgApi\Helpers\E2EHelper;
use Threema\MsgApi\Tools\CryptTool;
use Threema\MsgApi\Encryptor\AbstractEncryptor;

class MessageReceive extends AbstractNetworkedCommand
{
Expand All @@ -32,13 +32,13 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$cryptTool = CryptTool::getInstance();
$encryptor = AbstractEncryptor::getInstance();
$helper = new E2EHelper($this->getPrivateKey($input, $output), $this->getConnection($input, $output));
$result = $helper->receiveMessage(
$cryptTool->hex2bin($input->getArgument('sender-key')),
$encryptor->hex2bin($input->getArgument('sender-key')),
$input->getArgument('message-id'),
$cryptTool->hex2bin($this->getMessage($input)),
$cryptTool->hex2bin($input->getArgument('nonce')),
$encryptor->hex2bin($this->getMessage($input)),
$encryptor->hex2bin($input->getArgument('nonce')),
$input->getArgument('folder')
);

Expand Down
8 changes: 4 additions & 4 deletions src/Threema/Console/Symfony/VersionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Threema\MsgApi\Constants;
use Threema\MsgApi\Tools\CryptTool;
use Threema\MsgApi\Encryptor\AbstractEncryptor;

class VersionCommand extends Command
{
protected function configure()
{
$this->setName('api:version')
->setAliases(['version'])
->setDescription('Show API version, CryptTool version, Feature level');
->setDescription('Show API version, Encryptor version, Feature level');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$left = function (int $level) {
return $level == Constants::MSGAPI_SDK_FEATURE_LEVEL ? '===>' : '';
};
$defaultCryptTool = CryptTool::getInstance();
$encryptor = AbstractEncryptor::getInstance();
$output->writeln('Threema PHP MsgApi Tool');
$output->writeln('Gateway API Version: ' . Constants::MSGAPI_SDK_VERSION);
$output->writeln('CryptTool: ' . $defaultCryptTool->getName() . ' (' . $defaultCryptTool->getDescription() . ')');
$output->writeln('Encryptor: ' . $encryptor->getName() . ' (' . $encryptor->getDescription() . ')');
$output->writeln('Feature level: ' . Constants::MSGAPI_SDK_FEATURE_LEVEL);
$table = new Table($output);
$table->getStyle()->setPadType(STR_PAD_LEFT);
Expand Down
8 changes: 4 additions & 4 deletions src/Threema/MsgApi/Commands/LookupBulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace Threema\MsgApi\Commands;

use Threema\MsgApi\Commands\Results\LookupBulkResult;
use Threema\MsgApi\Tools\CryptTool;
use Threema\MsgApi\Encryptor\AbstractEncryptor;

/**
* It is possible (though very unlikely) that two different phone numbers or emails will hash to the same value.
Expand Down Expand Up @@ -59,15 +59,15 @@ public function getParams()

public function getJson(): string
{
$cryptTool = CryptTool::getInstance();
$encryptor = AbstractEncryptor::getInstance();
$request = [];
foreach ($this->phone as $id => $phoneNumber) {
$hashedPhoneNumber = $cryptTool->hashPhoneNo($phoneNumber);
$hashedPhoneNumber = $encryptor->hashPhoneNo($phoneNumber);
$request['phoneHashes'][] = $hashedPhoneNumber;
$this->hashedPhone[$hashedPhoneNumber][$id] = $phoneNumber;
}
foreach ($this->email as $id => $emailAddress) {
$hashedEmail = $cryptTool->hashEmail($emailAddress);
$hashedEmail = $encryptor->hashEmail($emailAddress);
$request['emailHashes'][] = $hashedEmail;
$this->hashedEmail[$hashedEmail][$id] = $emailAddress;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Threema/MsgApi/Commands/LookupEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace Threema\MsgApi\Commands;

use Threema\MsgApi\Commands\Results\LookupIdResult;
use Threema\MsgApi\Tools\CryptTool;
use Threema\MsgApi\Encryptor\AbstractEncryptor;

class LookupEmail implements CommandInterface
{
Expand Down Expand Up @@ -45,7 +45,7 @@ public function getParams()
*/
public function getPath()
{
return 'lookup/email_hash/' . urlencode(CryptTool::getInstance()->hashEmail($this->emailAddress));
return 'lookup/email_hash/' . urlencode(AbstractEncryptor::getInstance()->hashEmail($this->emailAddress));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Threema/MsgApi/Commands/LookupPhone.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace Threema\MsgApi\Commands;

use Threema\MsgApi\Commands\Results\LookupIdResult;
use Threema\MsgApi\Tools\CryptTool;
use Threema\MsgApi\Encryptor\AbstractEncryptor;

class LookupPhone implements CommandInterface
{
Expand Down Expand Up @@ -45,7 +45,7 @@ public function getParams()
*/
public function getPath()
{
return 'lookup/phone_hash/' . urlencode(CryptTool::getInstance()->hashPhoneNo($this->phoneNumber));
return 'lookup/phone_hash/' . urlencode(AbstractEncryptor::getInstance()->hashPhoneNo($this->phoneNumber));
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Threema/MsgApi/Commands/SendE2E.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace Threema\MsgApi\Commands;

use Threema\MsgApi\Commands\Results\SendE2EResult;
use Threema\MsgApi\Tools\CryptTool;
use Threema\MsgApi\Encryptor\AbstractEncryptor;

class SendE2E implements CommandInterface
{
Expand Down Expand Up @@ -59,11 +59,11 @@ public function getBox()
*/
public function getParams()
{
$cryptTool = CryptTool::getInstance();
$encryptor = AbstractEncryptor::getInstance();

$p['to'] = $this->threemaId;
$p['nonce'] = $cryptTool->bin2hex($this->getNonce());
$p['box'] = $cryptTool->bin2hex($this->getBox());
$p['nonce'] = $encryptor->bin2hex($this->getNonce());
$p['box'] = $encryptor->bin2hex($this->getBox());
return $p;
}

Expand Down
Loading

0 comments on commit 05b7830

Please sign in to comment.