Git Product home page Git Product logo

react-native-openpgp's Introduction

React-Native-OpenPGP

React-Native-OpenPGP is a Javascript implementation of the OpenPGP protocol based on OpenPGP.js.

Getting started

Installation

npm install --save react-native-openpgp
react-native link react-native-openpgp

Note: Run npm install -g rnpm if you haven't installed RNPM (React-Native Package Manager) yet! Alternatively you can add the Android and iOS modules library by following the official guide.

Usage

import * as openpgp from 'react-native-openpgp';

Encrypt and decrypt String data with a password

var options, encrypted;

options = {
  data: 'Hello, World!',      // input as String
  passwords: ['secret stuff'] // multiple passwords possible
};

openpgp.encrypt(options)
  .then((ciphertext) => {
    encrypted = ciphertext.data; // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
  })
  .catch((error) => {
    console.log("Something went wrong: " + error);
  });
options = {
  message: openpgp.readMessage(encrypted), // parse armored message
  password: 'secret stuff'                         // decrypt with password
};

openpgp.decrypt(options)
  .then((plaintext) => {
    return plaintext.data; // 'Hello, World!'
  })
  .catch((error) => {
    console.log("Something went wrong: " + error);
  });

Encrypt and decrypt Uint8Array data with PGP keys

var options, encrypted;

var pubkey = '-----BEGIN PGP PUBLIC KEY BLOCK ... END PGP PUBLIC KEY BLOCK-----';
var privkey = '-----BEGIN PGP PRIVATE KEY BLOCK ... END PGP PRIVATE KEY BLOCK-----';

options = {
  data: new Uint8Array([0x01, 0x01, 0x01]),           // input as Uint8Array
  publicKeys: openpgp.readArmoredKey(pubkey).keys,   // for encryption
  privateKeys: openpgp.readArmoredKey(privkey).keys, // for signing (optional)
  armor: false                                        // don't ASCII armor
};

openpgp.encrypt(options)
  .then((ciphertext) => {
    encrypted = ciphertext.message.packets.write(); // get raw encrypted packets as Uint8Array
  })
  .catch((error) => {
    console.log("Something went wrong: " + error);
  });
options = {
  message: openpgp.readBinaryMessage(encrypted),             // parse encrypted bytes
  publicKeys: openpgp.readArmoredKey(pubkey).keys,     // for verification (optional)
  privateKey: openpgp.readArmoredKey(privkey).keys[0], // for decryption
  format: 'binary'                                      // output as Uint8Array
};

openpgp.decrypt(options)
  .then((plaintext) => {
    return plaintext.data // Uint8Array([0x01, 0x01, 0x01])
  })
  .catch((error) => {
    console.log("Something went wrong: " + error);
  });

Generate new key pair

var options = {
  userIds: [{ name:'Jon Smith', email:'[email protected]' }], // multiple user IDs
  numBits: 2048,                                            // RSA key size
  passphrase: 'super long and hard to guess secret'         // protects the private key
};

openpgp.generateKey(options)
  .then((key) => {
    var privkey = key.privateKeyArmored; // '-----BEGIN PGP PRIVATE KEY BLOCK ... '
    var pubkey = key.publicKeyArmored;   // '-----BEGIN PGP PUBLIC KEY BLOCK ... '
  })
  .catch((error) => {
    console.log("Something went wrong: " + error);
  });

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.