123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
- namespace Aws;
- class PhpHash implements HashInterface
- {
-
- private $context;
-
- private $algo;
-
- private $options;
-
- private $hash;
-
- public function __construct($algo, array $options = [])
- {
- $this->algo = $algo;
- $this->options = $options;
- }
- public function update($data)
- {
- if ($this->hash !== null) {
- $this->reset();
- }
- hash_update($this->getContext(), $data);
- }
- public function complete()
- {
- if ($this->hash) {
- return $this->hash;
- }
- $this->hash = hash_final($this->getContext(), true);
- if (isset($this->options['base64']) && $this->options['base64']) {
- $this->hash = base64_encode($this->hash);
- }
- return $this->hash;
- }
- public function reset()
- {
- $this->context = $this->hash = null;
- }
-
- private function getContext()
- {
- if (!$this->context) {
- $key = isset($this->options['key']) ? $this->options['key'] : '';
- $this->context = hash_init(
- $this->algo,
- $key ? HASH_HMAC : 0,
- $key
- );
- }
- return $this->context;
- }
- }
|