Add better examples for binary data use. Fix message docs
This commit is contained in:
parent
6c954e19c0
commit
24c213d8d9
60
README.md
60
README.md
|
@ -42,66 +42,74 @@ Or just fetch a minified build under [dist](https://github.com/openpgpjs/openpgp
|
||||||
Here are some examples of how to use the v2.x api. If you're upgrading from v1.x it might help to check out the [documentation](https://github.com/openpgpjs/openpgpjs#documentation).
|
Here are some examples of how to use the v2.x api. If you're upgrading from v1.x it might help to check out the [documentation](https://github.com/openpgpjs/openpgpjs#documentation).
|
||||||
|
|
||||||
#### Set up
|
#### Set up
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import openpgp from 'openpgp.js'; // use as ES6, CommonJS, AMD module or via window.openpgp
|
import openpgp from 'openpgp.js'; // use as ES6, CommonJS, AMD module or via window.openpgp
|
||||||
|
|
||||||
openpgp.initWorker({ path:'openpgp.worker.js' }) // the relative Web Worker path
|
openpgp.initWorker({ path:'openpgp.worker.js' }) // the relative Web Worker path
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Encrypt with password
|
#### Encrypt and decrypt *String* data with a password
|
||||||
|
|
||||||
```js
|
```js
|
||||||
let options = {
|
let options, encrypted;
|
||||||
|
|
||||||
|
options = {
|
||||||
data: 'Hello, World!', // input as String
|
data: 'Hello, World!', // input as String
|
||||||
passwords: ['secret stuff'] // multiple passwords possible
|
passwords: ['secret stuff'] // multiple passwords possible
|
||||||
};
|
};
|
||||||
|
|
||||||
openpgp.encrypt(options).then(ciphertext => { ... });
|
openpgp.encrypt(options).then(armored => {
|
||||||
|
encrypted = armored; // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
|
||||||
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Decrypt with password
|
|
||||||
```js
|
```js
|
||||||
const ciphertext = '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----';
|
options = {
|
||||||
|
message: openpgp.message.readArmored(encrypted), // parse armored message
|
||||||
let options = {
|
password: 'secret stuff' // decrypt with password
|
||||||
message: openpgp.message.readArmored(ciphertext), // the encrypted message
|
|
||||||
password: 'secret stuff', // single password
|
|
||||||
format : 'utf8' // output as String
|
|
||||||
};
|
};
|
||||||
|
|
||||||
openpgp.decrypt(options).then(plaintext => { ... })
|
openpgp.decrypt(options).then(plaintext => {
|
||||||
|
// return 'Hello, World!'
|
||||||
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Encrypt with PGP keys
|
#### Encrypt and decrypt *Uint8Array* data with PGP keys
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
let options, encrypted;
|
||||||
|
|
||||||
const pubkey = '-----BEGIN PGP PUBLIC KEY BLOCK ... END PGP PUBLIC KEY BLOCK-----';
|
const pubkey = '-----BEGIN PGP PUBLIC KEY BLOCK ... END PGP PUBLIC KEY BLOCK-----';
|
||||||
const privkey = '-----BEGIN PGP PRIVATE KEY BLOCK ... END PGP PRIVATE KEY BLOCK-----';
|
const privkey = '-----BEGIN PGP PRIVATE KEY BLOCK ... END PGP PRIVATE KEY BLOCK-----';
|
||||||
|
|
||||||
let options = {
|
options = {
|
||||||
data: new Uint8Array([0x01, 0x01, 0x01]), // input as Uint8Array
|
data: new Uint8Array([0x01, 0x01, 0x01]), // input as Uint8Array
|
||||||
publicKeys: openpgp.key.readArmored(pubkey).keys, // for encryption
|
publicKeys: openpgp.key.readArmored(pubkey).keys, // for encryption
|
||||||
privateKeys: openpgp.key.readArmored(privkey).keys // for signing (optional)
|
privateKeys: openpgp.key.readArmored(privkey).keys, // for signing (optional)
|
||||||
|
armor: false // don't ASCII armor
|
||||||
};
|
};
|
||||||
|
|
||||||
openpgp.encrypt(options).then(ciphertext => { ... });
|
openpgp.encrypt(options).then(message => {
|
||||||
|
encrypted = message.packets.write(); // get raw encrypted packets as Uint8Array
|
||||||
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Decrypt with PGP keys
|
|
||||||
```js
|
```js
|
||||||
const ciphertext = '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----';
|
options = {
|
||||||
const pubkey = '-----BEGIN PGP PUBLIC KEY BLOCK ... END PGP PUBLIC KEY BLOCK-----';
|
message: openpgp.message.read(encrypted), // parse encrypted bytes
|
||||||
const privkey = '-----BEGIN PGP PRIVATE KEY BLOCK ... END PGP PRIVATE KEY BLOCK-----';
|
|
||||||
|
|
||||||
let options = {
|
|
||||||
message: openpgp.message.readArmored(ciphertext), // the encrypted message
|
|
||||||
publicKeys: openpgp.key.readArmored(pubkey).keys, // for verification (optional)
|
publicKeys: openpgp.key.readArmored(pubkey).keys, // for verification (optional)
|
||||||
privateKey: openpgp.key.readArmored(privkey).keys[0], // for decryption
|
privateKey: openpgp.key.readArmored(privkey).keys[0], // for decryption
|
||||||
format: 'binary' // output as Uint8Array
|
format: 'binary' // output as Uint8Array
|
||||||
};
|
};
|
||||||
|
|
||||||
openpgp.decrypt(options).then(plaintext => { ... });
|
openpgp.decrypt(options).then(plaintext => {
|
||||||
|
// return Uint8Array([0x01, 0x01, 0x01])
|
||||||
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Generate new key pair
|
#### Generate new key pair
|
||||||
|
|
||||||
```js
|
```js
|
||||||
let options = {
|
let options = {
|
||||||
userIds: [{ name:'Jon Smith', email:'jon@example.com' }], // multiple user IDs
|
userIds: [{ name:'Jon Smith', email:'jon@example.com' }], // multiple user IDs
|
||||||
|
@ -116,6 +124,7 @@ openpgp.generateKey(options).then(key => {
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Lookup public key on HKP server
|
#### Lookup public key on HKP server
|
||||||
|
|
||||||
```js
|
```js
|
||||||
let hkp = new openpgp.HKP('https://pgp.mit.edu');
|
let hkp = new openpgp.HKP('https://pgp.mit.edu');
|
||||||
|
|
||||||
|
@ -129,6 +138,7 @@ hkp.lookup(options).then(key => {
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Upload public key to HKP server
|
#### Upload public key to HKP server
|
||||||
|
|
||||||
```js
|
```js
|
||||||
let hkp = new openpgp.HKP('https://pgp.mit.edu');
|
let hkp = new openpgp.HKP('https://pgp.mit.edu');
|
||||||
|
|
||||||
|
|
|
@ -398,9 +398,9 @@ export function readArmored(armoredText) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* reads an OpenPGP binary string message and returns a message object
|
* reads an OpenPGP message as byte array and returns a message object
|
||||||
* @param {Uint8Array} binary message
|
* @param {Uint8Array} input binary message
|
||||||
* @return {module:message~Message} new message object
|
* @return {Message} new message object
|
||||||
* @static
|
* @static
|
||||||
*/
|
*/
|
||||||
export function read(input) {
|
export function read(input) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user