Skip to content

Commit

Permalink
Improve example with password encryption in README
Browse files Browse the repository at this point in the history
  • Loading branch information
tanx committed Apr 22, 2016
1 parent a6dd8a9 commit 73ab545
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,36 +50,38 @@ var openpgp = require('openpgp'); // use as CommonJS, AMD, ES6 module or via win

openpgp.initWorker({ path:'openpgp.worker.js' }) // set the relative web worker path

openpgp.config.aead_protect = true // activate fast AES-GCM mode (experimental)
openpgp.config.aead_protect = true // activate fast AES-GCM mode (not yet OpenPGP standard)
```

#### Encrypt and decrypt *String* data with a password
#### Encrypt and decrypt *Uint8Array* data with a password

```js
var options, encrypted;

options = {
data: 'Hello, World!', // input as String
passwords: ['secret stuff'] // multiple passwords possible
data: new Uint8Array([0x01, 0x01, 0x01]), // input as Uint8Array (or String)
passwords: ['secret stuff'], // multiple passwords possible
armor: false // don't ASCII armor (for Uint8Array output)
};

openpgp.encrypt(options).then(function(ciphertext) {
encrypted = ciphertext.data; // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
encrypted = ciphertext.message.packets.write(); // get raw encrypted packets as Uint8Array
});
```

```js
options = {
message: openpgp.message.readArmored(encrypted), // parse armored message
password: 'secret stuff' // decrypt with password
message: openpgp.message.read(encrypted), // parse encrypted bytes
password: 'secret stuff' // decrypt with password
format: 'binary' // output as Uint8Array
};

openpgp.decrypt(options).then(function(plaintext) {
return plaintext.data; // 'Hello, World!'
return plaintext.data // Uint8Array([0x01, 0x01, 0x01])
});
```

#### Encrypt and decrypt *Uint8Array* data with PGP keys
#### Encrypt and decrypt *String* data with PGP keys

```js
var options, encrypted;
Expand All @@ -88,27 +90,25 @@ 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
data: 'Hello, World!', // input as String (or Uint8Array)
publicKeys: openpgp.key.readArmored(pubkey).keys, // for encryption
privateKeys: openpgp.key.readArmored(privkey).keys, // for signing (optional)
armor: false // don't ASCII armor
};

openpgp.encrypt(options).then(function(ciphertext) {
encrypted = ciphertext.message.packets.write(); // get raw encrypted packets as Uint8Array
encrypted = ciphertext.data; // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
});
```

```js
options = {
message: openpgp.message.read(encrypted), // parse encrypted bytes
message: openpgp.message.readArmored(encrypted), // parse armored message
publicKeys: openpgp.key.readArmored(pubkey).keys, // for verification (optional)
privateKey: openpgp.key.readArmored(privkey).keys[0], // for decryption
format: 'binary' // output as Uint8Array
};

openpgp.decrypt(options).then(function(plaintext) {
return plaintext.data // Uint8Array([0x01, 0x01, 0x01])
return plaintext.data; // 'Hello, World!'
});
```

Expand Down

0 comments on commit 73ab545

Please sign in to comment.