| 1 |
<?php |
| 2 |
namespace Aws; |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
class PhpHash implements HashInterface |
| 8 |
{ |
| 9 |
|
| 10 |
private $context; |
| 11 |
|
| 12 |
|
| 13 |
private $algo; |
| 14 |
|
| 15 |
|
| 16 |
private $options; |
| 17 |
|
| 18 |
|
| 19 |
private $hash; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public function __construct($algo, array $options = []) |
| 29 |
{ |
| 30 |
$this->algo = $algo; |
| 31 |
$this->options = $options; |
| 32 |
} |
| 33 |
|
| 34 |
public function update($data) |
| 35 |
{ |
| 36 |
if ($this->hash !== null) { |
| 37 |
$this->reset(); |
| 38 |
} |
| 39 |
|
| 40 |
hash_update($this->getContext(), $data); |
| 41 |
} |
| 42 |
|
| 43 |
public function complete() |
| 44 |
{ |
| 45 |
if ($this->hash) { |
| 46 |
return $this->hash; |
| 47 |
} |
| 48 |
|
| 49 |
$this->hash = hash_final($this->getContext(), true); |
| 50 |
|
| 51 |
if (isset($this->options['base64']) && $this->options['base64']) { |
| 52 |
$this->hash = base64_encode($this->hash); |
| 53 |
} |
| 54 |
|
| 55 |
return $this->hash; |
| 56 |
} |
| 57 |
|
| 58 |
public function reset() |
| 59 |
{ |
| 60 |
$this->context = $this->hash = null; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
private function getContext() |
| 69 |
{ |
| 70 |
if (!$this->context) { |
| 71 |
$key = isset($this->options['key']) ? $this->options['key'] : ''; |
| 72 |
$this->context = hash_init( |
| 73 |
$this->algo, |
| 74 |
$key ? HASH_HMAC : 0, |
| 75 |
$key |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
return $this->context; |
| 80 |
} |
| 81 |
} |