Code Monkey home page Code Monkey logo

dart-pg's Introduction

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, 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.

dart-pg's People

Contributors

nguyennv avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.