Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
Signed-off-by: Nguyen Van Nguyen <[email protected]>
  • Loading branch information
nguyennv committed Dec 10, 2024
2 parents cb93f02 + ba5ef48 commit aa5ee96
Show file tree
Hide file tree
Showing 245 changed files with 17,422 additions and 11,742 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2022-present, Nguyen Van Nguyen and other contributors. All rights reserved.
Copyright (c) 2024-present by Dart Privacy Guard project. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Expand Down
160 changes: 89 additions & 71 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
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](https://www.rfc-editor.org/rfc/rfc4880), [RFC5581](https://www.rfc-editor.org/rfc/rfc5581), [RFC6637](https://www.rfc-editor.org/rfc/rfc6637),
parts of [RFC4880bis](https://datatracker.ietf.org/doc/html/draft-ietf-openpgp-rfc4880bis).
It implements [RFC 9580](https://www.rfc-editor.org/rfc/rfc9580) and
provides encryption with public key or symmetric cryptographic algorithms,
digital signatures, compression, and key management.

## Features
* Support data signing & encryption.
* Support key management: key generation, key reading, key decryption.
* Support public-key algorithms: [RSA](https://en.wikipedia.org/wiki/RSA_(cryptosystem)),
[DSA](https://en.wikipedia.org/wiki/Digital_Signature_Algorithm),
[ElGamal](https://en.wikipedia.org/wiki/ElGamal_encryption),
[ECDSA](https://en.wikipedia.org/wiki/Elliptic_Curve_Digital_Signature_Algorithm),
[EdDSA](https://en.wikipedia.org/wiki/EdDSA)
* Support public-key algorithms: [RSA](https://www.rfc-editor.org/rfc/rfc3447),
[ECDSA](https://www.rfc-editor.org/rfc/rfc6979),
[EdDSA](https://www.rfc-editor.org/rfc/rfc8032)
and [ECDH](https://en.wikipedia.org/wiki/Elliptic-curve_Diffie%E2%80%93Hellman).
* Support symmetric ciphers: 3DES, IDEA, CAST5, Blowfish, Twofish,
[AES](https://en.wikipedia.org/wiki/Advanced_Encryption_Standard),
[Camellia](https://en.wikipedia.org/wiki/Camellia_(cipher)).
* Support AEAD algorithms: [EAX](https://www.cs.ucdavis.edu/~rogaway/papers/eax.pdf), [OCB](https://tools.ietf.org/html/rfc7253), [GCM](https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf).
* Support hash algorithms: MD5, SHA-1, RIPEMD-160, SHA-256, SHA-384, SHA-512, SHA-224.
* Support compression algorithms: ZIP, ZLIB.
* Support symmetric ciphers: Blowfish, Twofish,
[AES](https://www.rfc-editor.org/rfc/rfc3394),
[Camellia](https://www.rfc-editor.org/rfc/rfc3713).
* Support AEAD ciphers: [EAX](https://seclab.cs.ucdavis.edu/papers/eax.pdf),
[OCB](https://tools.ietf.org/html/rfc7253),
[GCM](https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38d.pdf).
* Support hash algorithms: SHA-256, SHA-384, SHA-512, SHA-224, SHA3-256, SHA3-512.
* Support compression algorithms: Zip, Zlib.
* Support [ECC](https://en.wikipedia.org/wiki/Elliptic-curve_cryptography) curves:
[secP256k1, secP384r1, secP521r1](https://www.rfc-editor.org/rfc/rfc6090),
[secp256r1, secp384r1, secp521r1](https://www.rfc-editor.org/rfc/rfc6090),
[brainpoolP256r1, brainpoolP384r1, brainpoolP512r1](https://www.rfc-editor.org/rfc/rfc5639),
[curve25519](https://www.rfc-editor.org/rfc/rfc7748), [ed25519](https://www.rfc-editor.org/rfc/rfc8032),
[prime256v1](https://www.secg.org/sec2-v2.pdf).
[Curve25519, Curve448](https://www.rfc-editor.org/rfc/rfc7748),
[Ed25519, Ed448](https://www.rfc-editor.org/rfc/rfc8032).
* 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.

## Getting started
In `Dart` or `Flutter` project add the dependency:
Expand All @@ -37,149 +41,163 @@ dependencies:
### Encrypt and decrypt data with a password
```dart
const text = 'Hello Dart PG!';
const literalText = 'Hello Dart Privacy Guard!';
const password = 'secret stuff';

final encryptedMessage = await OpenPGP.encrypt(
OpenPGP.createTextMessage(text), passwords: [password]
final encryptedMessage = OpenPGP.encryptCleartext(
literalText, passwords: [password]
);
final encrypted = encryptedMessage.armor();
final decryptedMessage = await OpenPGP.decrypt(
OpenPGP.readMessage(encrypted), passwords: [password]
final armored = encryptedMessage.armor();
final literalMessage = OpenPGP.decrypt(
armored, passwords: [password]
);
final decrypted = decryptedMessage.armor();
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.
```dart
const text = 'Hello Dart PG!';
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 = await OpenPGP.readPublicKey(armoredPublicKey);
final privateKey = await OpenPGP.decryptPrivateKey(armoredPrivateKey, passphrase);
final publicKey = OpenPGP.readPublicKey(armoredPublicKey);
final privateKey = OpenPGP.decryptPrivateKey(armoredPrivateKey, passphrase);
final encryptedMessage = await OpenPGP.encrypt(
OpenPGP.createTextMessage(text), encryptionKeys: [publicKey]
final encryptedMessage = OpenPGP.encryptCleartext(
literalText, encryptionKeys: [publicKey]
);
final encrypted = encryptedMessage.armor();
final armored = encryptedMessage.armor();
final decryptedMessage = await OpenPGP.decrypt(
OpenPGP.readMessage(encrypted), decryptionKeys: [privateKey]
final literalMessage = OpenPGP.decrypt(
armored, decryptionKeys: [privateKey]
);
final decrypted = decryptedMessage.armor();
final literalData = literalMessage.literalData;
```

Sign message & encrypt with multiple public keys:
```dart
final text = 'Hello Dart PG!';
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 = await Future.wait(
armoredPublicKeys.map((armored) => OpenPGP.readPublicKey(armored))
);
final privateKey = await OpenPGP.decryptPrivateKey(armoredPrivateKey, passphrase);
final publicKeys = armoredPublicKeys.map((armored) => OpenPGP.readPublicKey(armored));
final privateKey = OpenPGP.decryptPrivateKey(armoredPrivateKey, passphrase);
final encryptedMessage = await OpenPGP.encrypt(
OpenPGP.createTextMessage(text),
final encryptedMessage = OpenPGP.encryptCleartext(
literalText,
encryptionKeys: publicKeys,
signingKeys: [privateKey],
);
final encrypted = encryptedMessage.armor();
final armored = encryptedMessage.armor();
final literalMessage = OpenPGP.decrypt(
armored, decryptionKeys: [privateKey]
);
final literalData = literalMessage.literalData;
```

### Sign and verify cleartext
```dart
const text = 'Hello Dart PG!';
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 = await OpenPGP.readPublicKey(armoredPublicKey);
final privateKey = await OpenPGP.decryptPrivateKey(armoredPrivateKey, passphrase);
final publicKey = OpenPGP.readPublicKey(armoredPublicKey);
final privateKey = OpenPGP.decryptPrivateKey(armoredPrivateKey, passphrase);
final signedMessage = await OpenPGP.sign(text, signingKeys: [privateKey]);
final signed = signedMessage.armor();
final signedMessage = OpenPGP.signCleartext(text, signingKeys: [privateKey]);
final armored = signedMessage.armor();
final verifiedMessage = await OpenPGP.verify(signed, verificationKeys: [publicKey]);
final verifiedMessage = OpenPGP.verify(armored, verificationKeys: [publicKey]);
final verifications = verifiedMessage.verifications;
```

### Detached sign and verify cleartext
```dart
const text = 'Hello Dart PG!';
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 = await OpenPGP.readPublicKey(armoredPublicKey);
final privateKey = await OpenPGP.decryptPrivateKey(armoredPrivateKey, passphrase);
final publicKey = OpenPGP.readPublicKey(armoredPublicKey);
final privateKey = OpenPGP.decryptPrivateKey(armoredPrivateKey, passphrase);
final signature = await OpenPGP.signDetached(text, signingKeys: [privateKey]);
final signature = OpenPGP.signDetachedCleartext(text, signingKeys: [privateKey]);
final armored = signature.armor();
final cleartextMessage = await OpenPGP.verifyDetached(
final verifications = OpenPGP.verifyDetached(
text, armored, verificationKeys: [publicKey]
);
final verifications = cleartextMessage.verifications;
```

### Generate new key pair
rsa type:
```dart
const passphrase = 'secret stuff';
final userID = [name, '($comment)', '<$email>'].join(' ');
final privateKey = await OpenPGP.generateKey(
final privateKey = OpenPGP.generateKey(
[userID],
passphrase,
type: KeyGenerationType.rsa,
rsaKeySize: RSAKeySize.s4096,
type: KeyType.rsa,
rsaKeySize: RSAKeySize.normal,
);
final publicKey = privateKey.toPublic;
final publicKey = privateKey.publicKey;
```

dsa type (uses DSA algorithm for signing & ElGamal algorithm for encryption):
ecdsa type (uses ECDSA algorithm for signing & ECDH algorithm for encryption): Possible values for curve are
secp256k1, secp384r1, secp521r1, brainpoolp256r1, brainpoolp384r1, brainpoolp512r1
```dart
const passphrase = 'secret stuff';
final userID = [name, '($comment)', '<$email>'].join(' ');
final privateKey = await OpenPGP.generateKey(
final privateKey = OpenPGP.generateKey(
[userID],
passphrase,
type: KeyGenerationType.dsa,
dhKeySize: DHKeySize.l2048n224,
type: KeyType.ecc,
curve: Ecc.secp521r1,
);
final publicKey = privateKey.toPublic;
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 and prime256v1
eddsa type (uses EdDSA legacy algorithm with ed25519 for signing & ECDH algorithm with curve25519 for encryption):
```dart
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):
```dart
const passphrase = 'secret stuff';
final userID = [name, '($comment)', '<$email>'].join(' ');
final privateKey = await OpenPGP.generateKey(
final privateKey = OpenPGP.generateKey(
[userID],
passphrase,
type: KeyGenerationType.ecdsa,
curve: CurveInfo.secp521r1,
type: KeyType.curve25519,
);
final publicKey = privateKey.toPublic;
final publicKey = privateKey.publicKey;
```

eddsa type (uses EdDSA algorithm with ed25519 for signing & ECDH algorithm with curve25519 for encryption):
Curve448 key type (uses Ed448 algorithm for signing & X448 algorithm for encryption):
```dart
const passphrase = 'secret stuff';
final userID = [name, '($comment)', '<$email>'].join(' ');
final privateKey = await OpenPGP.generateKey(
final privateKey = OpenPGP.generateKey(
[userID],
passphrase,
type: KeyGenerationType.eddsa,
type: KeyType.curve448,
);
final publicKey = privateKey.toPublic;
final publicKey = privateKey.publicKey;
```

## Development
Expand Down
20 changes: 16 additions & 4 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
Dart PG Examples
==================
Dart Dart Privacy Guard Examples
================================

* [Encrypt example](encrypt_example.dart): a basic Dart PG encrypt & sign example.
* [Decrypt example](encrypt_example.dart): a basic Dart PG decrypt & verify example.
* [Cleartext signing example](cleartext_signing.dart): a basic cleartext signing example.
* [Key decrypting example](key_decrypting.dart): a basic key decrypting example.
* [Key generation example](key_generation.dart): a basic key generation example.
* [Key reading example](key_reading.dart): a basic key reading example.
* [Literal data example](literal_data.dart): a basic literal data example.

## Run examples
```bash
dart run cleartext_signing.dart
dart run key_decrypting.dart
dart run key_generation.dart
dart run key_reading.dart
dart run literal_data.dart
```
Loading

0 comments on commit aa5ee96

Please sign in to comment.