Skip to content

Commit

Permalink
Add PHP Privacy Examples
Browse files Browse the repository at this point in the history
Signed-off-by: Nguyen Van Nguyen <[email protected]>
  • Loading branch information
nguyennv committed Sep 27, 2024
1 parent 34c85ca commit b924dcf
Show file tree
Hide file tree
Showing 5 changed files with 776 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
PHP Privacy Examples
====================

* [Key generation example](key-generation.php)
* [Key reading example](key-reading.php)
* [Cleartext signing example](cleartext-signing.php)
* [Data signing & encryption example](literal-data.php)
244 changes: 244 additions & 0 deletions examples/cleartext-signing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
<?php declare(strict_types=1);

require_once dirname(__DIR__) . "/vendor/autoload.php";

use OpenPGP\OpenPGP;

$passphase = "{8P@|gFI8YJgnf>dl:4B21BJ|8[]d|SK";

$keyData = <<<EOT
-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: PHP Privacy v1
Comment: https://github.com/web-of-trust/php-privacy
xcMGBGb2E+UBCAD9mcrGhqeajAQzuBZvO8y7wTbmrIxQrE+frfcJK33lU/yaMTq6L4KO6Lx/1L8S
KC+oyJwbfEpED+QM/wa16Xltx4AOJSE2Wak2FNkyxvGwKgEzFZzHgMo1elrVktbl97nPhvgNCLi6
VeuB75J2GxX9wsY9UrAt9iw7MdF+EwhaEgmUW03ej4XpKYnfHrFwCe4vNToYXY1ybqYul9w9cV44
Zcr+DiLrK4sIsMgtfijRtnZ6/ucjxr3S3bX6K/9CyRdL/F5PCa5sMPBEjEkrE8y2eTxAEJReEBzu
vLBvV4onx9vt1w8dIk/NNVF5nMUtDeNlsZxweMq5IwCv7tiD8+6fABEBAAH+BwMID2K2B8y7OJ7g
drpomebfM+u7qhtXMpnKUhj/aWm0CDsrsqx9gRSud05E1Ifw6knFieUQLBJafDWq4uTC2W11pcJV
npths6kczmFIcP0xV9Gyx2G+o+Mlbiz/WudBwpVr2+uOn1YWSMRJSnc5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AxyEarCwKAEGAEIAFQFAmb2E+UW
IQStDn3KK4PvuTpUKOrIehsRle+qKwkQyHobEZXvqispFAAAAAAAEAAQc2FsdEBvcGVucGdwLm9y
Z4wL/+gSXVx3wKtusPpHP88CGwwAAP12CAAX3lZbaZBi+AXJv4cRYWaA2o1AsyVkthEmScBmxLns
31x02HzpsH8APsC03qRLbnLF/TJDkomlDSvzTavJ6LWzXLAORY1eJP7eKz1QtPttT1yKcia8qZ9n
5PDRkFuSCd7otcAhoBnUTRnn2ra7mcCijYWcH7yGwg1Bhc1HWRCEcOpUt5VgsSxWO2qfnCO358lU
Uoed/KGdHpmmFjV0M4bKQHX7Gom8v3hEGja9pLzYvzGb2cQhPFOOK0ssPcy1IZcWfcgxew7yM2Ke
EllP0ZPmrysvMPDwFxLW2HHwQjABilKMwRM1oRttd2LA5BtBaw5Pw/dJy907vzUSSUrHCDok
=0gnP
-----END PGP PRIVATE KEY BLOCK-----
EOT;
$rsaPrivateKey = OpenPGP::decryptPrivateKey($keyData, $passphase);

