Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/PhpHash.php

raw symbols SCIP
1 <?php
2 namespace Aws;
3
4 /**
5 * Incremental hashing using PHP's hash functions.
6 */
7 class PhpHash implements HashInterface
8 {
9 /** @var resource|\HashContext */
10 private $context;
11
12 /** @var string */
13 private $algo;
14
15 /** @var array */
16 private $options;
17
18 /** @var string */
19 private $hash;
20
21 /**
22 * @param string $algo Hashing algorithm. One of PHP's hash_algos()
23 * return values (e.g. md5, sha1, etc...).
24 * @param array $options Associative array of hashing options:
25 * - key: Secret key used with the hashing algorithm.
26 * - base64: Set to true to base64 encode the value when complete.
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 * Get a hash context or create one if needed
65 *
66 * @return resource|\HashContext
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 }