Add detached binary signature example to README

This commit is contained in:
Will Hilton 2017-01-06 00:06:44 -05:00
parent 5fa35ea1c0
commit f13c341713
No known key found for this signature in database
GPG Key ID: 9609B8A5928BA6B9

View File

@ -185,6 +185,35 @@ openpgp.verify(options).then(function(verified) {
});
```
#### Create and verify *detached* signatures for binary data
```js
var content, detachedSig, validity;
var pubkey = '-----BEGIN PGP PUBLIC KEY BLOCK ... END PGP PUBLIC KEY BLOCK-----';
var privkey = '-----BEGIN PGP PRIVATE KEY BLOCK ... END PGP PRIVATE KEY BLOCK-----';
```
```js
content = 'Hello, World!'; // input as String
var privateKeys = openpgp.key.readArmored(privkey).keys; // for signing
var bytes = openpgp.util.str2Uint8Array(content); // convert text to binary
var message = openpgp.message.fromBinary(bytes);
var signedMessage = message.sign(privateKeys);
var signature = signedMessage.packets.filterByTag(openpgp.enums.packet.signature);
var armoredMessage = openpgp.armor.encode(openpgp.enums.armor.message, signature.write());
armoredMessage = armoredMessage.replace('-----BEGIN PGP MESSAGE-----\r\n', '-----BEGIN PGP SIGNATURE-----\r\n');
armoredMessage = armoredMessage.replace('-----END PGP MESSAGE-----\r\n', '-----END PGP SIGNATURE-----\r\n');
detachedSig = armoredMessage; // '-----BEGIN PGP SIGNATURE ... END PGP SIGNATURE-----'
```
```js
var publicKeys = openpgp.key.readArmored(pubkey).keys; // for verifying signatures
var msg = openpgp.message.readSignedContent(content, detachedSig);
var result = msg.verify(publicKeys);
validity = result[0].valid; // true
```
### Documentation
A jsdoc build of our code comments is available at [doc/index.html](http://openpgpjs.org/openpgpjs/doc/index.html). Public calls should generally be made through the OpenPGP object [doc/openpgp.html](http://openpgpjs.org/openpgpjs/doc/module-openpgp.html).