1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the LGPL. For more information please see |
17
|
|
|
* <http://phing.info>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class ChecksumAlgorithm |
22
|
|
|
*/ |
23
|
|
|
class HashfileAlgorithm implements Algorithm |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Checksum algorithm to be used. |
27
|
|
|
*/ |
28
|
|
|
private $algorithm = 'md5'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Specifies the algorithm to be used to compute the checksum. |
32
|
|
|
* Defaults to "CRC". Other popular algorithms like "ADLER" may be used as well. |
33
|
|
|
* @param string $algorithm the digest algorithm to use |
34
|
|
|
*/ |
35
|
1 |
|
public function setAlgorithm(string $algorithm): void |
36
|
|
|
{ |
37
|
1 |
|
$this->algorithm = strtolower($algorithm); |
38
|
1 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* This algorithm supports `hash_algos()`. |
42
|
|
|
* @return bool <i>true</i> if all is ok, otherwise <i>false</i>. |
43
|
|
|
*/ |
44
|
1 |
|
public function isValid(): bool |
45
|
|
|
{ |
46
|
1 |
|
return in_array($this->algorithm, hash_algos(), true); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Computes a value for a file content with the specified checksum algorithm. |
51
|
|
|
* @param PhingFile $file File object for which the value should be evaluated. |
52
|
|
|
* @return string|null The value for that file |
53
|
|
|
*/ |
54
|
1 |
|
public function getValue(PhingFile $file): ?string |
55
|
|
|
{ |
56
|
1 |
|
if (!$this->isValid()) { |
57
|
|
|
throw new BuildException('Wrong hash algorithm.'); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
if ($file->canRead()) { |
61
|
1 |
|
return hash_file($this->algorithm, $file->getAbsolutePath()); |
62
|
|
|
} |
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string some information about this algorithm. |
68
|
|
|
*/ |
69
|
1 |
|
public function __toString(): string |
70
|
|
|
{ |
71
|
1 |
|
return sprintf('<%s:algorithm=%s>', __CLASS__, $this->algorithm); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|