Page MenuHomeDevCentral

D4086.diff
No OneTemporary

D4086.diff

diff --git a/github/src/XHubSignature256.php b/github/src/XHubSignature256.php
new file mode 100644
--- /dev/null
+++ b/github/src/XHubSignature256.php
@@ -0,0 +1,66 @@
+<?php
+
+namespace Keruald\GitHub;
+
+class XHubSignature256 extends XHubSignature {
+
+ const string DEFAULT_ALGO = "sha256";
+
+ /**
+ * Initializes a new instance of the XHubSignature256 class
+ *
+ * @param string $secret the secret token
+ * @param string $algo the hash algorithm [facultative]
+ */
+ public function __construct ($secret, $algo = self::DEFAULT_ALGO) {
+ parent::__construct($secret, $algo);
+ }
+
+ ///
+ /// Static helper methods
+ ///
+
+ /**
+ * Computes a signature for the specified secret and payload
+ *
+ * @param string $secret the secret token to secure messages
+ * @param string $payload the payload
+ * @param string $algo the hash algorithm [facultative]
+ *
+ * @return string the payload signature
+ */
+ public static function hashPayload(
+ $secret,
+ $payload,
+ $algo = self::DEFAULT_ALGO,
+ ) {
+ $instance = new static($secret, $algo);
+ $instance->payload = $payload;
+
+ return $instance->compute();
+ }
+
+ /**
+ * Validates a payload against specified secret
+ *
+ * @param string $secret the secret token to secure messages
+ * @param string $payload the payload
+ * @param string $signature the signature delivered with the payload
+ * @param string $algo the hash algorithm [facultative]
+ *
+ * @return bool true if the signature is correct; otherwise, false.
+ */
+ public static function validatePayload (
+ $secret,
+ $payload,
+ $signature,
+ $algo = self::DEFAULT_ALGO,
+ ) {
+ $instance = new static($secret, $algo);
+ $instance->payload = $payload;
+ $instance->signature = $signature;
+
+ return $instance->validate();
+ }
+
+}
diff --git a/github/tests/XHubSignature256Test.php b/github/tests/XHubSignature256Test.php
new file mode 100644
--- /dev/null
+++ b/github/tests/XHubSignature256Test.php
@@ -0,0 +1,39 @@
+<?php declare(strict_types=1);
+
+use PHPUnit\Framework\TestCase;
+
+use Keruald\GitHub\XHubSignature256;
+
+require 'XHubSignatureConstants.php';
+
+class XHubSignature256Test extends TestCase {
+ protected XHubSignature256 $instance;
+
+ protected function setUp() : void {
+ $this->instance = new XHubSignature256(DEFAULT_256_SECRET);
+
+ $this->instance->payload = DEFAULT_256_PAYLOAD;
+ }
+
+ ///
+ /// Tests
+ ///
+
+ public function testValidate() : void {
+ $this->instance->signature = DEFAULT_256_SIGNATURE;
+ $this->assertTrue($this->instance->validate());
+ }
+
+ ///
+ /// Test static helper methods
+ ///
+
+ public function testValidatePayload() : void {
+ $this->assertTrue(XHubSignature256::validatePayload(
+ DEFAULT_256_SECRET,
+ DEFAULT_256_PAYLOAD,
+ DEFAULT_256_SIGNATURE
+ ));
+ }
+
+}

File Metadata

Mime Type
text/plain
Expires
Tue, Apr 21, 02:23 (22 h, 4 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3644926
Default Alt Text
D4086.diff (3 KB)

Event Timeline