Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fec1c5ad57 | |||
| a6e5e86c9e | |||
| 1357b98883 | |||
| fa18a6f422 | |||
| ada130329f | |||
| 4c489adf9b | |||
| c8a2ad4d48 | |||
| d54a6cbdf5 | |||
| e8f4c82f59 | |||
| 24eb2c2bee | |||
| 94edb7f556 | |||
| 3f5b3f02ad | |||
| a270922b4b | |||
| 8c607ae610 | |||
| d3d4cf512f | |||
| efae352c35 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
vendor
|
vendor/
|
||||||
composer.lock
|
composer.lock
|
||||||
|
tools/
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<phive xmlns="https://phar.io/phive">
|
<phive xmlns="https://phar.io/phive">
|
||||||
<phar name="phpunit" version="^9.6" installed="9.6.13" location="./tools/phpunit" copy="false"/>
|
<phar name="phpunit" version="^9.6" installed="9.6.19" location="./tools/phpunit" copy="false"/>
|
||||||
<phar name="phpcs" version="^3.7.2" installed="3.7.2" location="./tools/phpcs" copy="false"/>
|
<phar name="phpcs" version="^3.7.2" installed="3.10.0" location="./tools/phpcs" copy="false"/>
|
||||||
<phar name="phpcbf" version="^3.7.2" installed="3.7.2" location="./tools/phpcbf" copy="false"/>
|
<phar name="phpcbf" version="^3.7.2" installed="3.10.0" location="./tools/phpcbf" copy="false"/>
|
||||||
<phar name="psalm" version="^5.15.0" installed="5.16.0" location="./tools/psalm" copy="false"/>
|
<phar name="psalm" version="^5.15.0" installed="5.24.0" location="./tools/psalm" copy="false"/>
|
||||||
<phar name="phpstan" version="^1.10.37" installed="1.10.46" location="./tools/phpstan" copy="false"/>
|
<phar name="phpstan" version="^1.10.37" installed="1.11.1" location="./tools/phpstan" copy="false"/>
|
||||||
<phar name="phan" version="^5.4.2" installed="5.4.2" location="./tools/phan" copy="false"/>
|
<phar name="phan" version="^5.4.2" installed="5.4.3" location="./tools/phan" copy="false"/>
|
||||||
</phive>
|
</phive>
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ Alternative setup composer local zip file repot:
|
|||||||
|
|
||||||
## Install package
|
## Install package
|
||||||
|
|
||||||
`composer require egrajp/corelibs-composer-all:^8.0`
|
`composer require egrajp/corelibs-composer-all:^9.0`
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
|
|||||||
18
phpcs.xml
Normal file
18
phpcs.xml
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<?xml version="1.0"?>
|
||||||
|
<ruleset name="MyStandard">
|
||||||
|
<description>PSR12 override rules (strict, standard). Switch spaces indent to tab.</description>
|
||||||
|
<arg name="tab-width" value="4"/>
|
||||||
|
<rule ref="PSR1"/>
|
||||||
|
<rule ref="PSR12">
|
||||||
|
<!-- turn off white space check for tab -->
|
||||||
|
<exclude name="Generic.WhiteSpace.DisallowTabIndent"/>
|
||||||
|
</rule>
|
||||||
|
<!-- no space indent, must be tab, 4 is tab iwdth -->
|
||||||
|
<rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/>
|
||||||
|
<rule ref="Generic.WhiteSpace.ScopeIndent">
|
||||||
|
<properties>
|
||||||
|
<property name="indent" value="4"/>
|
||||||
|
<property name="tabIndent" value="true"/>
|
||||||
|
</properties>
|
||||||
|
</rule>
|
||||||
|
</ruleset>
|
||||||
@@ -1 +1 @@
|
|||||||
9.11.0
|
9.12.2
|
||||||
|
|||||||
@@ -53,13 +53,19 @@ echo "[START]";
|
|||||||
if [ ! -z "${GITEA_UPLOAD_FILENAME}" ] &&
|
if [ ! -z "${GITEA_UPLOAD_FILENAME}" ] &&
|
||||||
[ ! -z "${GITEA_URL_DL}" ] && [ ! -z "${GITEA_URL_PUSH}" ] &&
|
[ ! -z "${GITEA_URL_DL}" ] && [ ! -z "${GITEA_URL_PUSH}" ] &&
|
||||||
[ ! -z "${GITEA_USER}" ] && [ ! -z "${GITEA_TOKEN}" ]; then
|
[ ! -z "${GITEA_USER}" ] && [ ! -z "${GITEA_TOKEN}" ]; then
|
||||||
curl -LJO \
|
if [ ! -f "${PACKAGE_DOWNLOAD}${GITEA_UPLOAD_FILENAME}-v${VERSION}.zip" ]; then
|
||||||
--output-dir "${PACKAGE_DOWNLOAD}" \
|
curl -LJO \
|
||||||
${GITEA_URL_DL}/v${VERSION}.zip;
|
--output-dir "${PACKAGE_DOWNLOAD}" \
|
||||||
curl --user ${GITEA_USER}:${GITEA_TOKEN} \
|
${GITEA_URL_DL}/v${VERSION}.zip;
|
||||||
--upload-file "${PACKAGE_DOWNLOAD}${GITEA_UPLOAD_FILENAME}-v${VERSION}.zip" \
|
fi;
|
||||||
${GITEA_URL_PUSH}?version=${VERSION};
|
if [ ! -f "${PACKAGE_DOWNLOAD}${GITEA_UPLOAD_FILENAME}-v${VERSION}.zip" ]; then
|
||||||
echo "${VERSION}" > "${file_last_published}";
|
echo "Version file does not exist for ${VERSION}";
|
||||||
|
else
|
||||||
|
curl --user ${GITEA_USER}:${GITEA_TOKEN} \
|
||||||
|
--upload-file "${PACKAGE_DOWNLOAD}${GITEA_UPLOAD_FILENAME}-v${VERSION}.zip" \
|
||||||
|
${GITEA_URL_PUSH}?version=${VERSION};
|
||||||
|
echo "${VERSION}" > "${file_last_published}";
|
||||||
|
fi;
|
||||||
else
|
else
|
||||||
echo "Missing either GITEA_UPLOAD_FILENAME, GITEA_URL_DL, GITEA_URL_PUSH, GITEA_USER or GITEA_TOKEN environment variable";
|
echo "Missing either GITEA_UPLOAD_FILENAME, GITEA_URL_DL, GITEA_URL_PUSH, GITEA_USER or GITEA_TOKEN environment variable";
|
||||||
fi;
|
fi;
|
||||||
|
|||||||
@@ -51,6 +51,23 @@ class File
|
|||||||
// return lines in file
|
// return lines in file
|
||||||
return $lines;
|
return $lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get the mime type of a file via finfo
|
||||||
|
* if file not found, throws exception
|
||||||
|
* else returns '' for any other finfo read problem
|
||||||
|
*
|
||||||
|
* @param string $read_file File to read, relative or absolute path
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function getMimeType(string $read_file): string
|
||||||
|
{
|
||||||
|
$finfo = new \finfo(FILEINFO_MIME_TYPE);
|
||||||
|
if (!is_file($read_file)) {
|
||||||
|
throw new \UnexpectedValueException('[getMimeType] File not found: ' . $read_file);
|
||||||
|
}
|
||||||
|
return $finfo->file($read_file) ?: '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// __END__
|
// __END__
|
||||||
|
|||||||
@@ -675,9 +675,9 @@ class DateTime
|
|||||||
foreach ($period as $dt) {
|
foreach ($period as $dt) {
|
||||||
$curr = $dt->format('D');
|
$curr = $dt->format('D');
|
||||||
if ($curr == 'Sat' || $curr == 'Sun') {
|
if ($curr == 'Sat' || $curr == 'Sun') {
|
||||||
$days[2] ++;
|
$days[2]++;
|
||||||
} else {
|
} else {
|
||||||
$days[1] ++;
|
$days[1]++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($return_named === true) {
|
if ($return_named === true) {
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ class MimeEncode
|
|||||||
/**
|
/**
|
||||||
* wrapper function for mb mime convert
|
* wrapper function for mb mime convert
|
||||||
* for correct conversion with long strings
|
* for correct conversion with long strings
|
||||||
|
* NOTE: This is only a wrapper for mb_encode_mimeheader to stay compatible
|
||||||
*
|
*
|
||||||
* @param string $string string to encode
|
* @param string $string string to encode
|
||||||
* @param string $encoding target encoding
|
* @param string $encoding target encoding
|
||||||
@@ -29,38 +30,9 @@ class MimeEncode
|
|||||||
$current_internal_encoding = mb_internal_encoding();
|
$current_internal_encoding = mb_internal_encoding();
|
||||||
// set internal encoding, so the mimeheader encode works correctly
|
// set internal encoding, so the mimeheader encode works correctly
|
||||||
mb_internal_encoding($encoding);
|
mb_internal_encoding($encoding);
|
||||||
// if a subject, make a work around for the broken mb_mimencode
|
// use the internal convert to mime header
|
||||||
$pos = 0;
|
// it works from PHP 8.2 on
|
||||||
// after 36 single bytes characters,
|
$string = mb_encode_mimeheader($string, $encoding, 'B', $line_break);
|
||||||
// if then comes MB, it is broken
|
|
||||||
// has to 2 x 36 < 74 so the mb_encode_mimeheader
|
|
||||||
// 74 hardcoded split does not get triggered
|
|
||||||
$split = 36;
|
|
||||||
$_string = '';
|
|
||||||
while ($pos < mb_strlen($string, $encoding)) {
|
|
||||||
$output = mb_strimwidth($string, $pos, $split, "", $encoding);
|
|
||||||
$pos += mb_strlen($output, $encoding);
|
|
||||||
// if the strinlen is 0 here, get out of the loop
|
|
||||||
if (!mb_strlen($output, $encoding)) {
|
|
||||||
$pos += mb_strlen($string, $encoding);
|
|
||||||
}
|
|
||||||
$_string_encoded = mb_encode_mimeheader($output, $encoding);
|
|
||||||
// only make linebreaks if we have mime encoded code inside
|
|
||||||
// the space only belongs in the second line
|
|
||||||
if ($_string && preg_match("/^=\?/", $_string_encoded)) {
|
|
||||||
$_string .= $line_break . " ";
|
|
||||||
} elseif (
|
|
||||||
// hack for plain text with space at the end
|
|
||||||
mb_strlen($output, $encoding) == $split &&
|
|
||||||
mb_substr($output, -1, 1, $encoding) == " "
|
|
||||||
) {
|
|
||||||
// if output ends with space, add one more
|
|
||||||
$_string_encoded .= " ";
|
|
||||||
}
|
|
||||||
$_string .= $_string_encoded;
|
|
||||||
}
|
|
||||||
// strip out any spaces BEFORE a line break
|
|
||||||
$string = str_replace(" " . $line_break, $line_break, $_string);
|
|
||||||
// before we end, reset internal encoding
|
// before we end, reset internal encoding
|
||||||
mb_internal_encoding($current_internal_encoding);
|
mb_internal_encoding($current_internal_encoding);
|
||||||
// return mime encoded string
|
// return mime encoded string
|
||||||
|
|||||||
@@ -118,6 +118,22 @@ class Strings
|
|||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Strip any duplicated slahes from a path
|
||||||
|
* eg: //foo///bar/foo.inc -> /foo/bar/foo.inc
|
||||||
|
*
|
||||||
|
* @param string $path Path to strip slashes from
|
||||||
|
* @return string Clean path, on error returns original path
|
||||||
|
*/
|
||||||
|
public static function stripMultiplePathSlashes(string $path): string
|
||||||
|
{
|
||||||
|
return preg_replace(
|
||||||
|
'#/+#',
|
||||||
|
'/',
|
||||||
|
$path
|
||||||
|
) ?? $path;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// __END__
|
// __END__
|
||||||
|
|||||||
@@ -236,7 +236,7 @@ class ArrayIO extends \CoreLibs\DB\IO
|
|||||||
*/
|
*/
|
||||||
public function getPkId(): int|string|null
|
public function getPkId(): int|string|null
|
||||||
{
|
{
|
||||||
return $this->pk_id;
|
return $this->pk_id ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1302,7 +1302,7 @@ class IO
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->cursor_ext[$query_hash]['pos'] ++;
|
$this->cursor_ext[$query_hash]['pos']++;
|
||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1519,7 +1519,7 @@ class IO
|
|||||||
]);
|
]);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$this->query_called[$query_hash] ++;
|
$this->query_called[$query_hash]++;
|
||||||
// return hash
|
// return hash
|
||||||
return $query_hash;
|
return $query_hash;
|
||||||
}
|
}
|
||||||
@@ -2469,7 +2469,7 @@ class IO
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$this->cursor_ext[$query_hash]['log_pos'] ++;
|
$this->cursor_ext[$query_hash]['log_pos']++;
|
||||||
}
|
}
|
||||||
// reset log for each read
|
// reset log for each read
|
||||||
$this->cursor_ext[$query_hash]['log'] = [];
|
$this->cursor_ext[$query_hash]['log'] = [];
|
||||||
@@ -2668,8 +2668,8 @@ class IO
|
|||||||
if ($return) {
|
if ($return) {
|
||||||
$this->cursor_ext[$query_hash]['log'][] = 'Return Data';
|
$this->cursor_ext[$query_hash]['log'][] = 'Return Data';
|
||||||
// internal position counter
|
// internal position counter
|
||||||
$this->cursor_ext[$query_hash]['pos'] ++;
|
$this->cursor_ext[$query_hash]['pos']++;
|
||||||
$this->cursor_ext[$query_hash]['read_rows'] ++;
|
$this->cursor_ext[$query_hash]['read_rows']++;
|
||||||
// read is finished
|
// read is finished
|
||||||
if (
|
if (
|
||||||
$this->cursor_ext[$query_hash]['read_rows'] ==
|
$this->cursor_ext[$query_hash]['read_rows'] ==
|
||||||
|
|||||||
@@ -826,27 +826,28 @@ class Generate
|
|||||||
$pk_selected = $res[$this->int_pk_name];
|
$pk_selected = $res[$this->int_pk_name];
|
||||||
}
|
}
|
||||||
$t_string = '';
|
$t_string = '';
|
||||||
foreach ($this->field_array as $i => $field_array) {
|
foreach ($this->field_array as $field_array) {
|
||||||
if ($t_string) {
|
if ($t_string) {
|
||||||
$t_string .= ', ';
|
$t_string .= ', ';
|
||||||
}
|
}
|
||||||
if (isset($field_array['before_value'])) {
|
if (!empty($field_array['before_value'])) {
|
||||||
$t_string .= $field_array['before_value'];
|
$t_string .= $this->l->__($field_array['before_value']);
|
||||||
}
|
}
|
||||||
// must have res element set
|
// must have res element set
|
||||||
if (
|
if (
|
||||||
isset($field_array['name']) &&
|
!empty($field_array['name']) &&
|
||||||
isset($res[$field_array['name']])
|
isset($res[$field_array['name']])
|
||||||
) {
|
) {
|
||||||
if (isset($field_array['binary'])) {
|
$_t_value = '';
|
||||||
if (isset($field_array['binary'][0])) {
|
// if we have a binary set, where 0 = YES and 1 = NO
|
||||||
$t_string .= $field_array['binary'][0];
|
if (!empty($field_array['binary'])) {
|
||||||
} elseif (isset($field_array['binary'][1])) {
|
$_t_value = !empty($res[$field_array['name']]) ?
|
||||||
$t_string .= $field_array['binary'][1];
|
($field_array['binary'][0] ?? 'Yes') :
|
||||||
}
|
($field_array['binary'][1] ?? 'No');
|
||||||
} else {
|
} else {
|
||||||
$t_string .= $res[$field_array['name']];
|
$_t_value = $res[$field_array['name']];
|
||||||
}
|
}
|
||||||
|
$t_string .= $this->l->__($_t_value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$pk_names[] = $t_string;
|
$pk_names[] = $t_string;
|
||||||
@@ -1506,7 +1507,7 @@ class Generate
|
|||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
!empty($this->reference_array[$key]['mandatory']) &&
|
!empty($this->reference_array[$key]['mandatory']) &&
|
||||||
!$this->reference_array[$key]['selected'][0]
|
empty($this->reference_array[$key]['selected'][0])
|
||||||
) {
|
) {
|
||||||
$this->msg .= sprintf(
|
$this->msg .= sprintf(
|
||||||
$this->l->__('Please select at least one Element from field <b>%s</b>!<br>'),
|
$this->l->__('Please select at least one Element from field <b>%s</b>!<br>'),
|
||||||
|
|||||||
@@ -53,6 +53,7 @@ class EditPages implements Interface\TableArraysInterface
|
|||||||
'value' => $_POST['name'] ?? '',
|
'value' => $_POST['name'] ?? '',
|
||||||
'output_name' => 'Page name',
|
'output_name' => 'Page name',
|
||||||
'mandatory' => 1,
|
'mandatory' => 1,
|
||||||
|
'error_check' => 'unique',
|
||||||
'type' => 'text'
|
'type' => 'text'
|
||||||
],
|
],
|
||||||
'order_number' => [
|
'order_number' => [
|
||||||
|
|||||||
@@ -204,11 +204,11 @@ class Image
|
|||||||
E_USER_DEPRECATED
|
E_USER_DEPRECATED
|
||||||
);
|
);
|
||||||
// NOTE: we need to depracte this
|
// NOTE: we need to depracte this
|
||||||
$cache_folder = BASE . LAYOUT . CONTENT_PATH . CACHE . IMAGES;
|
$cache_folder = BASE . CONTENT_PATH . LAYOUT . CACHE . IMAGES;
|
||||||
$web_folder = LAYOUT . CACHE . IMAGES;
|
$web_folder = LAYOUT . CACHE . IMAGES;
|
||||||
if (!is_dir($cache_folder)) {
|
if (!is_dir($cache_folder)) {
|
||||||
if (false === mkdir($cache_folder)) {
|
if (false === mkdir($cache_folder)) {
|
||||||
$cache_folder = BASE . LAYOUT . CONTENT_PATH . CACHE;
|
$cache_folder = BASE . CONTENT_PATH . LAYOUT . CACHE;
|
||||||
$web_folder = LAYOUT . CACHE;
|
$web_folder = LAYOUT . CACHE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -156,7 +156,7 @@ class ProgressBar
|
|||||||
{
|
{
|
||||||
// avoid divison through 0
|
// avoid divison through 0
|
||||||
if ($this->max - $this->min == 0) {
|
if ($this->max - $this->min == 0) {
|
||||||
$this->max ++;
|
$this->max++;
|
||||||
}
|
}
|
||||||
$percent = round(($step - $this->min) / ($this->max - $this->min) * 100);
|
$percent = round(($step - $this->min) / ($this->max - $this->min) * 100);
|
||||||
if ($percent > 100) {
|
if ($percent > 100) {
|
||||||
@@ -186,7 +186,7 @@ class ProgressBar
|
|||||||
}
|
}
|
||||||
// avoid divison through 0
|
// avoid divison through 0
|
||||||
if ($this->max - $this->min == 0) {
|
if ($this->max - $this->min == 0) {
|
||||||
$this->max ++;
|
$this->max++;
|
||||||
}
|
}
|
||||||
$pixel = round(($step - $this->min) * ($bar - ($this->pedding * 2)) / ($this->max - $this->min));
|
$pixel = round(($step - $this->min) * ($bar - ($this->pedding * 2)) / ($this->max - $this->min));
|
||||||
if ($step <= $this->min) {
|
if ($step <= $this->min) {
|
||||||
|
|||||||
@@ -21,58 +21,82 @@ use SodiumException;
|
|||||||
|
|
||||||
class SymmetricEncryption
|
class SymmetricEncryption
|
||||||
{
|
{
|
||||||
|
/** @var SymmetricEncryption self instance */
|
||||||
|
private static SymmetricEncryption $instance;
|
||||||
|
|
||||||
|
/** @var string bin hex key */
|
||||||
|
private string $key = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encrypt a message
|
* init class
|
||||||
|
* if key not passed, key must be set with createKey
|
||||||
*
|
*
|
||||||
* @param string $message Message to encrypt
|
* @param string|null|null $key
|
||||||
* @param string $key Encryption key (as hex string)
|
|
||||||
* @return string
|
|
||||||
* @throws \Exception
|
|
||||||
* @throws \RangeException
|
|
||||||
*/
|
*/
|
||||||
public static function encrypt(string $message, string $key): string
|
public function __construct(
|
||||||
|
string|null $key = null
|
||||||
|
) {
|
||||||
|
if ($key != null) {
|
||||||
|
$this->setKey($key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the singleton self object.
|
||||||
|
* For function wrapper use
|
||||||
|
*
|
||||||
|
* @return SymmetricEncryption object
|
||||||
|
*/
|
||||||
|
public static function getInstance(string|null $key = null): self
|
||||||
|
{
|
||||||
|
if (empty(self::$instance)) {
|
||||||
|
self::$instance = new self($key);
|
||||||
|
}
|
||||||
|
return self::$instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ************************************************************************
|
||||||
|
* MARK: PRIVATE
|
||||||
|
* *************************************************************************/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* create key and check validity
|
||||||
|
*
|
||||||
|
* @param string $key The key from which the binary key will be created
|
||||||
|
* @return string Binary key string
|
||||||
|
*/
|
||||||
|
private function createKey(string $key): string
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$key = CreateKey::hex2bin($key);
|
$key = CreateKey::hex2bin($key);
|
||||||
} catch (SodiumException $e) {
|
} catch (SodiumException $e) {
|
||||||
throw new \UnexpectedValueException('Invalid hex key');
|
throw new \UnexpectedValueException('Invalid hex key: ' . $e->getMessage());
|
||||||
}
|
}
|
||||||
if (mb_strlen($key, '8bit') !== SODIUM_CRYPTO_SECRETBOX_KEYBYTES) {
|
if (mb_strlen($key, '8bit') !== SODIUM_CRYPTO_SECRETBOX_KEYBYTES) {
|
||||||
throw new \RangeException(
|
throw new \RangeException(
|
||||||
'Key is not the correct size (must be '
|
'Key is not the correct size (must be '
|
||||||
. 'SODIUM_CRYPTO_SECRETBOX_KEYBYTES bytes long).'
|
. SODIUM_CRYPTO_SECRETBOX_KEYBYTES . ' bytes long).'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
|
return $key;
|
||||||
|
|
||||||
$cipher = base64_encode(
|
|
||||||
$nonce
|
|
||||||
. sodium_crypto_secretbox(
|
|
||||||
$message,
|
|
||||||
$nonce,
|
|
||||||
$key
|
|
||||||
)
|
|
||||||
);
|
|
||||||
sodium_memzero($message);
|
|
||||||
sodium_memzero($key);
|
|
||||||
return $cipher;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decrypt a message
|
* Decryption call
|
||||||
*
|
*
|
||||||
* @param string $encrypted Message encrypted with safeEncrypt()
|
* @param string $encrypted Text to decrypt
|
||||||
* @param string $key Encryption key (as hex string)
|
* @param ?string $key Mandatory encryption key, will throw exception if empty
|
||||||
* @return string
|
* @return string Plain text
|
||||||
* @throws \Exception
|
* @throws \RangeException
|
||||||
|
* @throws \UnexpectedValueException
|
||||||
|
* @throws \UnexpectedValueException
|
||||||
*/
|
*/
|
||||||
public static function decrypt(string $encrypted, string $key): string
|
private function decryptData(string $encrypted, ?string $key): string
|
||||||
{
|
{
|
||||||
try {
|
if (empty($key)) {
|
||||||
$key = CreateKey::hex2bin($key);
|
throw new \UnexpectedValueException('Key not set');
|
||||||
} catch (SodiumException $e) {
|
|
||||||
throw new \Exception('Invalid hex key');
|
|
||||||
}
|
}
|
||||||
|
$key = $this->createKey($key);
|
||||||
$decoded = base64_decode($encrypted);
|
$decoded = base64_decode($encrypted);
|
||||||
$nonce = mb_substr($decoded, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, '8bit');
|
$nonce = mb_substr($decoded, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, '8bit');
|
||||||
$ciphertext = mb_substr($decoded, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit');
|
$ciphertext = mb_substr($decoded, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit');
|
||||||
@@ -85,7 +109,7 @@ class SymmetricEncryption
|
|||||||
$key
|
$key
|
||||||
);
|
);
|
||||||
} catch (SodiumException $e) {
|
} catch (SodiumException $e) {
|
||||||
throw new \UnexpectedValueException('Invalid ciphertext (too short)');
|
throw new \UnexpectedValueException('Decipher message failed: ' . $e->getMessage());
|
||||||
}
|
}
|
||||||
if (!is_string($plain)) {
|
if (!is_string($plain)) {
|
||||||
throw new \UnexpectedValueException('Invalid Key');
|
throw new \UnexpectedValueException('Invalid Key');
|
||||||
@@ -94,6 +118,117 @@ class SymmetricEncryption
|
|||||||
sodium_memzero($key);
|
sodium_memzero($key);
|
||||||
return $plain;
|
return $plain;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encrypt a message
|
||||||
|
*
|
||||||
|
* @param string $message Message to encrypt
|
||||||
|
* @param ?string $key Mandatory encryption key, will throw exception if empty
|
||||||
|
* @return string
|
||||||
|
* @throws \Exception
|
||||||
|
* @throws \RangeException
|
||||||
|
*/
|
||||||
|
private function encryptData(string $message, ?string $key): string
|
||||||
|
{
|
||||||
|
if (empty($this->key) || $key === null) {
|
||||||
|
throw new \UnexpectedValueException('Key not set');
|
||||||
|
}
|
||||||
|
$key = $this->createKey($key);
|
||||||
|
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
|
||||||
|
try {
|
||||||
|
$cipher = base64_encode(
|
||||||
|
$nonce
|
||||||
|
. sodium_crypto_secretbox(
|
||||||
|
$message,
|
||||||
|
$nonce,
|
||||||
|
$key,
|
||||||
|
)
|
||||||
|
);
|
||||||
|
} catch (SodiumException $e) {
|
||||||
|
throw new \UnexpectedValueException("Create encrypted message failed: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
sodium_memzero($message);
|
||||||
|
sodium_memzero($key);
|
||||||
|
return $cipher;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ************************************************************************
|
||||||
|
* MARK: PUBLIC
|
||||||
|
* *************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set a new key for encryption
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function setKey(string $key)
|
||||||
|
{
|
||||||
|
if (empty($key)) {
|
||||||
|
throw new \UnexpectedValueException('Key cannot be empty');
|
||||||
|
}
|
||||||
|
$this->key = $key;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decrypt a message
|
||||||
|
* static version
|
||||||
|
*
|
||||||
|
* @param string $encrypted Message encrypted with safeEncrypt()
|
||||||
|
* @param string $key Encryption key (as hex string)
|
||||||
|
* @return string
|
||||||
|
* @throws \Exception
|
||||||
|
* @throws \RangeException
|
||||||
|
* @throws \UnexpectedValueException
|
||||||
|
* @throws \UnexpectedValueException
|
||||||
|
*/
|
||||||
|
public static function decryptKey(string $encrypted, string $key): string
|
||||||
|
{
|
||||||
|
return self::getInstance()->decryptData($encrypted, $key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decrypt a message
|
||||||
|
*
|
||||||
|
* @param string $encrypted Message encrypted with safeEncrypt()
|
||||||
|
* @return string
|
||||||
|
* @throws \RangeException
|
||||||
|
* @throws \UnexpectedValueException
|
||||||
|
* @throws \UnexpectedValueException
|
||||||
|
*/
|
||||||
|
public function decrypt(string $encrypted): string
|
||||||
|
{
|
||||||
|
return $this->decryptData($encrypted, $this->key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encrypt a message
|
||||||
|
* static version
|
||||||
|
*
|
||||||
|
* @param string $message Message to encrypt
|
||||||
|
* @param string $key Encryption key (as hex string)
|
||||||
|
* @return string
|
||||||
|
* @throws \Exception
|
||||||
|
* @throws \RangeException
|
||||||
|
*/
|
||||||
|
public static function encryptKey(string $message, string $key): string
|
||||||
|
{
|
||||||
|
return self::getInstance()->encryptData($message, $key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encrypt a message
|
||||||
|
*
|
||||||
|
* @param string $message Message to encrypt
|
||||||
|
* @return string
|
||||||
|
* @throws \Exception
|
||||||
|
* @throws \RangeException
|
||||||
|
*/
|
||||||
|
public function encrypt(string $message): string
|
||||||
|
{
|
||||||
|
return $this->encryptData($message, $this->key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// __END__
|
// __END__
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
base="/storage/var/www/html/developers/clemens/core_data/composer-packages/CoreLibs-Composer-All/";
|
base="/storage/var/www/html/developers/clemens/core_data/composer-packages/CoreLibs-Composer-All/";
|
||||||
vendor/bin/phan --progress-bar -C --analyze-twice
|
tools/phan --progress-bar -C --analyze-twice
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
base="/storage/var/www/html/developers/clemens/core_data/composer-packages/CoreLibs-Composer-All/";
|
base="/storage/var/www/html/developers/clemens/core_data/composer-packages/CoreLibs-Composer-All/";
|
||||||
vendor/bin/phpstan
|
tools/phpstan
|
||||||
|
|||||||
@@ -15,25 +15,27 @@ php_bin="";
|
|||||||
if [ ! -z "${1}" ]; then
|
if [ ! -z "${1}" ]; then
|
||||||
case "${1}" in
|
case "${1}" in
|
||||||
# "7.3") php_bin="/usr/bin/php7.3 "; ;;
|
# "7.3") php_bin="/usr/bin/php7.3 "; ;;
|
||||||
"7.4") php_bin="/usr/bin/php7.4 "; ;;
|
#"7.4") php_bin="/usr/bin/php7.4 "; ;;
|
||||||
"8.0") php_bin="/usr/bin/php8.0 "; ;;
|
#"8.0") php_bin="/usr/bin/php8.0 "; ;;
|
||||||
"8.1") php_bin="/usr/bin/php8.1 "; ;;
|
#"8.1") php_bin="/usr/bin/php8.1 "; ;;
|
||||||
"8.2") php_bin="/usr/bin/php8.2 "; ;;
|
"8.2") php_bin="/usr/bin/php8.2 "; ;;
|
||||||
|
"8.3") php_bin="/usr/bin/php8.3 "; ;;
|
||||||
*) echo "Not support PHP: ${1}"; exit; ;;
|
*) echo "Not support PHP: ${1}"; exit; ;;
|
||||||
esac;
|
esac;
|
||||||
fi;
|
fi;
|
||||||
if [ ! -z "${2}" ] && [ -z "${php_bin}" ]; then
|
if [ ! -z "${2}" ] && [ -z "${php_bin}" ]; then
|
||||||
case "${2}" in
|
case "${2}" in
|
||||||
# "7.3") php_bin="/usr/bin/php7.3 "; ;;
|
# "7.3") php_bin="/usr/bin/php7.3 "; ;;
|
||||||
"7.4") php_bin="/usr/bin/php7.4 "; ;;
|
#"7.4") php_bin="/usr/bin/php7.4 "; ;;
|
||||||
"8.0") php_bin="/usr/bin/php8.0 "; ;;
|
#"8.0") php_bin="/usr/bin/php8.0 "; ;;
|
||||||
"8.1") php_bin="/usr/bin/php8.1 "; ;;
|
#"8.1") php_bin="/usr/bin/php8.1 "; ;;
|
||||||
"8.2") php_bin="/usr/bin/php8.2 "; ;;
|
"8.2") php_bin="/usr/bin/php8.2 "; ;;
|
||||||
|
"8.3") php_bin="/usr/bin/php8.3 "; ;;
|
||||||
*) echo "Not support PHP: ${1}"; exit; ;;
|
*) echo "Not support PHP: ${1}"; exit; ;;
|
||||||
esac;
|
esac;
|
||||||
fi;
|
fi;
|
||||||
|
|
||||||
phpunit_call="${php_bin}${base}vendor/bin/phpunit ${opt_testdox} -c ${base}phpunit.xml ${base}test/phpunit/";
|
phpunit_call="${php_bin}${base}tools/phpunit ${opt_testdox} -c ${base}phpunit.xml ${base}test/phpunit/";
|
||||||
|
|
||||||
${phpunit_call};
|
${phpunit_call};
|
||||||
|
|
||||||
|
|||||||
@@ -28,10 +28,10 @@ final class CoreLibsCheckFileTest extends TestCase
|
|||||||
public function filesList(): array
|
public function filesList(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
['filename.txt', 'txt', 5],
|
['filename.txt', 'txt', 5, 'text/plain'],
|
||||||
['filename.csv', 'csv', 15],
|
['filename.csv', 'csv', 15, 'text/csv'],
|
||||||
['filename.tsv', 'tsv', 0],
|
['filename.tsv', 'tsv', 0, 'text/plain'],
|
||||||
['file_does_not_exits', '', -1],
|
['file_does_not_exits', '', -1, ''],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,6 +63,15 @@ final class CoreLibsCheckFileTest extends TestCase
|
|||||||
return $list;
|
return $list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function mimeTypeProvider(): array
|
||||||
|
{
|
||||||
|
$list = [];
|
||||||
|
foreach ($this->filesList() as $row) {
|
||||||
|
$list[$row[0] . ' must be mime type ' . $row[3]] = [$row[0], $row[3]];
|
||||||
|
}
|
||||||
|
return $list;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests if file extension matches
|
* Tests if file extension matches
|
||||||
*
|
*
|
||||||
@@ -115,6 +124,51 @@ final class CoreLibsCheckFileTest extends TestCase
|
|||||||
unlink($this->base_folder . $input);
|
unlink($this->base_folder . $input);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @covers ::getMimeType
|
||||||
|
* @dataProvider mimeTypeProvider
|
||||||
|
* @testdox getMimeType $input must be mime type $expected [$_dataName]
|
||||||
|
*
|
||||||
|
* @param string $input
|
||||||
|
* @param string $expected
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testGetMimeType(string $input, string $expected): void
|
||||||
|
{
|
||||||
|
if (!empty($expected)) {
|
||||||
|
$file = $this->base_folder . $input;
|
||||||
|
$fp = fopen($file, 'w');
|
||||||
|
switch ($expected) {
|
||||||
|
case 'text/csv':
|
||||||
|
for ($i = 1; $i <= 10; $i++) {
|
||||||
|
fwrite($fp, '"This is row","' . $expected . '",' . $i . PHP_EOL);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'text/tsv':
|
||||||
|
for ($i = 1; $i <= 10; $i++) {
|
||||||
|
fwrite($fp, "\"This is row\"\t\"" . $expected . "\"\t\"" . $i . PHP_EOL);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'text/plain':
|
||||||
|
fwrite($fp, 'This is mime type: ' . $expected . PHP_EOL);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
fclose($fp);
|
||||||
|
} else {
|
||||||
|
$this->expectException(\UnexpectedValueException::class);
|
||||||
|
}
|
||||||
|
$this->assertEquals(
|
||||||
|
$expected,
|
||||||
|
\CoreLibs\Check\File::getMimeType($this->base_folder . $input)
|
||||||
|
);
|
||||||
|
// unlink file
|
||||||
|
if (is_file($this->base_folder . $input)) {
|
||||||
|
unlink($this->base_folder . $input);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// __END__
|
// __END__
|
||||||
|
|||||||
@@ -33,15 +33,14 @@ final class CoreLibsConvertMimeEncodeTest extends TestCase
|
|||||||
'The quick brown fox jumps over the lazy sheep that sleeps in the ravine '
|
'The quick brown fox jumps over the lazy sheep that sleeps in the ravine '
|
||||||
. 'and has no idea what is going on here',
|
. 'and has no idea what is going on here',
|
||||||
'UTF-8',
|
'UTF-8',
|
||||||
'The quick brown fox jumps over the lazy sheep that sleeps in the ravine '
|
"The quick brown fox jumps over the lazy sheep that sleeps in the ravine and\r\n"
|
||||||
. 'and has no idea what is going on here'
|
. ' has no idea what is going on here'
|
||||||
],
|
],
|
||||||
'standard with special chars UTF-8' => [
|
'standard with special chars UTF-8' => [
|
||||||
'This is ümläßtと漢字もカタカナ!!^$%&',
|
'This is ümläßtと漢字もカタカナ!!^$%&',
|
||||||
'UTF-8',
|
'UTF-8',
|
||||||
'This is =?UTF-8?B?w7xtbMOkw59044Go5ryi5a2X44KC44Kr44K/44Kr44OK77yBIV4k?='
|
"This is =?UTF-8?B?w7xtbMOkw59044Go5ryi5a2X44KC44Kr44K/44Kr44OK77yBIV4k?=\r\n"
|
||||||
. "\r\n"
|
. ' =?UTF-8?B?JSY=?='
|
||||||
. ' =?UTF-8?B?JQ==?=&'
|
|
||||||
],
|
],
|
||||||
'35 chars and space at the end UTF-8' => [
|
'35 chars and space at the end UTF-8' => [
|
||||||
'12345678901234567890123456789012345 '
|
'12345678901234567890123456789012345 '
|
||||||
@@ -62,9 +61,8 @@ final class CoreLibsConvertMimeEncodeTest extends TestCase
|
|||||||
. 'is there a space?',
|
. 'is there a space?',
|
||||||
'UTF-8',
|
'UTF-8',
|
||||||
"=?UTF-8?B?44Kr44K/44Kr44OK44Kr44K/44Kr44OK44GL44Gq44Kr44K/44Kr44OK44Kr?=\r\n"
|
"=?UTF-8?B?44Kr44K/44Kr44OK44Kr44K/44Kr44OK44GL44Gq44Kr44K/44Kr44OK44Kr?=\r\n"
|
||||||
. " =?UTF-8?B?44K/44Kr44OK?=\r\n"
|
. " =?UTF-8?B?44K/44Kr44OK44GL44Gq44Kr44K/44Kr44OK44Kr44K/44Kr44OK44GL44Gq?=\r\n"
|
||||||
. " =?UTF-8?B?44GL44Gq44Kr44K/44Kr44OK44Kr44K/44Kr44OK44GL44Gq44Kr44K/44Kr?=\r\n"
|
. " =?UTF-8?B?44Kr44K/44Kr44OK44Kr44K/IGlzIHRoZXJlIGEgc3BhY2U/?="
|
||||||
. " =?UTF-8?B?44OK44Kr44K/?= is there a =?UTF-8?B?c3BhY2U/?="
|
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -85,16 +83,28 @@ final class CoreLibsConvertMimeEncodeTest extends TestCase
|
|||||||
// print "MIME: -" . $encoded . "-\n";
|
// print "MIME: -" . $encoded . "-\n";
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$expected,
|
$expected,
|
||||||
$encoded
|
$encoded,
|
||||||
|
"__mbMimeEncode"
|
||||||
);
|
);
|
||||||
$decoded = mb_decode_mimeheader($encoded);
|
$decoded = mb_decode_mimeheader($encoded);
|
||||||
// print "INPUT : " . $input . "\n";
|
// print "ENCODED: " . $encoded . "\n";
|
||||||
// print "DECODED: " . $decoded . "\n";
|
// print "INPUT : " . $input . " | " . mb_strlen($input) . "\n";
|
||||||
|
// print "DECODED: " . $decoded . " | " . mb_strlen($decoded) . "\n";
|
||||||
|
// $test_enc = mb_encode_mimeheader($input, $encoding);
|
||||||
|
// $test_dec = mb_decode_mimeheader($test_enc);
|
||||||
|
// print "TEST ENC: " . $test_enc . "\n";
|
||||||
// back compare decoded
|
// back compare decoded
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$input,
|
$input,
|
||||||
$decoded
|
$decoded,
|
||||||
|
"mb_decode_mimeheader"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// $this->assertEquals(
|
||||||
|
// $input,
|
||||||
|
// $test_dec,
|
||||||
|
// 'mb_encode_to_decode'
|
||||||
|
// );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -256,6 +256,80 @@ final class CoreLibsConvertStringsTest extends TestCase
|
|||||||
$output
|
$output
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* provider for testStripMultiplePathSlashes
|
||||||
|
*
|
||||||
|
* @return array<mixed>
|
||||||
|
*/
|
||||||
|
public function stripMultiplePathSlashesProvider(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'no slahses' => [
|
||||||
|
'input' => 'string_abc',
|
||||||
|
'expected' => 'string_abc',
|
||||||
|
],
|
||||||
|
'one slash' => [
|
||||||
|
'input' => 'some/foo',
|
||||||
|
'expected' => 'some/foo',
|
||||||
|
],
|
||||||
|
'two slashes' => [
|
||||||
|
'input' => 'some//foo',
|
||||||
|
'expected' => 'some/foo',
|
||||||
|
],
|
||||||
|
'three slashes' => [
|
||||||
|
'input' => 'some///foo',
|
||||||
|
'expected' => 'some/foo',
|
||||||
|
],
|
||||||
|
'slashes in front' => [
|
||||||
|
'input' => '/foo',
|
||||||
|
'expected' => '/foo',
|
||||||
|
],
|
||||||
|
'two slashes in front' => [
|
||||||
|
'input' => '//foo',
|
||||||
|
'expected' => '/foo',
|
||||||
|
],
|
||||||
|
'thee slashes in front' => [
|
||||||
|
'input' => '///foo',
|
||||||
|
'expected' => '/foo',
|
||||||
|
],
|
||||||
|
'slashes in back' => [
|
||||||
|
'input' => 'foo/',
|
||||||
|
'expected' => 'foo/',
|
||||||
|
],
|
||||||
|
'two slashes in back' => [
|
||||||
|
'input' => 'foo//',
|
||||||
|
'expected' => 'foo/',
|
||||||
|
],
|
||||||
|
'thee slashes in back' => [
|
||||||
|
'input' => 'foo///',
|
||||||
|
'expected' => 'foo/',
|
||||||
|
],
|
||||||
|
'multiple slashes' => [
|
||||||
|
'input' => '/foo//bar///string/end_times',
|
||||||
|
'expected' => '/foo/bar/string/end_times',
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test multiple slashes clean up
|
||||||
|
*
|
||||||
|
* @covers ::stripMultiplePathSlashes
|
||||||
|
* @dataProvider stripMultiplePathSlashesProvider
|
||||||
|
* @testdox stripMultiplePathSlashes $input will be $expected [$_dataName]
|
||||||
|
*
|
||||||
|
* @param string $input
|
||||||
|
* @param string $expected
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testStripMultiplePathSlashes(string $input, string $expected): void
|
||||||
|
{
|
||||||
|
$this->assertEquals(
|
||||||
|
$expected,
|
||||||
|
\CoreLibs\Convert\Strings::stripMultiplePathSlashes($input)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// __END__
|
// __END__
|
||||||
|
|||||||
@@ -46,12 +46,34 @@ final class CoreLibsSecuritySymmetricEncryptionTest extends TestCase
|
|||||||
public function testEncryptDecryptSuccess(string $input, string $expected): void
|
public function testEncryptDecryptSuccess(string $input, string $expected): void
|
||||||
{
|
{
|
||||||
$key = CreateKey::generateRandomKey();
|
$key = CreateKey::generateRandomKey();
|
||||||
$encrypted = SymmetricEncryption::encrypt($input, $key);
|
|
||||||
$decrypted = SymmetricEncryption::decrypt($encrypted, $key);
|
// test class
|
||||||
|
$crypt = new SymmetricEncryption($key);
|
||||||
|
$encrypted = $crypt->encrypt($input);
|
||||||
|
$decrypted = $crypt->decrypt($encrypted);
|
||||||
|
$this->assertEquals(
|
||||||
|
$expected,
|
||||||
|
$decrypted,
|
||||||
|
'Class call',
|
||||||
|
);
|
||||||
|
|
||||||
|
// test indirect
|
||||||
|
$encrypted = SymmetricEncryption::getInstance($key)->encrypt($input);
|
||||||
|
$decrypted = SymmetricEncryption::getInstance($key)->decrypt($encrypted);
|
||||||
|
$this->assertEquals(
|
||||||
|
$expected,
|
||||||
|
$decrypted,
|
||||||
|
'Class Instance call',
|
||||||
|
);
|
||||||
|
|
||||||
|
// test static
|
||||||
|
$encrypted = SymmetricEncryption::encryptKey($input, $key);
|
||||||
|
$decrypted = SymmetricEncryption::decryptKey($encrypted, $key);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$expected,
|
$expected,
|
||||||
$decrypted
|
$decrypted,
|
||||||
|
'Static call',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,10 +108,24 @@ final class CoreLibsSecuritySymmetricEncryptionTest extends TestCase
|
|||||||
public function testEncryptFailed(string $input, string $exception_message): void
|
public function testEncryptFailed(string $input, string $exception_message): void
|
||||||
{
|
{
|
||||||
$key = CreateKey::generateRandomKey();
|
$key = CreateKey::generateRandomKey();
|
||||||
$encrypted = SymmetricEncryption::encrypt($input, $key);
|
|
||||||
$wrong_key = CreateKey::generateRandomKey();
|
$wrong_key = CreateKey::generateRandomKey();
|
||||||
|
|
||||||
|
// wrong key in class call
|
||||||
|
$crypt = new SymmetricEncryption($key);
|
||||||
|
$encrypted = $crypt->encrypt($input);
|
||||||
$this->expectExceptionMessage($exception_message);
|
$this->expectExceptionMessage($exception_message);
|
||||||
SymmetricEncryption::decrypt($encrypted, $wrong_key);
|
$crypt->setKey($key);
|
||||||
|
$crypt->decrypt($encrypted);
|
||||||
|
|
||||||
|
// class instance
|
||||||
|
$encrypted = SymmetricEncryption::getInstance($key)->encrypt($input);
|
||||||
|
$this->expectExceptionMessage($exception_message);
|
||||||
|
SymmetricEncryption::getInstance($wrong_key)->decrypt($encrypted);
|
||||||
|
|
||||||
|
// class static
|
||||||
|
$encrypted = SymmetricEncryption::encryptKey($input, $key);
|
||||||
|
$this->expectExceptionMessage($exception_message);
|
||||||
|
SymmetricEncryption::decryptKey($encrypted, $wrong_key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -107,7 +143,6 @@ final class CoreLibsSecuritySymmetricEncryptionTest extends TestCase
|
|||||||
'too short hex key' => [
|
'too short hex key' => [
|
||||||
'key' => '1cabd5cba9e042f12522f4ff2de5c31d233b',
|
'key' => '1cabd5cba9e042f12522f4ff2de5c31d233b',
|
||||||
'excpetion_message' => 'Key is not the correct size (must be '
|
'excpetion_message' => 'Key is not the correct size (must be '
|
||||||
. 'SODIUM_CRYPTO_SECRETBOX_KEYBYTES bytes long).'
|
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -126,13 +161,33 @@ final class CoreLibsSecuritySymmetricEncryptionTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function testWrongKey(string $key, string $exception_message): void
|
public function testWrongKey(string $key, string $exception_message): void
|
||||||
{
|
{
|
||||||
$this->expectExceptionMessage($exception_message);
|
|
||||||
SymmetricEncryption::encrypt('test', $key);
|
|
||||||
// we must encrypt valid thing first so we can fail with the wrong kjey
|
|
||||||
$enc_key = CreateKey::generateRandomKey();
|
$enc_key = CreateKey::generateRandomKey();
|
||||||
$encrypted = SymmetricEncryption::encrypt('test', $enc_key);
|
|
||||||
|
// class
|
||||||
|
$crypt = new SymmetricEncryption($key);
|
||||||
$this->expectExceptionMessage($exception_message);
|
$this->expectExceptionMessage($exception_message);
|
||||||
SymmetricEncryption::decrypt($encrypted, $key);
|
$crypt->encrypt('test');
|
||||||
|
$crypt->setKey($enc_key);
|
||||||
|
$encrypted = $crypt->encrypt('test');
|
||||||
|
$this->expectExceptionMessage($exception_message);
|
||||||
|
$crypt->setKey($key);
|
||||||
|
$crypt->decrypt($encrypted);
|
||||||
|
|
||||||
|
// class instance
|
||||||
|
$this->expectExceptionMessage($exception_message);
|
||||||
|
SymmetricEncryption::getInstance($key)->encrypt('test');
|
||||||
|
// we must encrypt valid thing first so we can fail with the wrong key
|
||||||
|
$encrypted = SymmetricEncryption::getInstance($enc_key)->encrypt('test');
|
||||||
|
$this->expectExceptionMessage($exception_message);
|
||||||
|
SymmetricEncryption::getInstance($key)->decrypt($encrypted);
|
||||||
|
|
||||||
|
// class static
|
||||||
|
$this->expectExceptionMessage($exception_message);
|
||||||
|
SymmetricEncryption::encryptKey('test', $key);
|
||||||
|
// we must encrypt valid thing first so we can fail with the wrong key
|
||||||
|
$encrypted = SymmetricEncryption::encryptKey('test', $enc_key);
|
||||||
|
$this->expectExceptionMessage($exception_message);
|
||||||
|
SymmetricEncryption::decryptKey($encrypted, $key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -145,7 +200,7 @@ final class CoreLibsSecuritySymmetricEncryptionTest extends TestCase
|
|||||||
return [
|
return [
|
||||||
'too short ciphertext' => [
|
'too short ciphertext' => [
|
||||||
'input' => 'short',
|
'input' => 'short',
|
||||||
'exception_message' => 'Invalid ciphertext (too short)'
|
'exception_message' => 'Decipher message failed: '
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -164,8 +219,18 @@ final class CoreLibsSecuritySymmetricEncryptionTest extends TestCase
|
|||||||
public function testWrongCiphertext(string $input, string $exception_message): void
|
public function testWrongCiphertext(string $input, string $exception_message): void
|
||||||
{
|
{
|
||||||
$key = CreateKey::generateRandomKey();
|
$key = CreateKey::generateRandomKey();
|
||||||
|
// class
|
||||||
|
$crypt = new SymmetricEncryption($key);
|
||||||
$this->expectExceptionMessage($exception_message);
|
$this->expectExceptionMessage($exception_message);
|
||||||
SymmetricEncryption::decrypt($input, $key);
|
$crypt->decrypt($input);
|
||||||
|
|
||||||
|
// class instance
|
||||||
|
$this->expectExceptionMessage($exception_message);
|
||||||
|
SymmetricEncryption::getInstance($key)->decrypt($input);
|
||||||
|
|
||||||
|
// class static
|
||||||
|
$this->expectExceptionMessage($exception_message);
|
||||||
|
SymmetricEncryption::decryptKey($input, $key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
/home/clemens/.phive/phars/phan-5.4.2.phar
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
/home/clemens/.phive/phars/phpcbf-3.7.2.phar
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
/home/clemens/.phive/phars/phpcs-3.7.2.phar
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
/home/clemens/.phive/phars/phpstan-1.10.46.phar
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
/home/clemens/.phive/phars/phpunit-9.6.13.phar
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
/home/clemens/.phive/phars/psalm-5.16.0.phar
|
|
||||||
Reference in New Issue
Block a user