cryptal  latest
Cryptography Abstraction Layer
Hash.php
1 <?php
2 
4 
7 
8 class Hash extends \php_user_filter
9 {
10  protected $context;
11 
12  public function onCreate()
13  {
14  if (!isset($this->params['algorithm']) ||
15  !is_object($this->params['algorithm']) ||
16  !($this->params['algorithm'] instanceof HashEnum)) {
17  throw new \InvalidArgumentException('Invalid algorithm');
18  }
19 
20  $allowUnsafe = isset($this->params['allowUnsafe']) ? (bool) $this->params['allowUnsafe'] : false;
21  $this->context = Registry::buildHash($this->params['algorithm'], $allowUnsafe);
22  return true;
23  }
24 
25  public function filter($in, $out, &$consumed, $closing)
26  {
27  while ($bucket = stream_bucket_make_writeable($in)) {
28  $this->context->update($bucket->data);
29  $consumed += $bucket->datalen;
30  }
31 
32  if ($closing) {
33  $bucket = stream_bucket_new($this->stream, $this->context->finalize(true));
34  stream_bucket_append($out, $bucket);
35  }
36 
37  return PSFS_PASS_ON;
38  }
39 }