Skip to content

Commit

Permalink
Add support for the hashing API
Browse files Browse the repository at this point in the history
  • Loading branch information
fpoirotte committed Jul 28, 2017
1 parent 227636b commit f328fd3
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@
},
"autoload": {
"psr-4": {
"fpoirotte\\Cryptal\\Plugins\\": "src/"
"fpoirotte\\Cryptal\\Plugins\\Openssl\\": "src/"
}
},
"extra": {
"branch-alias": {
"dev-develop": "1.0.x-dev"
},
"cryptal.entrypoint": "fpoirotte\\Cryptal\\Plugins\\Openssl"
"cryptal.entrypoint": [
"fpoirotte\\Cryptal\\Plugins\\Openssl\\Crypto",
"fpoirotte\\Cryptal\\Plugins\\Openssl\\Hash"
]
}
}
10 changes: 5 additions & 5 deletions composer.lock

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

4 changes: 2 additions & 2 deletions src/Openssl.php → src/Crypto.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace fpoirotte\Cryptal\Plugins;
namespace fpoirotte\Cryptal\Plugins\Openssl;

use fpoirotte\Cryptal\Implementers\PluginInterface;
use fpoirotte\Cryptal\Implementers\CryptoInterface;
Expand All @@ -10,7 +10,7 @@
use fpoirotte\Cryptal\ModeEnum;
use fpoirotte\Cryptal\ImplementationTypeEnum;

class Openssl implements CryptoInterface, PluginInterface
class Crypto implements CryptoInterface, PluginInterface
{
protected $method;
protected $tagLength;
Expand Down
64 changes: 64 additions & 0 deletions src/Hash.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace fpoirotte\Cryptal\Plugins\Openssl;

use fpoirotte\Cryptal\Implementers\PluginInterface;
use fpoirotte\Cryptal\Implementers\HashInterface;
use fpoirotte\Cryptal\RegistryWrapper;
use fpoirotte\Cryptal\HashEnum;
use fpoirotte\Cryptal\ImplementationTypeEnum;

class Hash extends HashInterface implements PluginInterface
{
private $data;
protected $method;
protected static $supportedAlgos = null;

public function __construct(HashEnum $algorithm)
{
if (static::$supportedAlgos === null) {
static::checkSupport();
}

$this->method = $algorithm;
}

protected static function checkSupport()
{
$supported = array(
(string) HashEnum::HASH_MD2() => 'md2',
(string) HashEnum::HASH_MD4() => 'md4',
(string) HashEnum::HASH_MD5() => 'md5',
(string) HashEnum::HASH_RIPEMD160() => 'ripemd160',
(string) HashEnum::HASH_SHA1() => 'sha1',
(string) HashEnum::HASH_SHA224() => 'sha224',
(string) HashEnum::HASH_SHA256() => 'sha256',
(string) HashEnum::HASH_SHA384() => 'sha384',
(string) HashEnum::HASH_SHA512() => 'sha512',
);

static::$supportedAlgos = array_intersect($supported, openssl_get_md_methods(false));
}

protected function internalUpdate($data)
{
$this->data .= $data;
}

protected function internalFinish()
{
return openssl_digest($this->data, $this->method, true);
}

public static function registerAlgorithms(RegistryWrapper $registry)
{
static::checkSupport();
foreach (static::$supportedAlgos as $algo => $algoConst) {
$registry->addHash(
__CLASS__,
HashEnum::$algo(),
ImplementationTypeEnum::TYPE_COMPILED()
);
}
}
}

0 comments on commit f328fd3

Please sign in to comment.