victorwesterlund / php-age
PHP wrapper for the age file encryption command line tool
This package is auto-updated.
Last update: 2024-11-20 16:09:39 UTC
README
Encrypt and decrypt files with age from PHP. This library is only a wrapper for the the command line tool, it does not implement the C2CP age specification in PHP.
// Encrypt a file with a generated key $age = new FileEncryption("hello.txt"); $keypair = $age->keygen("hello.key")->encrypt("hello.txt.age"); // Encrypt a file with a specific public key $age->public_key("age1mrf8uana2kan6jsrnf04ywxycvl4nnkzzk3et8rdz6fe6vg7upssclnak7")->encrypt("hello.txt.age");
// Decrypt a file with a key file $age = new FileEncryption("hello.txt.age"); $age->private_key("hello.key")->decrypt("decrypted-hello.txt");
Installation
This library requires PHP 8.1+ and the age command line tool.
- Install the age command line tool
- Install this library with composer
composer require victorwesterlund/php-age
How to use
Import and use the library:
require_once "vendor/autoload.php"; use \Age\FileEncryption;
Encrypt a file
Encrypt a file on disk by passing it to the FileEncryption
constructor
// Relative or absolute path to a file that should be encrypted $age = new FileEncryption("hello.txt");
Note The library will not archive a folder for you. You'll have to
tar
your folder before passing it toFileEncryption
Generated key pair
You can encrypt a file with a generated key pair (age-keygen
) by chaining keygen()
// encrypt() will return the generated keypair as an assoc array $keypair = $age->keygen()->encrypt("hello.txt.age"); // ["public" => "...", "private" => "..."]
You can also save the generated key file to disk by passing an absolute or relative path to keygen()
$keypair = $age->keygen("hello.key)->encrypt("hello.txt.age"); // ["public" => "...", "private" => "..."]
Existing public key
Encrypt a file with an existing public key by chaining the public_key()
method
$keypair = $age->public_key("age1mrf8uana2kan6jsrnf04ywxycvl4nnkzzk3et8rdz6fe6vg7upssclnak7")->encrypt("hello.txt.age"); // ["public" => "age1mrf8uana2kan6jsrnf04ywxycvl4nnkzzk3et8rdz6fe6vg7upssclnak7", "private" => null]
Decrypt a file
Decrypt a file on disk by passing it to the FileEncryption
constructor
// Relative or absolute path to a file that should be decrypted $age = new FileEncryption("hello.txt.age");
Chain private_key()
with an absolute or relative path to the corresponding key file
$age->private_key("hello.key")->decrypt("decrypted-hello.txt"); // true
Optional features
Enable PEM encoding by chaining the optional armor()
method
$keypair = $age->armor()->keygen()->encrypt("hello.txt.age");