Skip to content

Commit

Permalink
Add toString method to SignedMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Dec 20, 2021
1 parent 4052eed commit 026f105
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions lib/Protocol/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,40 @@ public function signatureValidForPublicKey($publicKey)
Util::rawBinary($publicKey, 32)
);
}

/**
* @param bool $pretty
* @return string
* @throws SodiumException
*/
public function toString($pretty = false)
{
$code = $pretty ? JSON_PRETTY_PRINT : 0;
if ($this->signature) {
$signature = sodium_bin2base64(
Util::rawBinary($this->getSignature(), 64),
SODIUM_BASE64_VARIANT_URLSAFE
);
} else {
$signature = '';
}
return (string) json_encode(array(
'signature' => $signature,
'message' => $this->getContents(),
), $code);
}

/**
* @return string
*/
public function __toString()
{
try {
return $this->toString();
} catch (\Error $ex) {
return '';
} catch (\Exception $ex) {
return '';
}
}
}

0 comments on commit 026f105

Please sign in to comment.