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 RFC4880, RFC5581, RFC6637, parts of RFC4880bis.

Features

Getting started

In Dart or Flutter project add the dependency:

dependencies:
  ...
  dart_pg:

Usage

Encrypt and decrypt data with a password

const text = 'Hello Dart PG!';
const password = 'secret stuff';

final encryptedMessage = await OpenPGP.encrypt(
    OpenPGP.createTextMessage(text), passwords: [password]
);
final encrypted = encryptedMessage.armor();
final decryptedMessage = await OpenPGP.decrypt(
    OpenPGP.readMessage(encrypted), passwords: [password]
);
final decrypted = decryptedMessage.armor();

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 text = 'Hello Dart PG!';
const passphrase = 'secret stuff';
const armoredPublicKey = '-----BEGIN PGP PUBLIC KEY BLOCK-----';
const armoredPrivateKey = '-----BEGIN PGP PRIVATE KEY BLOCK-----';

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

final encryptedMessage = await OpenPGP.encrypt(
    OpenPGP.createTextMessage(text), encryptionKeys: [publicKey]
);
final encrypted = encryptedMessage.armor();

final decryptedMessage = await OpenPGP.decrypt(
    OpenPGP.readMessage(encrypted), decryptionKeys: [privateKey]
);
final decrypted = decryptedMessage.armor();

Sign message & encrypt with multiple public keys:

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

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

final encryptedMessage = await OpenPGP.encrypt(
    OpenPGP.createTextMessage(text),
    encryptionKeys: publicKeys,
    signingKeys: [privateKey],
);
final encrypted = encryptedMessage.armor();

Sign and verify cleartext

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

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

final signedMessage = await OpenPGP.sign(text, signingKeys: [privateKey]);
final signed = signedMessage.armor();

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

Detached sign and verify cleartext

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

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

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

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

Generate new key pair

rsa type:

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

dsa type (uses DSA algorithm for signing & ElGamal algorithm for encryption):

const passphrase = 'secret stuff';
final userID = [name, '($comment)', '<$email>'].join(' ');
final privateKey = await OpenPGP.generateKey(
    [userID],
    passphrase,
    type: KeyGenerationType.dsa,
    dhKeySize: DHKeySize.l2048n224,
);
final publicKey = privateKey.toPublic;

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

const passphrase = 'secret stuff';
final userID = [name, '($comment)', '<$email>'].join(' ');
final privateKey = await OpenPGP.generateKey(
    [userID],
    passphrase,
    type: KeyGenerationType.ecdsa,
    curve: CurveInfo.secp521r1,
);
final publicKey = privateKey.toPublic;

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

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

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.

Libraries

dart_pg