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.
- Support data signing & encryption.
- Support key management: key generation, key reading, key decryption.
- Support public-key algorithms: RSA, ECDSA, EdDSA and ECDH.
- Support symmetric ciphers: Blowfish, Twofish, AES, Camellia.
- Support AEAD ciphers: EAX, OCB, GCM.
- Support hash algorithms: SHA-256, SHA-384, SHA-512, SHA-224, SHA3-256, SHA3-512.
- Support compression algorithms: Zip, Zlib.
- Support ECC curves: secp256r1, secp384r1, secp521r1, brainpoolP256r1, brainpoolP384r1, brainpoolP512r1, Curve25519, Curve448, Ed25519, Ed448.
- Support public-key algorithms, symmetric ciphers & hash algorithms for signature verification & message decryption (backward compatibility): DSA, ElGamal, TripleDES, IDEA, CAST5, MD5, SHA-1, RIPEMD-160.
In Dart
or Flutter
project add the dependency:
dependencies:
...
dart_pg:
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;
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;
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;
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]
);
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;
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
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.