X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FUtil%2FLDSignature.php;h=8cfadb16dada19da23ac65ccf997bb610bd448af;hb=d4a5a8051ad34a7be72238967afb3e6b140afdc8;hp=48beeeb363e7c2a3ececc26878f1e8e28854585c;hpb=0a4119adaf6294bf43d135a0f435c1dd677c50e0;p=friendica.git diff --git a/src/Util/LDSignature.php b/src/Util/LDSignature.php index 48beeeb363..8cfadb16da 100644 --- a/src/Util/LDSignature.php +++ b/src/Util/LDSignature.php @@ -1,4 +1,23 @@ . + * + */ namespace Friendica\Util; @@ -12,12 +31,24 @@ use Friendica\Model\APContact; */ class LDSignature { - public static function isSigned($data) + /** + * Checks if element 'signature' is found and not empty + * + * @param array $data + * @return bool + */ + public static function isSigned(array $data): bool { return !empty($data['signature']); } - public static function getSigner($data) + /** + * Returns actor (signer) from given data + * + * @param array $data + * @return mixed Returns actor or false on error + */ + public static function getSigner(array $data) { if (!self::isSigned($data)) { return false; @@ -38,7 +69,7 @@ class LDSignature $dhash = self::hash(self::signableData($data)); $x = Crypto::rsaVerify($ohash . $dhash, base64_decode($data['signature']['signatureValue']), $pubkey); - Logger::log('LD-verify: ' . intval($x)); + Logger::info('LD-verify', ['verified' => (int)$x, 'actor' => $profile['url']]); if (empty($x)) { return false; @@ -47,13 +78,20 @@ class LDSignature } } - public static function sign($data, $owner) + /** + * Signs given data by owner's signature + * + * @param array $data Data to sign + * @param array $owner Owner information, like URL + * @return array Merged array of $data and signature + */ + public static function sign(array $data, array $owner): array { $options = [ 'type' => 'RsaSignature2017', 'nonce' => Strings::getRandomHex(64), 'creator' => $owner['url'] . '#main-key', - 'created' => DateTimeFormat::utcNow(DateTimeFormat::ATOM) + 'created' => DateTimeFormat::utcNow(DateTimeFormat::ATOM), ]; $ohash = self::hash(self::signableOptions($options)); @@ -63,13 +101,25 @@ class LDSignature return array_merge($data, ['signature' => $options]); } - private static function signableData($data) + /** + * Removes element 'signature' from array + * + * @param array $data + * @return array With no element 'signature' + */ + private static function signableData(array $data): array { unset($data['signature']); return $data; } - private static function signableOptions($options) + /** + * Removes some elements and adds '@context' to it + * + * @param array $options + * @return array With some removed elements and added '@context' element + */ + private static function signableOptions(array $options): array { $newopts = ['@context' => 'https://w3id.org/identity/v1']; @@ -80,7 +130,13 @@ class LDSignature return array_merge($newopts, $options); } - private static function hash($obj) + /** + * Hashes normalized object + * + * @param ??? $obj + * @return string SHA256 hash + */ + private static function hash($obj): string { return hash('sha256', JsonLD::normalize($obj)); }