diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..309b875 --- /dev/null +++ b/examples/README.md @@ -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) diff --git a/examples/cleartext-signing.php b/examples/cleartext-signing.php new file mode 100755 index 0000000..f8c4aad --- /dev/null +++ b/examples/cleartext-signing.php @@ -0,0 +1,244 @@ +dl:4B21BJ|8[]d|SK"; + +$keyData = <<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; +} diff --git a/examples/key-generation.php b/examples/key-generation.php new file mode 100755 index 0000000..6164a43 --- /dev/null +++ b/examples/key-generation.php @@ -0,0 +1,32 @@ +", + "Nguyen Van Nguyen ", +]; + +$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; diff --git a/examples/key-reading.php b/examples/key-reading.php new file mode 100755 index 0000000..224dfc3 --- /dev/null +++ b/examples/key-reading.php @@ -0,0 +1,215 @@ +getKeyAlgorithm()->name}" . PHP_EOL; +echo "Key fingerprint: {$privateKey->getFingerprint(true)}" . PHP_EOL; +echo "Key is decrypted: {$privateKey->isDecrypted()}" . PHP_EOL; +echo "User ID: {$privateKey->getPrimaryUser()->getUserID()}" . + PHP_EOL . + PHP_EOL; + +$keyData = <<getKeyAlgorithm()->name}" . PHP_EOL; +echo "Key fingerprint: {$privateKey->getFingerprint(true)}" . PHP_EOL; +echo "Key is decrypted: {$privateKey->isDecrypted()}" . PHP_EOL; +echo "User ID: {$privateKey->getPrimaryUser()->getUserID()}" . + PHP_EOL . + PHP_EOL; + +$keyData = <<getKeyAlgorithm()->name}" . PHP_EOL; +echo "Key fingerprint: {$privateKey->getFingerprint(true)}" . PHP_EOL; +echo "Key is decrypted: {$privateKey->isDecrypted()}" . PHP_EOL; +echo "User ID: {$privateKey->getPrimaryUser()->getUserID()}" . + PHP_EOL . + PHP_EOL; + +$keyData = <<getKeyAlgorithm()->name}" . PHP_EOL; +echo "Key fingerprint: {$privateKey->getFingerprint(true)}" . PHP_EOL; +echo "Key is decrypted: {$privateKey->isDecrypted()}" . PHP_EOL; +echo "User ID: {$privateKey->getPrimaryUser()->getUserID()}" . + PHP_EOL . + PHP_EOL; diff --git a/examples/literal-data.php b/examples/literal-data.php new file mode 100755 index 0000000..4a44793 --- /dev/null +++ b/examples/literal-data.php @@ -0,0 +1,278 @@ +3G5=FpdU"; + +$keyData = <<toPublic(), + $dsaPrivateKey->toPublic(), + $ecDsaPrivateKey->toPublic(), + $edDsaPrivateKey->toPublic(), + ], + [$passphase], + [$rsaPrivateKey, $dsaPrivateKey, $ecDsaPrivateKey, $edDsaPrivateKey] +); +echo $armored = $encryptedMessage->armor() . PHP_EOL; + +echo "Decrypt with passphase & verify signatures:" . PHP_EOL . PHP_EOL; +$literalMessage = OpenPGP::decrypt($encryptedMessage, passwords: [$passphase]); +$verifications = $literalMessage->verify([ + $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 "Decrypt with rsa key & verify signatures:" . PHP_EOL . PHP_EOL; +$literalMessage = OpenPGP::decrypt($encryptedMessage, [$rsaPrivateKey]); +$verifications = $literalMessage->verify([ + $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 "Decrypt with dsa key & verify signatures:" . PHP_EOL . PHP_EOL; +$literalMessage = OpenPGP::decrypt($encryptedMessage, [$dsaPrivateKey]); +$verifications = $literalMessage->verify([ + $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 "Decrypt with ec dsa key & verify signatures:" . PHP_EOL . PHP_EOL; +$literalMessage = OpenPGP::decrypt($encryptedMessage, [$ecDsaPrivateKey]); +$verifications = $literalMessage->verify([ + $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 "Decrypt with ed dsa key & verify signatures:" . PHP_EOL . PHP_EOL; +$literalMessage = OpenPGP::decrypt($encryptedMessage, [$edDsaPrivateKey]); +$verifications = $literalMessage->verify([ + $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; +}