Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
signature
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
5.12
0.00% covered (danger)
0.00%
0 / 1
 valid
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
5.12
1<?php
2// This file is part of Moodle - https://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <https://www.gnu.org/licenses/>.
16
17namespace message_notifyhub\local;
18
19/**
20 * The second check on a channel release: an Ed25519 signature made with the DevLMS release key.
21 *
22 * GitHub's SHA-256 proves the file is the one attached to the release; the signature proves the release was made
23 * with our key, so someone who got into the GitHub repository still cannot ship a channel the hub will install.
24 *
25 * @package    message_notifyhub
26 * @copyright  2026 DevLMS <contact@devlms.com>
27 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 */
29final class signature {
30    /** @var string The DevLMS release public key (Ed25519, base64). */
31    public const PUBLIC_KEY = '5dzReZgXfSHqHK0D0eDTqntq2t2WR17FU1ayM/G90dg=';
32
33    /** @var string|null For tests: another public key. */
34    public static ?string $publickey = null;
35
36    /**
37     * Whether a file carries a valid signature.
38     *
39     * @param string $data The file.
40     * @param string $signature Base64 signature, as in the release's .sig file.
41     * @return bool
42     */
43    public static function valid(string $data, string $signature): bool {
44        $sig = base64_decode(trim($signature), true);
45        $key = base64_decode(self::$publickey ?? self::PUBLIC_KEY, true);
46        if (
47            $sig === false || strlen($sig) !== SODIUM_CRYPTO_SIGN_BYTES || $key === false
48                || strlen($key) !== SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES
49        ) {
50            return false;
51        }
52        return sodium_crypto_sign_verify_detached($sig, $data, $key);
53    }
54}