$keyData = <<<EOT
-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: PHP Privacy v1
Comment: https://github.com/web-of-trust/php-privacy
xcK5BGb2E+URCADOP2KIFCNh2RJ/8vZLuglST004UnIYUh96raOR5finS9dce9M3P+0cg8AIpk0M
Rw9/FVzTL6Rb1+RAvzNEpN0S9JSY9dyt1SRhcKVinIRjp6e+R40vNLfgZ4uNilCk09nzhluzdqX3
ZVkYMdruONy9ihtJFLr/zoMzJfy2o2KMwniV1xhl1fP+jRbu0wwR4ZNqEY+Co3yZ38ZAMfmzZwO0
iMaw+8/MlvBz7+vbVd1jf+dbBF8tYQXDHlUsM6n4eSRVswsQntqodhA1BInUa48VgeBsuAcICPhX
c8u1Dzn855iVihM1xx9NNvPpbi+yneJr1FKPWv9pZhiHJyh1r/5fAOCXKKGix9sZ8Kb4BKRFZBqV
yU2yzars2q9EHaMxB/0WecOigsSCHkqneEI6hxXqC6w9zayL+ZL21H7sKot0JFAD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k5JmWpB
bcJ5IltP1Zt09UlgXA+gL709xXGM8fRtll91ktKjCRaYF+pYVKeax9G52esT73tzv0mgiQbd20CB
gqReAVUBHQVov5/+Idz2TFrs0QIplohfuilWKjQMvVa7xw3Zfz9eo1OGvaRdJZvyqMV0g++34A6q
c5iTI0Pz3N4BxO53v6QRbcdmgD5YX15G4+T91XOP2vceDXBXGhau8WmBf+mfoaTLxzds8arNLIjk
734Vkwl+i/4HAwiJVcUjP526FOAFgWCam24ZxXAUBF0S4vdRhrrV1BbocMKqZcpBvJHz7KC46Nkr
0Np7D6vzFpWDYZZyMKEeVLY8apH9/ak8+JjfhKzCmgQYEQgAVAUCZvYT5hYhBCXzQjGDxYFgSxHr
bc5gHwz06c92CRDOYB8M9OnPdikUAAAAAAAQABBzYWx0QG9wZW5wZ3Aub3JnqxjcuIbj3j6cUJge
SxTIwQIbDAAA+ewA31pUsURdzFltM00DUR/4I1pq9+oYSuWUGp2i6ywA30NckDDj7XXoqMbP2rOR
aCx35WNEMRaLjyALpTs=
=WXAi
-----END PGP PRIVATE KEY BLOCK-----
EOT;
$dsaPrivateKey = OpenPGP::decryptPrivateKey($keyData, $passphase);

$keyData = <<<EOT
-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: PHP Privacy v1
Comment: https://github.com/web-of-trust/php-privacy
xcBIBGb2E+YTBSuBBAAjBCMEARQIJE/Dxo80pwP7O+C/WhIfrwAN6gcCS3vgnRKW234NSbFWRkq1
VKP0Un7+1hcLtbvR3VQ8BtNIub4hPvAXmS7wAHt+SBI5KowIQMJR7kowpKRMg+qdTa56ALputpLv
4zBcStVyYMzmiXBKv7NFn9sPqUXysfTdkQfTKaHvWLfsQBj//gcDCPftcmzK+2m/4CRz9XsmVAPd
yD+zUg6OQLi9tOFdqa0mJJU3OuhvZa4+3bdL3u51s011ZWgbJx+UzKS8FejbEuxjr8A7frk1qTgX
j1wwTyrx9BIfRIglRYAgeUVsEu0wWyWF763EjauKfgWpeFuqFZ00zSpOZ3V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sdzk454KFF1HYvs+/DGHtdtv0q4IXmqKtZmF1S
SzK1H9RlRqY9QjWMJ8iGvMRzgF+qyx8Zi8rukycR2A7NVFZjmmqG7+kJwF+eUp5i1FhumlrV9aio
mL5D+mk1aOePCckhWzYxbDDPRy0LlpVzwsA0BBgTCgBkBQJm9hPnFiEEQJtxvik95KJsF8N07gBp
cF4HjEoJEO4AaXBeB4xKORQAAAAAABAAIHNhbHRAb3BlbnBncC5vcmdYrkkcJV/rNkpWHIMrIdnI
WshO6IXzFcQzmlXBJu8blwIbDAAABdUCCKhJiX5fS9eobqxIB9xyTMxbs9/YjXqxg/Rh8nAJMrPW
cXrJizQ9R8jZeigJTAdLCwfOrmiife5ln7q9emEfAu5wAgdOSuFWARFlnrBD6pFt1uNX/eDXKoMi
MPwMofGczg4pIeF6Zf1UKjsLSz2wQsA08tItrl6J/gDNw2FrKyTS2verFg==
=SjkS
-----END PGP PRIVATE KEY BLOCK-----
EOT;
$ecDsaPrivateKey = OpenPGP::decryptPrivateKey($keyData, $passphase);

