Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
Signed-off-by: Nguyen Van Nguyen <nguyennv1981@gmail.com>
  • Loading branch information
nguyennv committed Dec 13, 2024
2 parents 5362feb + 5517e74 commit e7864d8
Show file tree
Hide file tree
Showing 53 changed files with 1,275 additions and 1,151 deletions.
8 changes: 0 additions & 8 deletions examples/cleartext-signing.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

$keyData = <<<EOT
-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: PHP Privacy v2
Comment: https://github.com/web-of-trust/php-privacy
xcMGBGbzgU0BCAC+jS+ngfl33Ug9lmmcK5/skfFvQlUn4vtmSnUF5B4ohIX3CEkvCjdtinJlyUHL
MTgqHv3s64WCfMIwJUeRxXPP/g9vtOB2g0VFn695kZ80K8LqBawxEke+vFeXkwxY0hZC8I9dDpLK
Expand Down Expand Up @@ -73,8 +71,6 @@

$keyData = <<<EOT
-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: PHP Privacy v2
Comment: https://github.com/web-of-trust/php-privacy
xcBIBGbzgU4TBSuBBAAjBCMEAGIhXtcV0Xo0x84qunT4ndhgrlKZcXgXYB9dhoPpci/XSsAU2Y6Y
BXOtXZvtj82Ht1GNHa5QgYO85y556aue6rK3AWUkCOZoKvjQ1kNJZwqW7iaXaj1N/cfFn1WQf4+4
Expand Down Expand Up @@ -108,8 +104,6 @@

$keyData = <<<EOT
-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: PHP Privacy v2
Comment: https://github.com/web-of-trust/php-privacy
xX0GZvOBThsAAAAgNgTM0VxUPpT+EPeltZCRrLVp4PQtdVLqmrEbln7nqkX+HQcLAwgpPq31t9Lk
AOBxvjCfxzD7nRJShX2aDSjNwsQ9xcUoUQ4Xtho0KFR3KD6xWW/Ud0y/PStRBLeqoPjyf8LVfwiq
Expand Down Expand Up @@ -138,8 +132,6 @@

$keyData = <<<EOT
-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: PHP Privacy v2
Comment: https://github.com/web-of-trust/php-privacy
xa8GZvOBTxwAAAA5bkWce7vfRJjxiLiP1G9EJH0Hp+0gKywFeY2/n+9z3FASQZ0aPiyudnXS3svo
XKXtCpibchwzb0CA/h0HCwMIb7Ms2LezV3fgJzYF+rs24aoAuyVsm+DI0+F+fI3Mdr6CfwP5JGv4
Expand Down
18 changes: 5 additions & 13 deletions examples/key-decrypting.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

use OpenPGP\OpenPGP;

$passphase = 'Ax@2bGh;SxD&"A_;El%mPIvLx_!#3Aik';
$passphrase = 'Ax@2bGh;SxD&"A_;El%mPIvLx_!#3Aik';

$keyData = <<<EOT
-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: PHP Privacy v2
Comment: https://github.com/web-of-trust/php-privacy
xcMGBGbzftYBCADjibutkEy7HZgEPkJHzQuY3OLDTviCJABAm7BUAx0qeVIWJT7+53P1g/3vRr1q
rVBbQxrLBvS8Haij+SStqhTiYkjWmV5OTwY46uSd65cIuJUardzojbRDSLaODSn7c8J2yVLRlkls
Expand Down Expand Up @@ -70,7 +68,7 @@
-----END PGP PRIVATE KEY BLOCK-----
EOT;
echo "Decrypt RSA private key" . PHP_EOL;
$privateKey = OpenPGP::decryptPrivateKey($keyData, $passphase);
$privateKey = OpenPGP::decryptPrivateKey($keyData, $passphrase);
echo "Key algorithm: {$privateKey->getKeyAlgorithm()->name}" . PHP_EOL;
echo "Key version: {$privateKey->getVersion()}" . PHP_EOL;
echo "Key fingerprint: {$privateKey->getFingerprint(true)}" . PHP_EOL;
Expand All @@ -81,8 +79,6 @@

$keyData = <<<EOT
-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: PHP Privacy v2
Comment: https://github.com/web-of-trust/php-privacy
xcBIBGbzftYTBSuBBAAjBCMEAWHmrzLEzuTRVDQQTvCZdm/RnY/M9rzN8qzpp4lBAFSDhYY118hv
Jp2Ax0rsD5Qi8tMeTAdFef08HqzP4eeNSHu1AZi518zPlyv3ADpZ3jQ89Sw+WwR17a7+0ZuEV8Jd
Expand Down Expand Up @@ -113,7 +109,7 @@
-----END PGP PRIVATE KEY BLOCK-----
EOT;
echo "Decrypt Ecc private key" . PHP_EOL;
$privateKey = OpenPGP::decryptPrivateKey($keyData, $passphase);
$privateKey = OpenPGP::decryptPrivateKey($keyData, $passphrase);
echo "Key algorithm: {$privateKey->getKeyAlgorithm()->name}" . PHP_EOL;
echo "Key version: {$privateKey->getVersion()}" . PHP_EOL;
echo "Key fingerprint: {$privateKey->getFingerprint(true)}" . PHP_EOL;
Expand All @@ -124,8 +120,6 @@

