Skip to content

Commit

Permalink
Use laravel default logging
Browse files Browse the repository at this point in the history
  • Loading branch information
thelfensdrfer committed Dec 11, 2018
1 parent b999079 commit 5261014
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 64 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"php": ">= 7.2",
"ext-sodium": "*",
"illuminate/database": ">= 5.7",
"illuminate/encryption" : ">= 5.7",
"illuminate/support": ">= 5.7",
"monolog/monolog": ">= 1"
},
Expand Down
66 changes: 2 additions & 64 deletions src/DatabaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace VisualAppeal\DatabaseLogger;

use Illuminate\Container\Container;
use Illuminate\Support\Facades\DB;

use Monolog\Handler\AbstractProcessingHandler;
Expand Down Expand Up @@ -88,69 +89,6 @@ public function setEncryptionKey(string $encryptionKey): DatabaseHandler
return $this;
}

/**
* Encrypt the context.
*
* @param mixed $data
* @return string
* @throws \Exception
*/
public function encrypt($data): string
{
$nonce = random_bytes(
SODIUM_CRYPTO_SECRETBOX_NONCEBYTES
);

$cipher = base64_encode(
$nonce.
sodium_crypto_secretbox(
$data,
$nonce,
$this->encryptionKey
)
);
sodium_memzero($data);
sodium_memzero($this->encryptionKey);

return $cipher;
}

/**
* Decrypt the context.
*
* @param $data
* @return bool|string
* @throws \Exception
*/
public function decrypt($data)
{
$decoded = base64_decode($data);

if ($decoded === false) {
throw new \Exception('The encoding failed');
}
if (mb_strlen($decoded, '8bit') < (SODIUM_CRYPTO_SECRETBOX_NONCEBYTES + SODIUM_CRYPTO_SECRETBOX_MACBYTES)) {
throw new \Exception('The message was truncated');
}
$nonce = mb_substr($decoded, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, '8bit');
$ciphertext = mb_substr($decoded, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit');

$plain = sodium_crypto_secretbox_open(
$ciphertext,
$nonce,
$this->encryptionKey
);

if ($plain === false) {
throw new \Exception('The message was tampered with in transit');
}

sodium_memzero($ciphertext);
sodium_memzero($this->encryptionKey);

return $plain;
}

/**
* {@inheritdoc}
*/
Expand All @@ -161,7 +99,7 @@ protected function write(array $record)
} else {
try {
if ($this->encryption) {
$context = $this->encrypt($record['context']);
$context = Container::getInstance()->make('encrypter')->encrypt($record['context']);
} else {
$context = serialize($record['context']);
}
Expand Down

0 comments on commit 5261014

Please sign in to comment.