Made message signing in Cryptography use SHA512 as the message content for... #1
2 changed files with 332 additions and 0 deletions
|
@ -0,0 +1,171 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Socialbox\Objects\Database;
|
||||||
|
|
||||||
|
use DateMalformedStringException;
|
||||||
|
use DateTime;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use Socialbox\Enums\Status\EncryptionChannelMessageStatus;
|
||||||
|
use Socialbox\Enums\Types\EncryptionMessageRecipient;
|
||||||
|
use Socialbox\Interfaces\SerializableInterface;
|
||||||
|
|
||||||
|
class EncryptionChannelMessageRecord implements SerializableInterface
|
||||||
|
{
|
||||||
|
private string $uuid;
|
||||||
|
private string $channelUuid;
|
||||||
|
private EncryptionMessageRecipient $recipient;
|
||||||
|
private EncryptionChannelMessageStatus $status;
|
||||||
|
private string $hash;
|
||||||
|
private string $data;
|
||||||
|
private DateTime $timestamp;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs the Channel
|
||||||
|
*
|
||||||
|
* @param array $data
|
||||||
|
*/
|
||||||
|
public function __construct(array $data)
|
||||||
|
{
|
||||||
|
$this->uuid = $data['uuid'];
|
||||||
|
$this->channelUuid = $data['channel_uuid'];
|
||||||
|
$this->recipient = EncryptionMessageRecipient::from($data['recipient']);
|
||||||
|
$this->status = EncryptionChannelMessageStatus::from($data['status']);
|
||||||
|
$this->hash = $data['hash'];
|
||||||
|
$this->data = $data['data'];
|
||||||
|
|
||||||
|
if($data['timestamp'] instanceof DateTime)
|
||||||
|
{
|
||||||
|
$this->timestamp = $data['timestamp'];
|
||||||
|
}
|
||||||
|
elseif(is_int($data['timestamp']))
|
||||||
|
{
|
||||||
|
$this->timestamp = (new DateTime())->setTimestamp($data['timestamp']);
|
||||||
|
}
|
||||||
|
elseif(is_string($data['timestamp']))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
$this->timestamp = new DateTime($data['timestamp']);
|
||||||
|
}
|
||||||
|
catch (DateMalformedStringException $e)
|
||||||
|
{
|
||||||
|
throw new InvalidArgumentException('Invalid DateTime format for timestamp, got: ' . $data['timestamp'], $e->getCode(), $e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new InvalidArgumentException('Invalid timestamp type, got: ' . gettype($data['timestamp']));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the Unique Universal Identifier for the message
|
||||||
|
*
|
||||||
|
* @return string The Message's Unique Universal Identifier
|
||||||
|
*/
|
||||||
|
public function getUuid(): string
|
||||||
|
{
|
||||||
|
return $this->uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the Unique Universal Identifier of the channel that this message belongs to
|
||||||
|
*
|
||||||
|
* @return string The Channel's Unique Universal Identifier
|
||||||
|
*/
|
||||||
|
public function getChannelUuid(): string
|
||||||
|
{
|
||||||
|
return $this->channelUuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the recipient of the message
|
||||||
|
*
|
||||||
|
* @return EncryptionMessageRecipient The recipient of the message
|
||||||
|
*/
|
||||||
|
public function getRecipient(): EncryptionMessageRecipient
|
||||||
|
{
|
||||||
|
return $this->recipient;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the status of the message
|
||||||
|
*
|
||||||
|
* @return EncryptionChannelMessageStatus The status of the message
|
||||||
|
*/
|
||||||
|
public function getStatus(): EncryptionChannelMessageStatus
|
||||||
|
{
|
||||||
|
return $this->status;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**try {
|
||||||
|
switch($fieldName) {
|
||||||
|
case InformationFieldName::DISPLAY_NAME:
|
||||||
|
SessionManager::updateFlow($request->getSession(), [SessionFlags::SET_DISPLAY_NAME]);
|
||||||
|
break;
|
||||||
|
// Other cases...
|
||||||
|
}
|
||||||
|
} catch (Exception $e) {
|
||||||
|
try {
|
||||||
|
PeerInformationManager::deleteProperty($peer, $fieldName);
|
||||||
|
} catch (DatabaseOperationException $e) {
|
||||||
|
throw new StandardException('Failed to rollback the information field', StandardError::INTERNAL_SERVER_ERROR, $e);
|
||||||
|
}
|
||||||
|
if($e instanceof StandardException) {
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
throw new StandardException('Failed to update the session flow', StandardError::INTERNAL_SERVER_ERROR, $e);
|
||||||
|
}
|
||||||
|
* Returns the SHA512 hash of the decrypted content
|
||||||
|
*
|
||||||
|
* @return string The SHA512 hash of the decrypted content
|
||||||
|
*/
|
||||||
|
public function getHash(): string
|
||||||
|
{
|
||||||
|
return $this->hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the encrypted content of the message
|
||||||
|
*
|
||||||
|
* @return string The encrypted content of the message
|
||||||
|
*/
|
||||||
|
public function getData(): string
|
||||||
|
{
|
||||||
|
return $this->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the Timestamp for when this message was created
|
||||||
|
*
|
||||||
|
* @return DateTime The Timestamp for when the message was created
|
||||||
|
*/
|
||||||
|
public function getTimestamp(): DateTime
|
||||||
|
{
|
||||||
|
return $this->timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public static function fromArray(array $data): EncryptionChannelMessageRecord
|
||||||
|
{
|
||||||
|
return new self($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function toArray(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'uuid' => $this->uuid,
|
||||||
|
'channel_uuid' => $this->channelUuid,
|
||||||
|
'recipient' => $this->recipient->value,
|
||||||
|
'status' => $this->status->value,
|
||||||
|
'hash' => $this->hash,
|
||||||
|
'data' => $this->data,
|
||||||
|
'timestamp' => $this->timestamp->getTimestamp()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
161
src/Socialbox/Objects/Database/EncryptionChannelRecord.php
Normal file
161
src/Socialbox/Objects/Database/EncryptionChannelRecord.php
Normal file
|
@ -0,0 +1,161 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Socialbox\Objects\Database;
|
||||||
|
|
||||||
|
use DateMalformedStringException;
|
||||||
|
use DateTime;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use Socialbox\Enums\Status\EncryptionChannelStatus;
|
||||||
|
use Socialbox\Interfaces\SerializableInterface;
|
||||||
|
use Socialbox\Objects\PeerAddress;
|
||||||
|
|
||||||
|
class EncryptionChannelRecord implements SerializableInterface
|
||||||
|
{
|
||||||
|
private string $uuid;
|
||||||
|
private EncryptionChannelStatus $status;
|
||||||
|
private PeerAddress $callingPeerAddress;
|
||||||
|
private string $callingPublicEncryptionKey;
|
||||||
|
private PeerAddress $receivingPeerAddress;
|
||||||
|
private ?string $receivingPublicEncryptionKey;
|
||||||
|
private DateTime $created;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs the Encryption Channel Record from an array representation of the object, requires the following
|
||||||
|
* fields:
|
||||||
|
* - uuid
|
||||||
|
* - status
|
||||||
|
* - calling_peer_address
|
||||||
|
* - calling_public_encryption_key
|
||||||
|
* - receiving_peer_address
|
||||||
|
* - created
|
||||||
|
* The only optional field is `receiving_public_encryption_key`
|
||||||
|
*
|
||||||
|
* @param array $data
|
||||||
|
*/
|
||||||
|
public function __construct(array $data)
|
||||||
|
{
|
||||||
|
$this->uuid = $data['uuid'];
|
||||||
|
$this->status = EncryptionChannelStatus::from($data['status']);
|
||||||
|
$this->callingPeerAddress = PeerAddress::fromAddress($data['calling_peer_address']);
|
||||||
|
$this->callingPublicEncryptionKey = $data['calling_public_encryption_key'];
|
||||||
|
$this->receivingPeerAddress = PeerAddress::fromAddress($data['receiving_peer_address']);
|
||||||
|
$this->receivingPublicEncryptionKey = $data['receiving_public_encryption_key'] ?? null;
|
||||||
|
|
||||||
|
if($data['created'] instanceof DateTime)
|
||||||
|
{
|
||||||
|
$this->created = $data['created'];
|
||||||
|
}
|
||||||
|
elseif(is_int($data['created']))
|
||||||
|
{
|
||||||
|
$this->created = (new DateTime())->setTimestamp($data['created']);
|
||||||
|
}
|
||||||
|
elseif(is_string($data['created']))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
$this->created = new DateTime($data['created']);
|
||||||
|
}
|
||||||
|
catch (DateMalformedStringException $e)
|
||||||
|
{
|
||||||
|
throw new InvalidArgumentException('Invalid DateTime given in created, got: ' . $data['created'], $e->getCode(), $e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new InvalidArgumentException('Invalid created type, got: ' . gettype($data['created']));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the Universal Unique Identifier of the encryption channel record
|
||||||
|
*
|
||||||
|
* @return string The UUID V4
|
||||||
|
*/
|
||||||
|
public function getUuid(): string
|
||||||
|
{
|
||||||
|
return $this->uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the current status of the encryption channel record
|
||||||
|
*
|
||||||
|
* @return EncryptionChannelStatus The current status of the encryption channel record
|
||||||
|
*/
|
||||||
|
public function getStatus(): EncryptionChannelStatus
|
||||||
|
{
|
||||||
|
return $this->status;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the PeerAddress of the calling peer for the encryption channel record
|
||||||
|
*
|
||||||
|
* @return PeerAddress The address of the calling peer
|
||||||
|
*/
|
||||||
|
public function getCallingPeerAddress(): PeerAddress
|
||||||
|
{
|
||||||
|
return $this->callingPeerAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the public encryption key of the calling peer
|
||||||
|
*
|
||||||
|
* @return string The public encryption key of the caller
|
||||||
|
*/
|
||||||
|
public function getCallingPublicEncryptionKey(): string
|
||||||
|
{
|
||||||
|
return $this->callingPublicEncryptionKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the PeerAddress of the receiving peer for the encryption channel record
|
||||||
|
*
|
||||||
|
* @return PeerAddress
|
||||||
|
*/
|
||||||
|
public function getReceivingPeerAddress(): PeerAddress
|
||||||
|
{
|
||||||
|
return $this->receivingPeerAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the public encryption key of the receiving peer
|
||||||
|
*
|
||||||
|
* @return string|null The public encryption key of the receiver
|
||||||
|
*/
|
||||||
|
public function getReceivingPublicEncryptionKey(): ?string
|
||||||
|
{
|
||||||
|
return $this->receivingPublicEncryptionKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The DateTime object of when the record was created
|
||||||
|
*
|
||||||
|
* @return DateTime The DateTime object of the record's creation date
|
||||||
|
*/
|
||||||
|
public function getCreated(): DateTime
|
||||||
|
{
|
||||||
|
return $this->created;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public static function fromArray(array $data): EncryptionChannelRecord
|
||||||
|
{
|
||||||
|
return new self($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function toArray(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'uuid' => $this->uuid,
|
||||||
|
'status' => $this->status->value,
|
||||||
|
'calling_peer_address' => $this->callingPeerAddress->getAddress(),
|
||||||
|
'calling_public_encryption_key' => $this->callingPublicEncryptionKey,
|
||||||
|
'receiving_peer_address' => $this->receivingPeerAddress->getAddress(),
|
||||||
|
'created' => $this->created->getTimestamp()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue