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!',
passwords: ['secret stuff']
};
openpgp.encrypt(options)
.then((ciphertext) => {
encrypted = ciphertext.data;
})
.catch((error) => {
console.log("Something went wrong: " + error);
});
options = {
message: openpgp.readMessage(encrypted),
password: 'secret stuff'
};
openpgp.decrypt(options)
.then((plaintext) => {
return plaintext.data;
})
.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]),
publicKeys: openpgp.readArmoredKey(pubkey).keys,
privateKeys: openpgp.readArmoredKey(privkey).keys,
armor: false
};
openpgp.encrypt(options)
.then((ciphertext) => {
encrypted = ciphertext.message.packets.write();
})
.catch((error) => {
console.log("Something went wrong: " + error);
});
options = {
message: openpgp.readBinaryMessage(encrypted),
publicKeys: openpgp.readArmoredKey(pubkey).keys,
privateKey: openpgp.readArmoredKey(privkey).keys[0],
format: 'binary'
};
openpgp.decrypt(options)
.then((plaintext) => {
return plaintext.data
})
.catch((error) => {
console.log("Something went wrong: " + error);
});
Generate new key pair
var options = {
userIds: [{ name:'Jon Smith', email:'jon@example.com' }],
numBits: 2048,
passphrase: 'super long and hard to guess secret'
};
openpgp.generateKey(options)
.then((key) => {
var privkey = key.privateKeyArmored;
var pubkey = key.publicKeyArmored;
})
.catch((error) => {
console.log("Something went wrong: " + error);
});