diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2018-01-31 17:53:38 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2018-01-31 17:53:38 +0100 |
commit | 0f89cb3ddb6c668c488945fce099eff3102b4aa7 (patch) | |
tree | 615bc7f6cfe3f304363ffb5ef96686866e0adb88 /examples | |
parent | 59e0dbc8222fe546d328b31f94d3f8cd1a1097a2 (diff) |
Add the code from the guide as examples.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/guide-exploring-openpgp.rs | 70 | ||||
-rw-r--r-- | examples/guide-getting-started.rs | 17 | ||||
-rw-r--r-- | examples/guide-the-keystore.rs | 57 |
3 files changed, 144 insertions, 0 deletions
diff --git a/examples/guide-exploring-openpgp.rs b/examples/guide-exploring-openpgp.rs new file mode 100644 index 00000000..f8d71de9 --- /dev/null +++ b/examples/guide-exploring-openpgp.rs @@ -0,0 +1,70 @@ +//! https://preview.sequoia-pgp.org/guide/exploring-openpgp/ + +#[macro_use] extern crate openpgp; // For armored! +use openpgp::armor; // For armored! + +fn main() { + let mut reader = armored!( + "-----BEGIN PGP PUBLIC KEY BLOCK----- + + mQENBFpxtsABCADZcBa1Q3ZLZnju18o0+t8LoQuIIeyeUQ0H45y6xUqyrD5HSkVM + VGQs6IHLq70mAizBJ4VznUVqVOh/NhOlapXi6/TKpjHvttdg45o6Pgqa0Kx64luT + ZY+TEKyILcdBdhr3CzsEILnQst5jadgMvU9fnT/EkJIvxtWPlUzU5R7nnALO626x + 2M5Pj3k0h3ZNHMmYQQtReX/RP/xUh2SfOYG6i/MCclIlee8BXHB9k0bW2NAX2W7H + rLDGPm1LzmyqxFGDvDvfPlYZ5nN2cbGsv3w75LDzv75kMhVnkZsrUjnHjVRzFq7q + fSIpxlvJMEMKSIJ/TFztQoOBO5OlBb5qzYPpABEBAAG0F+G8iM+BzrnPg8+Ezr/P + hM6tzrvOt8+CiQFUBBMBCAA+FiEEfcpYtU6xQxad3uFfJH9tq8hJFP4FAlpxt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pDEy0mcajkgIUTvXQaDXKbccX6Wgep8dyBP7YucGmRPd9Z6H + bGeT3KvlJlH5kthQ9shsmT14gYwGMR6rKpNUXmlpetkjqUK7pGVaHGgJWUZ9QPGU + awwPdWWvZSyXJAPZ9lC5sTKwMJDwIxILug== + =lAie + -----END PGP PUBLIC KEY BLOCK-----" + ); + + // Parse message. + let message = openpgp::Message::from_reader(&mut reader).unwrap(); + + // Iterate over children. + for (i, p) in message.children().enumerate() { + println!("{}: {:?}", i, p); + } + + // Some space to make the output easier to parse. + println!(); + + // Parse into TPK. + let tpk = openpgp::tpk::TPK::from_message(message).unwrap(); + println!("Fingerprint: {}", tpk.fingerprint()); + + // List userids. + for (i, u) in tpk.userids().enumerate() { + println!("{}: UID: {}, {} self-signature(s), {} certification(s)", + i, u.userid(), + u.selfsigs().count(), + u.certifications().count()); + } + + // List subkeys. + for (i, s) in tpk.subkeys().enumerate() { + println!("{}: Fingerprint: {}, {} self-signature(s), {} certification(s)", + i, s.subkey().fingerprint(), + s.selfsigs().count(), + s.certifications().count()); + } +} diff --git a/examples/guide-getting-started.rs b/examples/guide-getting-started.rs new file mode 100644 index 00000000..0b9b3a13 --- /dev/null +++ b/examples/guide-getting-started.rs @@ -0,0 +1,17 @@ +//! https://preview.sequoia-pgp.org/guide/getting-started/ + +#[macro_use] extern crate openpgp; // For armored! +use openpgp::armor; // For armored! +use std::io; + +fn main() { + let mut reader = armored!( + "-----BEGIN PGP ARMORED FILE----- + + SGVsbG8gd29ybGQhCg== + =XLsG + -----END PGP ARMORED FILE-----" + ); + + io::copy(&mut reader, &mut io::stdout()).unwrap(); +} diff --git a/examples/guide-the-keystore.rs b/examples/guide-the-keystore.rs new file mode 100644 index 00000000..effd9e71 --- /dev/null +++ b/examples/guide-the-keystore.rs @@ -0,0 +1,57 @@ +//! https://preview.sequoia-pgp.org/guide/the-keystore/ + +#[macro_use] extern crate openpgp; // For armored! +use openpgp::armor; // For armored! +extern crate sequoia; +use sequoia::{core, store}; + +fn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MYitaMUyrwy+OzW3nCal/uJHIfj + bdKx29MbKgF/zaBs8mhTvf/Tu0rIVNDPEicwijDEolGSGebZxdGdHJA31uayMHDK + /mwySJViMZ8b+Lzc/dRgNbQoY6yjsjso7U9OZpQK1fooHOSQS6iLsSSsZLcGPD+7 + m7j3jwq68SIJPMsu0O8hdjFWL4Cfj815CwptAxRGkp00CIusAabO7m8DzwARAQAB + iQE2BBgBCAAgFiEEfcpYtU6xQxad3uFfJH9tq8hJFP4FAlpxtsACGwwACgkQJH9t + q8hJFP5rmQgAoYOUXolTiQmWipJTdMG/VZ5X7mL8JiBWAQ11K1o01cZCMlziyHnJ + xJ6Mqjb6wAFpYBtqysJG/vfjc/XEoKgfFs7+zcuEnt41xJQ6tl/L0VTxs+tEwjZu + Rp/owB9GCkqN9+xNEnlH77TLW1UisW+l0F8CJ2WFOj4lk9rcXcLlEdGmXfWIlVCb + 2/o0DD+HDNsF8nWHpDEy0mcajkgIUTvXQaDXKbccX6Wgep8dyBP7YucGmRPd9Z6H + bGeT3KvlJlH5kthQ9shsmT14gYwGMR6rKpNUXmlpetkjqUK7pGVaHGgJWUZ9QPGU + awwPdWWvZSyXJAPZ9lC5sTKwMJDwIxILug== + =lAie + -----END PGP PUBLIC KEY BLOCK-----" + ); + + // Provide some context. + let ctx = core::Context::new("org.sequoia-pgp.guide").unwrap(); + + // Parse TPK. + let tpk = openpgp::tpk::TPK::from_reader(&mut reader).unwrap(); + + // Open a store. + let store = store::Store::open(&ctx, "default").unwrap(); + + // Store the TPK. + store.import("Ἀριστοτέλης", &tpk).unwrap(); + + // Now let's get it back. + let tpk_ = store.lookup("Ἀριστοτέλης").unwrap().tpk().unwrap(); + assert_eq!(tpk.fingerprint(), tpk_.fingerprint()); +} |