Skip to content

Commit d0625d7

Browse files
committed
Add checksum list collection class
1 parent 90fbe70 commit d0625d7

File tree

2 files changed

+85
-33
lines changed

2 files changed

+85
-33
lines changed

src/ChecksumList.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Drupal\Signify;
4+
5+
class ChecksumList implements \Countable, \Iterator {
6+
7+
// Allowed checksum list verification algorithms and their base64-encoded lengths.
8+
protected $HASH_ALGO_BASE64_LENGTHS = array('SHA256' => 64, 'SHA512' => 128);
9+
10+
protected $checksums = array();
11+
12+
protected $position = 0;
13+
14+
public function __construct($checksum_list_raw, $list_is_trusted)
15+
{
16+
$lines = explode("\n", $checksum_list_raw);
17+
foreach ($lines as $line) {
18+
if (trim($line) == '') {
19+
continue;
20+
}
21+
22+
if (substr($line, 0, 1) === '\\') {
23+
throw new VerifierException('Filenames with problematic characters are not yet supported.');
24+
}
25+
26+
$algo = substr($line, 0, strpos($line, ' '));
27+
if (empty($this->HASH_ALGO_BASE64_LENGTHS[$algo])) {
28+
throw new VerifierException("Algorithm \"$algo\" is unsupported for checksum verification.");
29+
}
30+
31+
$filename_start = strpos($line, '(') + 1;
32+
$bytes_after_filename = $this->HASH_ALGO_BASE64_LENGTHS[$algo] + 4;
33+
$filename = substr($line, $filename_start, -$bytes_after_filename);
34+
35+
$verified_checksum = new VerifierFileChecksum($filename, $algo, substr($line, -$this->HASH_ALGO_BASE64_LENGTHS[$algo]), $list_is_trusted);
36+
$this->checksums[] = $verified_checksum;
37+
}
38+
39+
return $this->checksums;
40+
}
41+
42+
/**
43+
* @inheritDoc
44+
*/
45+
public function current() {
46+
return $this->checksums[$this->position];
47+
}
48+
49+
/**
50+
* @inheritDoc
51+
*/
52+
public function next() {
53+
$this->position += 1;
54+
}
55+
56+
/**
57+
* @inheritDoc
58+
*/
59+
public function key() {
60+
return $this->position;
61+
}
62+
63+
/**
64+
* @inheritDoc
65+
*/
66+
public function valid() {
67+
return isset($this->checksums[$this->position]);
68+
}
69+
70+
/**
71+
* @inheritDoc
72+
*/
73+
public function rewind() {
74+
$this->position = 0;
75+
}
76+
77+
/**
78+
* @inheritDoc
79+
*/
80+
public function count() {
81+
return iterator_count($this);
82+
}
83+
84+
}

src/Verifier.php

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ class Verifier
88
const COMMENTHDRLEN = 19;
99
const COMMENTMAXLEN = 1024;
1010

11-
// Allowed checksum list verification algorithms and their base64-encoded lengths.
12-
protected $HASH_ALGO_BASE64_LENGTHS = array('SHA256' => 64, 'SHA512' => 128);
13-
1411
/**
1512
* @var string
1613
*/
@@ -173,7 +170,7 @@ public function verifyChecksumList($signed_checksum_list, $working_directory)
173170
}
174171

175172
protected function verifyTrustedChecksumList($checksum_list_raw, $working_directory) {
176-
$checksum_list = $this->parseChecksumList($checksum_list_raw, true);
173+
$checksum_list = new ChecksumList($checksum_list_raw, true);
177174
$verified_count = 0;
178175

179176
/**
@@ -227,35 +224,6 @@ public function verifyChecksumFile($checksum_file) {
227224
return $this->verifyChecksumList($signed_checksum_list, $working_directory);
228225
}
229226

230-
protected function parseChecksumList($checksum_list_raw, $list_is_trusted)
231-
{
232-
$lines = explode("\n", $checksum_list_raw);
233-
$verified_checksums = array();
234-
foreach ($lines as $line) {
235-
if (trim($line) == '') {
236-
continue;
237-
}
238-
239-
if (substr($line, 0, 1) === '\\') {
240-
throw new VerifierException('Filenames with problematic characters are not yet supported.');
241-
}
242-
243-
$algo = substr($line, 0, strpos($line, ' '));
244-
if (empty($this->HASH_ALGO_BASE64_LENGTHS[$algo])) {
245-
throw new VerifierException("Algorithm \"$algo\" is unsupported for checksum verification.");
246-
}
247-
248-
$filename_start = strpos($line, '(') + 1;
249-
$bytes_after_filename = $this->HASH_ALGO_BASE64_LENGTHS[$algo] + 4;
250-
$filename = substr($line, $filename_start, -$bytes_after_filename);
251-
252-
$verified_checksum = new VerifierFileChecksum($filename, $algo, substr($line, -$this->HASH_ALGO_BASE64_LENGTHS[$algo]), $list_is_trusted);
253-
$verified_checksums[] = $verified_checksum;
254-
}
255-
256-
return $verified_checksums;
257-
}
258-
259227
/**
260228
* Verify a string message signed with CSIG chained-signature extended Signify format.
261229
*

0 commit comments

Comments
 (0)