Skip to content

Commit 790fff1

Browse files
committed
adapt defuse/php-ancryption as internal package adapting Ecotone coding standards
1 parent fb35463 commit 790fff1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+11344
-5
lines changed

composer.json

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"packages/PdoEventSourcing/src"
4545
],
4646
"Ecotone\\JMSConverter\\": "packages/JmsConverter/src",
47+
"Ecotone\\PHPEncryption\\": "packages/PHPEncryption/src",
4748
"Ecotone\\Redis\\": "packages/Redis/src",
4849
"Ecotone\\Sqs\\": "packages/Sqs/src",
4950
"Ecotone\\Laravel\\": "packages/Laravel/src",
@@ -94,6 +95,9 @@
9495
"Test\\Ecotone\\JMSConverter\\": [
9596
"packages/JmsConverter/tests"
9697
],
98+
"Test\\Ecotone\\PHPEncryption\\": [
99+
"packages/PHPEncryption/tests/unit"
100+
],
97101
"Test\\Ecotone\\Redis\\": [
98102
"packages/Redis/tests"
99103
],
@@ -117,17 +121,18 @@
117121
},
118122
"require": {
119123
"php": "^8.2",
124+
"ext-amqp": "*",
125+
"ext-openssl": "*",
120126
"doctrine/dbal": "^3.9|^4.0",
121127
"doctrine/persistence": "^2.5|^3.4",
122-
"defuse/php-encryption": "^2.4",
123128
"enqueue/amqp-lib": "^0.10.25",
124129
"enqueue/redis": "^0.10.9",
125130
"enqueue/sqs": "^0.10.15",
126131
"enqueue/enqueue": "^0.10.0",
127-
"ext-amqp": "*",
128132
"laminas/laminas-code": "^4",
129133
"jms/serializer": "^3.32",
130134
"laravel/framework": "^9.5.2|^10.0|^11.0|^12.0|^13.0",
135+
"paragonie/random_compat": "^2.0",
131136
"prooph/pdo-event-store": "^1.16.3",
132137
"psr/log": "^2.0|^3.0",
133138
"queue-interop/queue-interop": "^0.8",
@@ -172,7 +177,8 @@
172177
"symfony/monolog-bundle": "^3.10",
173178
"kwn/php-rdkafka-stubs": "^2.2",
174179
"symfony/var-exporter": "^6.4|^7.0|^8.0",
175-
"enqueue/dsn": "^0.10.27"
180+
"enqueue/dsn": "^0.10.27",
181+
"yoast/phpunit-polyfills": "^4.0.0"
176182
},
177183
"conflict": {
178184
"symfony/doctrine-messenger": ">7.0.5 < 7.1.0",
@@ -212,7 +218,10 @@
212218
},
213219
"scripts": {
214220
"tests:phpstan": "vendor/bin/phpstan",
215-
"tests:phpunit": "vendor/bin/phpunit --no-coverage",
221+
"tests:phpunit": [
222+
"(cd packages/PHPEncryption && tests/before-tests.sh)",
223+
"vendor/bin/phpunit --no-coverage"
224+
],
216225
"tests:behat": "vendor/bin/behat -vvv",
217226
"tests:ci": [
218227
"@tests:phpstan",

packages/DataProtection/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"ext-openssl": "*",
4040
"ecotone/ecotone": "~1.299.2",
4141
"ecotone/jms-converter": "~1.299.2",
42-
"defuse/php-encryption": "^2.4"
42+
"ecotone/php-encryption": "~1.299.2"
4343
},
4444
"require-dev": {
4545
"phpunit/phpunit": "^9.5|^10.5|^11.0",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
tests/ export-ignore
2+
.coveralls.yml export-ignore
3+
.gitattributes export-ignore
4+
.gitignore export-ignore
5+
behat.yaml export-ignore
6+
phpstan.neon export-ignore
7+
phpunit.xml export-ignore

packages/PHPEncryption/.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.idea/
2+
vendor/
3+
bin/
4+
tests/coverage
5+
!tests/coverage/.gitkeep
6+
file
7+
.phpunit.result.cache
8+
composer.lock
9+
phpunit.xml
10+
11+
tests/unit/File/big-generated-file

packages/PHPEncryption/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Copyright (c) 2025 Dariusz Gafka <support@simplycodedsoftware.com>
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
10+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
12+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
13+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
14+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
15+
THE SOFTWARE.
16+
17+
**Scope of the License**
18+
19+
Apache-2.0 Licence applies to non Enterprise Functionalities of the Ecotone Framework.
20+
Functionalities of the Ecotone Framework referred to as Enterprise functionalities, are not covered under the Apache-2.0 license. These functionalities are provided under a separate Enterprise License.
21+
For details on the Enterprise License, please refer to the [LICENSE-ENTERPRISE](./LICENSE-ENTERPRISE) file.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Copyright (c) 2025 Dariusz Gafka <support@simplycodedsoftware.com>
2+
3+
Licence is available at [ecotone.tech/documents/ecotone_enterprise_licence.pdf](https://ecotone.tech/documents/ecotone_enterprise_licence.pdf)

packages/PHPEncryption/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# This is Read Only Repository
2+
To contribute make use of [Ecotone-Dev repository](https://github.com/ecotoneframework/ecotone-dev).
3+
4+
<p align="left"><a href="https://ecotone.tech" target="_blank">
5+
<img src="https://github.com/ecotoneframework/ecotone-dev/blob/main/ecotone_small.png?raw=true">
6+
</a></p>
7+
8+
![Github Actions](https://github.com/ecotoneFramework/ecotone-dev/actions/workflows/split-testing.yml/badge.svg)
9+
[![Latest Stable Version](https://poser.pugx.org/ecotone/ecotone/v/stable)](https://packagist.org/packages/ecotone/ecotone)
10+
[![License](https://poser.pugx.org/ecotone/ecotone/license)](https://packagist.org/packages/ecotone/ecotone)
11+
[![Total Downloads](https://img.shields.io/packagist/dt/ecotone/ecotone)](https://packagist.org/packages/ecotone/ecotone)
12+
[![PHP Version Require](https://img.shields.io/packagist/dependency-v/ecotone/ecotone/php.svg)](https://packagist.org/packages/ecotone/ecotone)
13+
14+
The roots of Object Oriented Programming (OOP) were mainly about communication using Messages and logic encapsulation.
15+
`Ecotone` aims to return to the origins of OOP, by providing tools which allows us to fully move the focus from Objects to Flows, from Data storage to Application Design, from Technicalities to Business logic.
16+
Ecotone does that by making Messages first class-citizen in our Applications.
17+
18+
Thanks to being Message-Driven at the foundation level, Ecotone provides architecture which is resilient and scalable by default, making it possible for Developers to focus on business problems instead of technical concerns.
19+
Together with declarative configuration and higher level building blocks, it makes the system design explicit, easy to follow and change no matter of Developers experience.
20+
21+
Visit main page [ecotone.tech](https://ecotone.tech) to learn more.
22+
23+
> Ecotone can be used with [Symfony](https://docs.ecotone.tech/modules/symfony-ddd-cqrs-event-sourcing) and [Laravel](https://docs.ecotone.tech/modules/laravel-ddd-cqrs-event-sourcing) frameworks, or any other framework using [Ecotone Lite](https://docs.ecotone.tech/install-php-service-bus#install-ecotone-lite-no-framework).
24+
>
25+
## Getting started
26+
27+
The quickstart [page](https://docs.ecotone.tech/quick-start) of the
28+
[reference guide](https://docs.ecotone.tech) provides a starting point for using Ecotone.
29+
Read more on the [Ecotone's Blog](https://blog.ecotone.tech).
30+
31+
## AI-Friendly Documentation
32+
33+
Ecotone provides AI-optimized documentation for use with AI assistants and code editors:
34+
35+
- **MCP Server**: `https://docs.ecotone.tech/~gitbook/mcp` - [Install in VSCode](vscode:mcp/install?%7B%22name%22%3A%22Ecotone%22%2C%22url%22%3A%22https%3A%2F%2Fdocs.ecotone.tech%2F~gitbook%2Fmcp%22%7D)
36+
- **LLMs.txt**: [ecotone.tech/llms.txt](https://ecotone.tech/llms.txt)
37+
- **Context7**: Available via [@upstash/context7-mcp](https://github.com/upstash/context7)
38+
39+
Learn more: [AI Integration Guide](https://docs.ecotone.tech/other/ai-integration)
40+
41+
## Feature requests and issue reporting
42+
43+
Use [issue tracking system](https://github.com/ecotoneframework/ecotone-dev/issues) for new feature request and bugs.
44+
Please verify that it's not already reported by someone else.
45+
46+
## Contact
47+
48+
If you want to talk or ask questions about Ecotone
49+
50+
- [**Twitter**](https://twitter.com/EcotonePHP)
51+
- **support@simplycodedsoftware.com**
52+
- [**Community Channel**](https://discord.gg/GwM2BSuXeg)
53+
54+
## Support Ecotone
55+
56+
If you want to help building and improving Ecotone consider becoming a sponsor:
57+
58+
- [Sponsor Ecotone](https://github.com/sponsors/dgafka)
59+
- [Contribute to Ecotone](https://github.com/ecotoneframework/ecotone-dev).
60+
61+
## Tags
62+
63+
PHP, DDD, CQRS, Event Sourcing, Symfony, Laravel, Service Bus, Event Driven Architecture, SOA, Events, Commands
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"name": "ecotone/php-encryption",
3+
"license": [
4+
"Apache-2.0",
5+
"proprietary"
6+
],
7+
"homepage": "https://docs.ecotone.tech/",
8+
"forum": "https://discord.gg/GwM2BSuXeg",
9+
"type": "library",
10+
"minimum-stability": "dev",
11+
"prefer-stable": true,
12+
"authors": [
13+
{
14+
"name": "Dariusz Gafka",
15+
"email": "support@simplycodedsoftware.com"
16+
}
17+
],
18+
"keywords": ["security", "encryption", "AES", "openssl", "cipher", "cryptography", "symmetric key cryptography", "crypto", "encrypt", "authenticated encryption"],
19+
"description": "Secure PHP Encryption Library",
20+
"autoload": {
21+
"psr-4": {
22+
"Ecotone\\PHPEncryption\\": "src"
23+
}
24+
},
25+
"autoload-dev": {
26+
"psr-4": {
27+
"Test\\Ecotone\\PHPEncryption\\": "tests/unit"
28+
}
29+
},
30+
"scripts": {
31+
"tests:phpstan": "vendor/bin/phpstan",
32+
"tests:phpunit": [
33+
"tests/before-tests.sh",
34+
"vendor/bin/phpunit --no-coverage --testdox"
35+
],
36+
"tests:ci": [
37+
"@tests:phpstan",
38+
"@tests:phpunit"
39+
]
40+
},
41+
"require": {
42+
"php": "^8.2",
43+
"ext-openssl": "*",
44+
"paragonie/random_compat": "^2.0"
45+
},
46+
"require-dev": {
47+
"phpstan/phpstan": "^1.8",
48+
"phpunit/phpunit": "^11",
49+
"yoast/phpunit-polyfills": "^4.0.0"
50+
},
51+
"extra": {
52+
"branch-alias": {
53+
"dev-main": "1.299.1-dev"
54+
},
55+
"ecotone": {
56+
"repository": "php-encryption"
57+
},
58+
"merge-plugin": {
59+
"include": [
60+
"../local_packages.json"
61+
]
62+
},
63+
"license-info": {
64+
"Apache-2.0": {
65+
"name": "Apache License 2.0",
66+
"url": "https://github.com/ecotoneframework/ecotone-dev/blob/main/LICENSE",
67+
"description": "Allows to use non Enterprise features of Ecotone. For more information please write to support@simplycodedsoftware.com"
68+
},
69+
"proprietary": {
70+
"name": "Enterprise License",
71+
"description": "Allows to use Enterprise features of Ecotone. For more information please write to support@simplycodedsoftware.com"
72+
}
73+
},
74+
"release-time": "2026-02-07 18:38:09"
75+
},
76+
"config": {
77+
"allow-plugins": {
78+
"wikimedia/composer-merge-plugin": true
79+
}
80+
}
81+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
Cryptography Details
2+
=====================
3+
4+
Here is a high-level description of how this library works. Any discrepancy
5+
between this documentation and the actual implementation will be considered
6+
a security bug.
7+
8+
Let's start with the following definitions:
9+
10+
- HKDF-SHA256(*k*, *n*, *info*, *s*) is the key derivation function specified in
11+
RFC 5869 (using the SHA256 hash function). The parameters are:
12+
- *k*: The initial keying material.
13+
- *n*: The number of output bytes.
14+
- *info*: The info string.
15+
- *s*: The salt.
16+
- AES-256-CTR(*m*, *k*, *iv*) is AES-256 encryption in CTR mode. The parameters
17+
are:
18+
- *m*: An arbitrary-length (possibly zero-length) message.
19+
- *k*: A 32-byte key.
20+
- *iv*: A 16-byte initialization vector (nonce).
21+
- PBKDF2-SHA256(*p*, *s*, *i*, *n*) is the password-based key derivation
22+
function defined in RFC 2898 (using the SHA256 hash function). The parameters
23+
are:
24+
- *p*: The password string.
25+
- *s*: The salt string.
26+
- *i*: The iteration count.
27+
- *n*: The output length in bytes.
28+
- VERSION is the string `"\xDE\xF5\x02\x00"`.
29+
- AUTHINFO is the string `"Ecotone|V2|KeyForAuthentication"`.
30+
- ENCRINFO is the string `"Ecotone|V2|KeyForEncryption"`.
31+
32+
To encrypt a message *m* using a 32-byte key *k*, the following steps are taken:
33+
34+
1. Generate a random 32-byte string *salt*.
35+
2. Derive the 32-byte authentication key *akey* = HKDF-SHA256(*k*, 32, AUTHINFO, *salt*).
36+
3. Derive the 32-byte encryption key *ekey* = HKDF-SHA256(*k*, 32, ENCRINFO, *salt*).
37+
4. Generate a random 16-byte initialization vector *iv*.
38+
5. Compute *c* = AES-256-CTR(*m*, *ekey*, *iv*).
39+
6. Combine *ctxt* = VERSION || *salt* || *iv* || *c*.
40+
7. Compute *h* = HMAC-SHA256(*ctxt*, *akey*).
41+
8. Output *ctxt* || *h*.
42+
43+
Decryption is roughly the reverse process (see the code for details, since the
44+
security of the decryption routine is highly implementation-dependent).
45+
46+
For encryption using a password *p*, steps 1-3 above are replaced by:
47+
48+
1. Generate a random 32-byte string *salt*.
49+
2. Compute *k* = PBKDF2-SHA256(SHA256(*p*), *salt*, 100000, 32).
50+
3. Derive the 32-byte authentication key *akey* = HKDF-SHA256(*k*, 32, AUTHINFO, *salt*)
51+
4. Derive the 32-byte encryption key *ekey* = HKDF-SHA256(*k*, 32, ENCRINFO, *salt*)
52+
53+
The remainder of the process is the same. Notice the reuse of the same *salt*
54+
for PBKDF2-SHA256 and HKDF-SHA256. The prehashing of the password in step 2 is
55+
done to prevent a DoS attack using long passwords.
56+
57+
For `KeyProtectedByPassword`, the serialized key is encrypted according to the
58+
password encryption defined above. However, the actual password used for
59+
encryption is the SHA256 hash of the password the user provided. This is done in
60+
order to provide domain separation between the message encryption in the user's
61+
application and the internal key encryption done by this library.

packages/PHPEncryption/docs/FAQ.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Frequently Asked Questions
2+
===========================
3+
4+
How do I use this library to encrypt passwords?
5+
------------------------------------------------
6+
7+
Passwords should not be encrypted, they should be hashed with a *slow* password
8+
hashing function that's designed to slow down password guessing attacks. See
9+
[How to Safely Store Your Users' Passwords in
10+
2016](https://paragonie.com/blog/2016/02/how-safely-store-password-in-2016).
11+
12+
How do I give it the same key every time instead of a new random key?
13+
----------------------------------------------------------------------
14+
15+
A `Key` object can be saved to a string by calling its `saveToAsciiSafeString()`
16+
method. You will have to save that string somewhere safe, and then load it back
17+
into a `Key` object using `Key`'s `loadFromAsciiSafeString` static method.
18+
19+
Where you store the string depends on your application. For example if you are
20+
using `KeyProtectedByPassword` to encrypt files with a user's login password,
21+
then you should not store the `Key` at all. If you are protecting sensitive data
22+
on a server that may be compromised, then you should store it in a hardware
23+
security module. When in doubt, consult a security expert.
24+
25+
Why is an EnvironmentIsBrokenException getting thrown?
26+
-------------------------------------------------------
27+
28+
Either you've encountered a bug in this library, or your system doesn't support
29+
the use of this library. For example, if your system does not have a secure
30+
random number generator, this library will refuse to run, by throwing that
31+
exception, instead of falling back to an insecure random number generator.
32+
33+
Why am I getting a BadFormatException when loading a Key from a string?
34+
------------------------------------------------------------------------
35+
36+
If you're getting this exception, then the string you're giving to
37+
`loadFromAsciiSafeString()` is *not* the same as the string you got from
38+
`saveToAsciiSafeString()`. Perhaps your database column isn't wide enough and
39+
it's truncating the string as you insert it?
40+
41+
Does encrypting hide the length of the plaintext?
42+
--------------------------------------------------
43+
44+
Encryption does not, and is not intended to, hide the length of the data being
45+
encrypted. For example, it is not safe to encrypt a field in which only a small
46+
number of different-length values are possible (e.g. "male" or "female") since
47+
it would be possible to tell what the plaintext is by looking at the length of
48+
the ciphertext. In order to do this safely, it is your responsibility to, before
49+
encrypting, pad the data out to the length of the longest string that will ever
50+
be encrypted. This way, all plaintexts are the same length, and no information
51+
about the plaintext can be gleaned from the length of the ciphertext.

0 commit comments

Comments
 (0)