$keyData = <<<EOT
-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: PHP Privacy v1
Comment: https://github.com/web-of-trust/php-privacy
xYYEZvYT5xYJKwYBBAHaRw8BAQdAz6EScc8aKtK99coL8lAfh34G+XvDiowueuBHNU0JhrT+BwMI
khz8n+/HWZngWO+JsbvxNhniwR/nsIuV9wzTL8oxFs5ryEFE4HS0BFCLKhnsjknTv2nwEzVN7N6E
VHCSvjwarcKGQKlNOLl0yY04QkxH380qTmd1eWVuIFZhbiBOZ3V5ZW4gPG5ndXllbm52MTk4MUBn
bWFpbC5jb20+wsAHBBAWCgB5BQJm9hPnFiEEINYN82B6i6WuHDJXnt6JQmV45pQJEJ7eiUJleOaU
ORQAAAAAABAAIHNhbHRAb3BlbnBncC5vcmeeyPed2COwpofn832zsf7GqOVTYKqx+lHl6SonNgoK
MgIbAwQLBwgJAxUICgUWAAECAwIeAwIZAQAAaw0BAF88l/jCZjM9atPFj/9e4CBnSi6wvHFTcmnU
m/0t5fbUAQAAuybaUsNzdJ4BCSEmgMpZUH3VTo9haF4yo6NTQ5SECc0sTmd1eWVuIFZhbiBOZ3V5
ZW4gPG5ndXllbm52QGl3YXl2aWV0bmFtLmNvbT7CwAQEEBYKAHYFAmb2E+cWIQQg1g3zYHqLpa4c
Mlee3olCZXjmlAkQnt6JQmV45pQ5FAAAAAAAEAAgc2FsdEBvcGVucGdwLm9yZ2wTXRwwELAPHhpj
byBeuRETqMADGNYR+xHuwXRvlzjDAhsDBAsHCAkDFQgKBRYAAQIDAh4DAABKqwEAkK2Kxq2ldXmK
D8IXT00vleQyErlllhAVShUzVtkoX98BAEXpEQcnsKhAb9x7JH3qSAv87MhhxymnQNKFG2bxKDUL
x4sEZvYT5xIKKwYBBAGXVQEFAQEHQCZwzWJxIyMVyozKJFGR4jUFk70hPjlD0ff8sIbohTcFAwAI
B/4HAwiSmYzvfPhVvOA8vh/tskeYjgWcBJrSwstNN9EiiRd4+O08gzcU8BMKiaUY8/DCi25qEyf0
LgmiU4JLDQYvJT1c2U8wXCWlbmUr8Xfno7cvwrIEGBYKAGQFAmb2E+cWIQQg1g3zYHqLpa4cMlee
3olCZXjmlAkQnt6JQmV45pQ5FAAAAAAAEAAgc2FsdEBvcGVucGdwLm9yZyVxOTjEwhAuOGV62ERW
z0hTYzTdx7GLnIS2peI/h75/AhsMAACDowEAzqKJVZSgDPvR00paY+auufbPfDVI7p+zl0qXGqEU
mcYBAEnGTXv34M4dxMkPuz8qAMKX+MiHwiA3vBflJthea+EE
=bBuh
-----END PGP PRIVATE KEY BLOCK-----
EOT;
$edDsaPrivateKey = OpenPGP::decryptPrivateKey($keyData, $passphase);

echo "Sign cleartext message:" . PHP_EOL . PHP_EOL;
$text = <<<EOT
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc felis neque, interdum id iaculis ut, faucibus a ex.
Nam quam tortor, pharetra at dignissim ut, semper nec arcu. Vivamus mollis tortor vitae urna fringilla lacinia id
vel nunc. Ut laoreet pellentesque mattis. Curabitur viverra enim venenatis, mattis velit sed, fringilla lacus. Donec
nulla dui, vestibulum aliquam ultrices hendrerit, euismod iaculis magna. Praesent vitae ipsum id risus feugiat
auctor ac eget tellus.
What we need from the grocery store:
- tofu
- vegetables
- noodles
EOT;
$signedMessage = OpenPGP::signCleartext($text, [
$rsaPrivateKey,
$dsaPrivateKey,
$ecDsaPrivateKey,
$edDsaPrivateKey,
]);
echo $armored = $signedMessage->armor() . PHP_EOL;

echo "Verify signed message:" . PHP_EOL . PHP_EOL;
$verifications = OpenPGP::verify($armored, [
$rsaPrivateKey->toPublic(),
$dsaPrivateKey->toPublic(),
$ecDsaPrivateKey->toPublic(),
$edDsaPrivateKey->toPublic(),
]);
foreach ($verifications as $verification) {
echo "Key ID: {$verification->getKeyID(true)}" . PHP_EOL;
echo "Signature is verified: {$verification->isVerified()}" .
PHP_EOL .
PHP_EOL;
}

echo "Sign detached cleartext message:" . PHP_EOL . PHP_EOL;
$signature = OpenPGP::signDetachedCleartext($text, [
$rsaPrivateKey,
$dsaPrivateKey,
$ecDsaPrivateKey,
$edDsaPrivateKey,
]);
echo $armored = $signature->armor() . PHP_EOL;

echo "Verify detached signature:" . PHP_EOL . PHP_EOL;
$verifications = OpenPGP::verifyDetached($text, $armored, [
$rsaPrivateKey->toPublic(),
$dsaPrivateKey->toPublic(),
$ecDsaPrivateKey->toPublic(),
$edDsaPrivateKey->toPublic(),
]);
foreach ($verifications as $verification) {
echo "Key ID: {$verification->getKeyID(true)}" . PHP_EOL;
echo "Signature is verified: {$verification->isVerified()}" .
PHP_EOL .
PHP_EOL;
}
32 changes: 32 additions & 0 deletions examples/key-generation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types=1);

require_once dirname(__DIR__) . "/vendor/autoload.php";

use OpenPGP\OpenPGP;
use OpenPGP\Common\Helper;
use OpenPGP\Enum\CurveOid;
use OpenPGP\Enum\KeyType;

$userIDs = [
"Nguyen Van Nguyen <[email protected]>",
"Nguyen Van Nguyen <[email protected]>",
];

$passphase = Helper::generatePassword();
echo "Generate passphase: {$passphase}" . PHP_EOL;

echo "Generate RSA private key" . PHP_EOL;
$privateKey = OpenPGP::generateKey($userIDs, $passphase, KeyType::Rsa);
echo $privateKey->armor() . PHP_EOL;

echo "Generate DSA private key" . PHP_EOL;
$privateKey = OpenPGP::generateKey($userIDs, $passphase, KeyType::Dsa);
echo $privateKey->armor() . PHP_EOL;

echo "Generate EcDSA private key" . PHP_EOL;
$privateKey = OpenPGP::generateKey($userIDs, $passphase, KeyType::Ecc, curve: CurveOid::Secp521r1);
echo $privateKey->armor() . PHP_EOL;

echo "Generate EdDSA private key" . PHP_EOL;
$privateKey = OpenPGP::generateKey($userIDs, $passphase, KeyType::Ecc, curve: CurveOid::Ed25519);
echo $privateKey->armor() . PHP_EOL;
Loading

0 comments on commit b924dcf

Please sign in to comment.