$keyData = <<<EOT
-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: PHP Privacy v2
Comment: https://github.com/web-of-trust/php-privacy
xX0GZvN+1xsAAAAgMtES26hjSSQoUx6STK6IMe64fLvldCRkg7w50pfRxqD+HQcLAwjPsmWimBb8
n+B+mPc9X1kryrwErHX2103ACvuNq1yGPKvU6q3HygdPL8n7DHv/U5S88R6KQnNQfaIRGsYRpye+
Expand All @@ -151,7 +145,7 @@
-----END PGP PRIVATE KEY BLOCK-----
EOT;
echo "Decrypt Curve25519 private key" . PHP_EOL;
$privateKey = OpenPGP::decryptPrivateKey($keyData, $passphase);
$privateKey = OpenPGP::decryptPrivateKey($keyData, $passphrase);
echo "Key algorithm: {$privateKey->getKeyAlgorithm()->name}" . PHP_EOL;
echo "Key version: {$privateKey->getVersion()}" . PHP_EOL;
echo "Key fingerprint: {$privateKey->getFingerprint(true)}" . PHP_EOL;
Expand All @@ -162,8 +156,6 @@

$keyData = <<<EOT
-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: PHP Privacy v2
Comment: https://github.com/web-of-trust/php-privacy
xa8GZvN+1xwAAAA5Hf47PIbsLCoImil+MuXWGvUvzElvjIiq3fJrRIf+k1cXre22GEKF01R2q1Ma
mkSDoofsPPeNeUUA/h0HCwMITySmmq2jzrTgYtxFzyFzKGtQNbFTZXcTXmmxqT4UjfG+S5s5tFYy
Expand Down Expand Up @@ -195,7 +187,7 @@
-----END PGP PRIVATE KEY BLOCK-----
EOT;
echo "Decrypt Curve448 private key" . PHP_EOL;
$privateKey = OpenPGP::decryptPrivateKey($keyData, $passphase);
$privateKey = OpenPGP::decryptPrivateKey($keyData, $passphrase);
echo "Key algorithm: {$privateKey->getKeyAlgorithm()->name}" . PHP_EOL;
echo "Key version: {$privateKey->getVersion()}" . PHP_EOL;
echo "Key fingerprint: {$privateKey->getFingerprint(true)}" . PHP_EOL;
Expand Down
12 changes: 6 additions & 6 deletions examples/key-generation.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@
"Nguyen Van Nguyen <nguyennv1981@gmail.com>",
"Nguyen Van Nguyen <nguyennv@iwayvietnam.com>",
];
$passphase = Helper::generatePassword();
echo "Generate passphase: {$passphase}" . PHP_EOL;
$passphrase = Helper::generatePassword();
echo "Generate passphrase: {$passphrase}" . PHP_EOL;

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

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

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

echo "Generate Curve448 private key" . PHP_EOL;
$privateKey = OpenPGP::generateKey($userIDs, $passphase, KeyType::Curve448);
$privateKey = OpenPGP::generateKey($userIDs, $passphrase, KeyType::Curve448);
echo $privateKey->armor() . PHP_EOL;
8 changes: 0 additions & 8 deletions examples/key-reading.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

$keyData = <<<EOT
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: PHP Privacy v2
Comment: https://github.com/web-of-trust/php-privacy
xsBNBGbzbxEBCADJmISfDnVztCfrvKfr6rn4faGVReCPWET+ZDQBzjCieqGikm2dHFYZXkU2Mp5g
h+hOx0YZylAI1sOBxf6AxbPysiT5AIllV3fXghSXoj7TqVuzk3pVmyajhPgmUlaPQbhWH4BdCUD7
Expand Down Expand Up @@ -53,8 +51,6 @@

$keyData = <<<EOT
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: PHP Privacy v2
Comment: https://github.com/web-of-trust/php-privacy
xpMEZvNvEhMFK4EEACMEIwQAhYLZkwYSvw0U+DPzSGOQybF7GZ3OoZpk6kXgyn2CJEk8jLAdvnNG
2lZPHgCbm7PCMorp+lDpwQgRlGOPAWTkoa8Ao6CC/WPqEgu8c9YbWKrMaB1MTqlzsKrpbuOQC1mS
Expand Down Expand Up @@ -89,8 +85,6 @@

$keyData = <<<EOT
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: PHP Privacy v2
Comment: https://github.com/web-of-trust/php-privacy
xioGZvNvEhsAAAAgHVrZBj0nDKrbf0Tu8fWOjRiPaMulfxm1Mt7lEkY4gZzCuwYfGwgAAABcBQJm
828SIiEGoLT+uXrwY1ghIpvsuo3SXJzh9HEg0y3OT6Vr6OoCSYMJEKC0/rl68GNYAhsDAwsHCQQi
Expand Down Expand Up @@ -121,8 +115,6 @@

$keyData = <<<EOT
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: PHP Privacy v2
Comment: https://github.com/web-of-trust/php-privacy
xkMGZvNvExwAAAA5yWvpY7TWFFw8urr787Qb4cDyF9X59bJ6PjDvL2PoibX1jmU6Fuf30HSZnSLq
CAkvFx//A+rAbeEAwsA9Bh8cCgAAAFwFAmbzbxMiIQYwBf+MyThKw0UAWILFQZ6Yjv387mZGsM5P
Expand Down
26 changes: 9 additions & 17 deletions examples/literal-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
use OpenPGP\Common\Config;
use OpenPGP\Enum\SymmetricAlgorithm;

$passphase = "NU=WM<;ev3(^^M@)dIt,O|9k*<xLpv7v";
$passphrase = "NU=WM<;ev3(^^M@)dIt,O|9k*<xLpv7v";

$keyData = <<<EOT
-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: PHP Privacy v2
Comment: https://github.com/web-of-trust/php-privacy
xcMFBGbzljMBCADB4KjKfIR4GshZ3cBQAXCZgKgGKl9p313/K88LjDEJ3MWUCx07ww+fZzmOXh2V
vIGPt/QwDuoHmFX3B/HCesK5aIbda9CuWZrt2srRBxO33PKYVbY/Fh3Z4OU/xOacB3mHEq/C/+H9
Expand Down Expand Up @@ -71,12 +69,10 @@
=HDCL
-----END PGP PRIVATE KEY BLOCK-----
EOT;
$rsaPrivateKey = OpenPGP::decryptPrivateKey($keyData, $passphase);
$rsaPrivateKey = OpenPGP::decryptPrivateKey($keyData, $passphrase);

$keyData = <<<EOT
-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: PHP Privacy v2
Comment: https://github.com/web-of-trust/php-privacy
xcBHBGbzljQTBSuBBAAjBCMEAbbJmwFEE6Mwhn/S9gFoSwnY2vj9iPDA9ooNtKdg5BqL0A3fQ0y+
+zG4pu/YtGiQwjbrZiyWqAipcPH7w/3q88RkADcqkIjBreSHhVspX5CiL3RBmrqBXzo4L+xZ1ejj
Expand Down Expand Up @@ -106,12 +102,10 @@
=pP8r
-----END PGP PRIVATE KEY BLOCK-----
EOT;
$eccPrivateKey = OpenPGP::decryptPrivateKey($keyData, $passphase);
$eccPrivateKey = OpenPGP::decryptPrivateKey($keyData, $passphrase);

$keyData = <<<EOT
-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: PHP Privacy v2
Comment: https://github.com/web-of-trust/php-privacy
xX0GZvOWNBsAAAAgsDfIqP0lv8+V4Eaj0omfm+FPDcMeVeEak7qabHd/pcn+HQcLAwhRaGxMbGEj
KeAjp9Kh/3vzuZ8Xfdj8w8je6o5USELRl4rD0RO3MDrppA2pfRbSp1kn/Xvfuzggou+s4l++cR37
Expand All @@ -136,12 +130,10 @@
=oH6u
-----END PGP PRIVATE KEY BLOCK-----
EOT;
$curve25519PrivateKey = OpenPGP::decryptPrivateKey($keyData, $passphase);
$curve25519PrivateKey = OpenPGP::decryptPrivateKey($keyData, $passphrase);

$keyData = <<<EOT
-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: PHP Privacy v2
Comment: https://github.com/web-of-trust/php-privacy
xa8GZvOWNRwAAAA5EN1vmT1zBRrWb3u7WplWBLMom6ou9KLfVMa+qHNqAEGlyiBr3ik4wD8UOO8t
ETP76oGUaeS5PXaA/h0HCwMI7kHpr3/alWrgIY5ni6U2mqvWywD89BwZpx+Npit0FTmE/XzIxX+Z
Expand Down Expand Up @@ -172,7 +164,7 @@
=aUu5
-----END PGP PRIVATE KEY BLOCK-----
EOT;
$curve448PrivateKey = OpenPGP::decryptPrivateKey($keyData, $passphase);
$curve448PrivateKey = OpenPGP::decryptPrivateKey($keyData, $passphrase);

echo "Sign & encrypt literal data message with AES128 cipher:" .
PHP_EOL .
Expand All @@ -184,13 +176,13 @@
$rsaPrivateKey->toPublic(),
$eccPrivateKey->toPublic(),
],
[$passphase],
[$passphrase],
[$rsaPrivateKey, $eccPrivateKey, $curve25519PrivateKey, $curve448PrivateKey]
);
echo $armored = $encryptedMessage->armor() . PHP_EOL;

echo "Decrypt with passphase & verify signatures:" . PHP_EOL . PHP_EOL;
$literalMessage = OpenPGP::decryptMessage($armored, passwords: [$passphase]);
echo "Decrypt with passphrase & verify signatures:" . PHP_EOL . PHP_EOL;
$literalMessage = OpenPGP::decryptMessage($armored, passwords: [$passphrase]);
$verifications = $literalMessage->verify([
$rsaPrivateKey->toPublic(),
$eccPrivateKey->toPublic(),
Expand Down Expand Up @@ -249,7 +241,7 @@
$curve25519PrivateKey->toPublic(),
$curve448PrivateKey->toPublic(),
],
[$passphase],
[$passphrase],
[$rsaPrivateKey, $eccPrivateKey, $curve25519PrivateKey, $curve448PrivateKey]
);
echo $armored = $encryptedMessage->armor() . PHP_EOL;
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Armor.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public static function encode(
? implode(
Helper::EOL,
array_map(
static fn($hashAlgo) => "Hash: $hashAlgo",
static fn ($hash) => "Hash: $hash",
$hashAlgos
)
) .
Expand Down
53 changes: 30 additions & 23 deletions src/Common/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
AeadAlgorithm,
CompressionAlgorithm,
HashAlgorithm,
PresetRFC,
SymmetricAlgorithm
};

Expand Down Expand Up @@ -40,7 +41,9 @@ final class Config

private static CompressionAlgorithm $preferredCompression = CompressionAlgorithm::Uncompressed;

private static AeadAlgorithm $preferredAead = AeadAlgorithm::Gcm;
private static AeadAlgorithm $preferredAead = AeadAlgorithm::Ocb;

private static PresetRFC $presetRFC = PresetRFC::RFC4880;

private static int $s2kItCount = 224;

Expand All @@ -54,8 +57,6 @@ final class Config

private static bool $aeadProtect = false;

private static bool $useV6Key = false;

private static bool $allowUnauthenticated = false;

private static bool $showVersion = true;
Expand Down Expand Up @@ -148,6 +149,32 @@ public static function setPreferredAead(AeadAlgorithm $algo): void
self::$preferredAead = $algo;
}

/**
* Get preset RFC.
*
* @return PresetRFC
*/
public static function presetRFC(): PresetRFC
{
return self::$presetRFC;
}

/**
* Preset RFC4880.
*/
public static function presetRFC4880(): void
{
self::$presetRFC = PresetRFC::RFC4880;
}

/**
* Preset RFC9580.
*/
public static function presetRFC9580(): void
{
self::$presetRFC = PresetRFC::RFC9580;
}

/**
* Get S2K iteration count byte.
*
Expand Down Expand Up @@ -278,26 +305,6 @@ public static function setAeadProtect(bool $protect): void
self::$aeadProtect = $protect;
}

/**
* Get use V6 key.
*
* @return bool
*/
public static function useV6Key(): bool
{
return self::$useV6Key;
}

/**
* Set use V6 key.
*
* @param bool $useV6Key
*/
public static function setUseV6Key(bool $useV6Key): void
{
self::$useV6Key = $useV6Key;
}

/**
* Get allow decryption of messages without integrity protection.
*
Expand Down
6 changes: 3 additions & 3 deletions src/Common/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public static function stringToKey(
public static function computeChecksum(string $text): string
{
$sum = array_sum(
array_map(static fn($char) => ord($char), str_split($text))
array_map(static fn ($char) => ord($char), str_split($text))
);
return pack("n", $sum & 0xffff);
}
Expand All @@ -169,7 +169,7 @@ public static function generatePassword(int $length = 32): string
{
return preg_replace_callback(
"/\*/u",
static fn() => chr(random_int(40, 126)),
static fn () => chr(random_int(40, 126)),
str_repeat("*", $length)
);
}
Expand All @@ -184,7 +184,7 @@ public static function removeTrailingSpaces(string $text): string
{
$lines = explode(self::EOL, $text);
$lines = array_map(
static fn($line) => rtrim($line, self::SPACES),
static fn ($line) => rtrim($line, self::SPACES),
$lines
);
return implode(self::EOL, $lines);
Expand Down
Loading

0 comments on commit e7864d8

Please sign in to comment.