-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into call-dotenv-construct
- Loading branch information
Showing
14 changed files
with
641 additions
and
100 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
name: CI | ||
|
||
on: | ||
push: | ||
branches: [ master ] | ||
pull_request: | ||
branches: [ master ] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
strategy: | ||
matrix: | ||
php: [7.x, 8.x] | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Use PHP ${{ matrix.php }} | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: ${{ matrix.php }} | ||
tools: composer:v2 | ||
coverage: none | ||
- name: Setup problem matchers | ||
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" | ||
- name: Install latest dependencies | ||
uses: nick-invision/retry@v2 | ||
with: | ||
timeout_minutes: 5 | ||
max_attempts: 5 | ||
command: composer update --no-interaction --no-progress | ||
- run: vendor/bin/phpunit |
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
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,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" beStrictAboutTestsThatDoNotTestAnything="true" beStrictAboutOutputDuringTests="true" bootstrap="vendor/autoload.php" colors="true" convertDeprecationsToExceptions="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" failOnRisky="true" failOnWarning="false" processIsolation="false" stopOnError="false" stopOnFailure="false" verbose="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"> | ||
<coverage processUncoveredFiles="true"> | ||
<include> | ||
<directory suffix=".php">./src</directory> | ||
</include> | ||
</coverage> | ||
<testsuites> | ||
<testsuite name="PHP DotenvVault Test Suite"> | ||
<directory suffix="Test.php">./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
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,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DotenvVault\Decrypter; | ||
|
||
use Exception; | ||
|
||
final class Decrypter implements DecrypterInterface | ||
{ | ||
/** | ||
* Decrypt encrypted content into a string | ||
* | ||
* @param string $content | ||
* @param string $keyStr | ||
* | ||
* @throws \Exception | ||
* | ||
* @return string | ||
*/ | ||
public function decrypt(string $encrypted, string $keyStr) | ||
{ | ||
if ($encrypted === null || !is_string($encrypted) || strlen($encrypted) < 1) { | ||
$msg = 'MISSING_CIPHERTEXT: It must be a non-empty string'; | ||
throw new Exception($msg); | ||
} | ||
|
||
// grab last 64 to permit keys like vlt_64 or custom_64 | ||
$last64 = substr($keyStr, -64); | ||
|
||
// must be 64 characters long | ||
if (strlen($last64) !== 64) { | ||
$msg = 'INVALID_DOTENV_KEY: It must be 64 characters long (or more)'; | ||
throw new Exception($msg); | ||
} | ||
|
||
// check key length is good INVALID_DOTENV_KEY: It must be 64 characters long (or more) | ||
$key = hex2bin($last64); | ||
|
||
// base64 decode | ||
$decoded = base64_decode($encrypted, true); | ||
|
||
// determine cipher and pull out nonce and tag | ||
$ciphertext = substr($decoded, 12, -16); | ||
$nonce = substr($decoded, 0, 12); | ||
$tag = substr($decoded, -16); | ||
|
||
try { | ||
$plaintext = openssl_decrypt($ciphertext, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $nonce, $tag); | ||
|
||
if ($plaintext === false) { | ||
$msg = 'DECRYPTION_FAILED: Please check your DOTENV_KEY'; | ||
throw new Exception($msg); | ||
} else { | ||
return $plaintext; | ||
} | ||
} catch (ExceptionType $e) { | ||
$msg = 'DECRYPTION_FAILED: Please check your DOTENV_KEY'; | ||
throw new Exception($msg); | ||
} | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DotenvVault\Decrypter; | ||
|
||
interface DecrypterInterface | ||
{ | ||
/** | ||
* Decrypt encrypted content into a string | ||
* | ||
* @param string $content | ||
* @param string $keyStr | ||
* | ||
* @throws \Exception | ||
* | ||
* | ||
* @return string | ||
*/ | ||
public function decrypt(string $encrypted, string $keyStr); | ||
} |
Oops, something went wrong.