2020-09-22 12:19:48 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace OCA\BigBlueButton;
|
|
|
|
|
|
|
|
use OCP\Security\ICrypto;
|
|
|
|
|
|
|
|
class Crypto {
|
|
|
|
/** @var ICrypto */
|
|
|
|
private $crypto;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
ICrypto $crypto
|
|
|
|
) {
|
|
|
|
$this->crypto = $crypto;
|
|
|
|
}
|
|
|
|
|
2021-02-24 16:33:37 +01:00
|
|
|
/**
|
|
|
|
* @return false|string
|
|
|
|
*/
|
|
|
|
public function calculateHMAC(string $message) {
|
2020-09-22 12:19:48 +02:00
|
|
|
if ($message === null) {
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->encodeBase64UrlSafe(\sha1($this->crypto->calculateHMAC($message), true));
|
|
|
|
}
|
|
|
|
|
2021-02-24 15:23:26 +01:00
|
|
|
public function verifyHMAC(string $message, string $mac): bool {
|
2020-09-22 12:19:48 +02:00
|
|
|
if ($message === null || $mac === null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$validMac = $this->encodeBase64UrlSafe(\sha1($this->crypto->calculateHMAC($message), true));
|
|
|
|
|
|
|
|
return $validMac === $mac;
|
|
|
|
}
|
|
|
|
|
2021-02-24 15:23:26 +01:00
|
|
|
/**
|
|
|
|
* @return false|string
|
|
|
|
*/
|
2021-02-13 16:14:40 +01:00
|
|
|
private function encodeBase64UrlSafe(string $data) {
|
2020-09-22 12:19:48 +02:00
|
|
|
$b64 = \base64_encode($data);
|
|
|
|
|
|
|
|
if ($b64 === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return \rtrim(\strtr($b64, '+/', '-_'), '=');
|
|
|
|
}
|
|
|
|
}
|