-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f4df134
commit b2d56fb
Showing
8 changed files
with
141 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Phore\ObjectStore\Encryption; | ||
|
||
interface ObjectStoreEncryption | ||
{ | ||
|
||
public function encrypt(string $data) : string; | ||
|
||
public function decrypt(string $data) : string; | ||
|
||
|
||
public function supportsStreaming() : bool; | ||
public function supportsAppending() : bool; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Phore\ObjectStore\Encryption; | ||
|
||
class PassThruNoEncryption implements ObjectStoreEncryption | ||
{ | ||
public function encrypt(string $data): string | ||
{ | ||
return $data; | ||
} | ||
|
||
public function decrypt(string $data): string | ||
{ | ||
return $data; | ||
} | ||
|
||
public function supportsAppending(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function supportsStreaming(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Phore\ObjectStore\Encryption; | ||
|
||
class SodiumSyncEncryption implements ObjectStoreEncryption | ||
{ | ||
private $key; | ||
|
||
public function __construct(string $key) | ||
{ | ||
if (strlen($key) !== SODIUM_CRYPTO_SECRETBOX_KEYBYTES) { | ||
throw new \InvalidArgumentException("Key must be exactly " . SODIUM_CRYPTO_SECRETBOX_KEYBYTES . " bytes long"); | ||
} | ||
$this->key = $key; | ||
} | ||
|
||
public function encrypt(string $data): string | ||
{ | ||
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); | ||
$encrypted = sodium_crypto_secretbox($data, $nonce, $this->key); | ||
return $nonce . $encrypted; | ||
} | ||
|
||
public function decrypt(string $data): string | ||
{ | ||
$nonce = mb_substr($data, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, "8bit"); | ||
$encrypted = mb_substr($data, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, "8bit"); | ||
$decrypted = sodium_crypto_secretbox_open($encrypted, $nonce, $this->key); | ||
if ($decrypted === false) { | ||
throw new \InvalidArgumentException("Could not decrypt data"); | ||
} | ||
return $decrypted; | ||
} | ||
|
||
public function supportsStreaming(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function supportsAppending(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters