feat: add examples
This commit is contained in:
9
__enclave/virt_enclave/examples/create_signing_key.rs
Normal file
9
__enclave/virt_enclave/examples/create_signing_key.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
use virt_enclave::sig::SigningKeyPair;
|
||||
|
||||
fn main() {
|
||||
let signing_key_pair = SigningKeyPair::new();
|
||||
match signing_key_pair.write_to_file("platform_signing_key.json") {
|
||||
Err(_) => println!("Write platform signing key failed!"),
|
||||
Ok(_) => println!("Write platform signing key successed!"),
|
||||
}
|
||||
}
|
||||
34
__enclave/virt_enclave/examples/sign_file.rs
Normal file
34
__enclave/virt_enclave/examples/sign_file.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use std::io::Read;
|
||||
use std::fs::File;
|
||||
use ring::digest;
|
||||
use virt_enclave::sig::*;
|
||||
|
||||
fn main() {
|
||||
let mut args = std::env::args();
|
||||
args.next();
|
||||
let signing_key_pair = match SigningKeyPair::read_from_file("platform_signing_key.json") {
|
||||
Err(_) => { println!("Read file failed!"); return; },
|
||||
Ok(k) => k,
|
||||
};
|
||||
let f = match args.next() {
|
||||
None => { println!("File not assigned!"); return; },
|
||||
Some(f) => f,
|
||||
};
|
||||
let mut file = match File::open(&f) {
|
||||
Err(_) => { println!("Open file failed: {}", f); return; }
|
||||
Ok(f) => f,
|
||||
};
|
||||
let mut buf = vec![];
|
||||
let _len = match file.read_to_end(&mut buf) {
|
||||
Err(_) => { println!("Read file failed: {}", f); return; }
|
||||
Ok(c) => c,
|
||||
};
|
||||
let d = digest::digest(&digest::SHA256, &buf);
|
||||
let digest_hex = hex::encode(&d);
|
||||
let mut signed_message = SignedMessage::new(d.as_ref().to_vec(), None);
|
||||
signed_message.sign(&signing_key_pair);
|
||||
|
||||
println!("File : {}", f);
|
||||
println!("Hex : {}", digest_hex);
|
||||
println!("Signed: {}", serde_json::to_string(&signed_message).unwrap());
|
||||
}
|
||||
Reference in New Issue
Block a user