Skip to content

web-of-trust/dart-pg

Repository files navigation

Dart PG (Dart Privacy Guard) - The OpenPGP library in Dart language

Dart PG is an implementation of the OpenPGP standard in Dart language. It implements RFC 9580 and provides encryption with public key or symmetric cryptographic algorithms, digital signatures, compression, and key management.

Features

Getting started

In Dart or Flutter project add the dependency:

dependencies:
  ...
  dart_pg:

Usage

Encrypt and decrypt data with a password

const literalText = 'Hello Dart Privacy Guard!';
const password = 'secret stuff';

final encryptedMessage = OpenPGP.encryptCleartext(
    literalText, passwords: [password]
);
final armored = encryptedMessage.armor();
final literalMessage = OpenPGP.decrypt(
    armored, passwords: [password]
);
final literalData = literalMessage.literalData;

Encrypt and decrypt data with PGP keys

Encryption will use the algorithm preferred by the public (encryption) key (defaults to aes256 for keys generated), and decryption will use the algorithm used for encryption.

const literalText = 'Hello Dart Privacy Guard!';
const passphrase = 'secret stuff';
const armoredPublicKey = '-----BEGIN PGP PUBLIC KEY BLOCK-----';
const armoredPrivateKey = '-----BEGIN PGP PRIVATE KEY BLOCK-----';

final publicKey = OpenPGP.readPublicKey(armoredPublicKey);
final privateKey = OpenPGP.decryptPrivateKey(armoredPrivateKey, passphrase);

final encryptedMessage = OpenPGP.encryptCleartext(
    literalText, encryptionKeys: [publicKey]
);
final armored = encryptedMessage.armor();

final literalMessage = OpenPGP.decrypt(
    armored, decryptionKeys: [privateKey]
);
final literalData = literalMessage.literalData;

Sign message & encrypt with multiple public keys:

final literalText = 'Hello Dart Privacy Guard!';
const passphrase = 'secret stuff';
const armoredPublicKeys = ['-----BEGIN PGP PUBLIC KEY BLOCK-----'];
const armoredPrivateKey = '-----BEGIN PGP PRIVATE KEY BLOCK-----';

final publicKeys = armoredPublicKeys.map((armored) => OpenPGP.readPublicKey(armored));
final privateKey = OpenPGP.decryptPrivateKey(armoredPrivateKey, passphrase);

final encryptedMessage = OpenPGP.encryptCleartext(
    literalText,
    encryptionKeys: publicKeys,
    signingKeys: [privateKey],
);
final armored = encryptedMessage.armor();

final literalMessage = OpenPGP.decrypt(
    armored, decryptionKeys: [privateKey]
);
final literalData = literalMessage.literalData;

Sign and verify cleartext

const text = 'Hello Dart Privacy Guard!';
const passphrase = 'secret stuff';
const armoredPublicKey = '-----BEGIN PGP PUBLIC KEY BLOCK-----';
const armoredPrivateKey = '-----BEGIN PGP PRIVATE KEY BLOCK-----';

final publicKey = OpenPGP.readPublicKey(armoredPublicKey);
final privateKey = OpenPGP.decryptPrivateKey(armoredPrivateKey, passphrase);

final signedMessage = OpenPGP.signCleartext(text, signingKeys: [privateKey]);
final armored = signedMessage.armor();

final verifiedMessage = OpenPGP.verify(armored, verificationKeys: [publicKey]);
final verifications = verifiedMessage.verifications;

Detached sign and verify cleartext

const text = 'Hello Dart Privacy Guard!';
const passphrase = 'secret stuff';
const armoredPublicKey = '-----BEGIN PGP PUBLIC KEY BLOCK-----';
const armoredPrivateKey = '-----BEGIN PGP PRIVATE KEY BLOCK-----';

final publicKey = OpenPGP.readPublicKey(armoredPublicKey);
final privateKey = OpenPGP.decryptPrivateKey(armoredPrivateKey, passphrase);

final signature = OpenPGP.signDetachedCleartext(text, signingKeys: [privateKey]);
final armored = signature.armor();

final verifications = OpenPGP.verifyDetached(
    text, armored, verificationKeys: [publicKey]
);

Generate new key pair

rsa type:

const passphrase = 'secret stuff';
final userID = [name, '($comment)', '<$email>'].join(' ');
final privateKey = OpenPGP.generateKey(
    [userID],
    passphrase,
    type: KeyType.rsa,
    rsaKeySize: RSAKeySize.normal,
);
final publicKey = privateKey.publicKey;

ecdsa type (uses ECDSA algorithm for signing & ECDH algorithm for encryption): Possible values for curve are secp256k1, secp384r1, secp521r1, brainpoolp256r1, brainpoolp384r1, brainpoolp512r1

const passphrase = 'secret stuff';
final userID = [name, '($comment)', '<$email>'].join(' ');
final privateKey = OpenPGP.generateKey(
    [userID],
    passphrase,
    type: KeyType.ecc,
    curve: Ecc.secp521r1,
);
final publicKey = privateKey.publicKey;

eddsa type (uses EdDSA legacy algorithm with ed25519 for signing & ECDH algorithm with curve25519 for encryption):

const passphrase = 'secret stuff';
final userID = [name, '($comment)', '<$email>'].join(' ');
final privateKey = OpenPGP.generateKey(
    [userID],
    passphrase,
    type: KeyType.ecc,
    curve: Ecc.ed25519,
);
final publicKey = privateKey.publicKey;

Curve25519 key type (uses Ed25519 algorithm for signing & X25519 algorithm for encryption):

const passphrase = 'secret stuff';
final userID = [name, '($comment)', '<$email>'].join(' ');
final privateKey = OpenPGP.generateKey(
    [userID],
    passphrase,
    type: KeyType.curve25519,
);
final publicKey = privateKey.publicKey;

Curve448 key type (uses Ed448 algorithm for signing & X448 algorithm for encryption):

const passphrase = 'secret stuff';
final userID = [name, '($comment)', '<$email>'].join(' ');
final privateKey = OpenPGP.generateKey(
    [userID],
    passphrase,
    type: KeyType.curve448,
);
final publicKey = privateKey.publicKey;

Development

To create your own build of the library, just run the following command after cloning the git repo. This will download all dependencies, run the tests

dart pub get && dart test

Licensing

BSD 3-Clause

For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.