diff options
author | Daniel Schadt <kingdread@gmx.de> | 2023-11-28 17:37:12 +0100 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2023-11-28 17:38:02 +0100 |
commit | 1737847637522132c1c35e4970d3f5e0f6338b4e (patch) | |
tree | 44fb1f141ef7cc9d94ae94b4b11b83b29953c5dd /README.md | |
parent | ccebb485a85688855ff44f509c6fa224147c13f3 (diff) | |
download | hohibe-1737847637522132c1c35e4970d3f5e0f6338b4e.tar.gz hohibe-1737847637522132c1c35e4970d3f5e0f6338b4e.tar.bz2 hohibe-1737847637522132c1c35e4970d3f5e0f6338b4e.zip |
add an example to the documentation
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 39 |
1 files changed, 39 insertions, 0 deletions
@@ -13,6 +13,45 @@ it to be secure. Use it at **your own risk** and if you know what you are doing! +## Example Code + +```rust +use hohibe::kem::HybridKem; + +const MAX_DEPTH: usize = 3; + +fn main() -> Result<(), Box<dyn std::error::Error>> { + let mut rng = rand::thread_rng(); + let kem = HybridKem::new(MAX_DEPTH); + let (public_key, master_secret) = kem.setup(&mut rng)?; + + // Encrypt for hibe.example.com + let ciphertext = kem.encrypt(&mut rng, &public_key, &["com", "example", "hibe"], b"GET /")?; + + // Assume that the owner of example.com is given the secret key for their domain ... + let example_com = kem.generate_key( + &mut rng, + &public_key, + &master_secret, + &["com", "example"], + )?; + // ... and they can use that to derive the key for the subdomain + let secret_key = kem.derive_key( + &mut rng, + &public_key, + &example_com, + &["com", "example", "hibe"], + )?; + + // Now we can decrypt + let plaintext = kem.decrypt(&public_key, &secret_key, &ciphertext)?; + + assert_eq!(plaintext, b"GET /"); + + Ok(()) +} +``` + ## License hohibe is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. |